java 如何在休眠中将多个类映射到一张表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29655306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i map multiple classes to one table in hibernate?
提问by kennyg
From my research it seems not likely that its possible, but here is my use case.
从我的研究来看,这似乎不太可能,但这是我的用例。
I have a table called user with an address id.
我有一个名为 user 的表,带有地址 ID。
I'd like to map a UserReference and User class to this table where UserReference does not contain the Address object (to save sql joining time) and User doescontain the Address object (in case it's needed).
我想一个UserReference和用户类映射到这个表,其中UserReference不包含地址对象(以保存SQL连接时间)和用户不包含地址对象(如果它需要)。
I can't use since it expects a join table, and I can't just define two separate classes because a get() seems to return double of every row (one for User, one for UserReference).
我不能使用,因为它需要一个连接表,而且我不能只定义两个单独的类,因为 get() 似乎返回每一行的两倍(一个用于 User,一个用于 UserReference)。
Does anyone know how I could go about this?
有谁知道我该怎么做?
I guess I should try to explain myself better.
我想我应该试着更好地解释自己。
public class UserReference {
private int id;
private String name;
}
public class User extends UserReference {
private Address;
}
I'd like to map both UserReference and User so that I can query for UserReference when I only need the basic details, and User whenever I need the full object.
我想同时映射 UserReference 和 User,以便我可以在只需要基本详细信息时查询 UserReference,而在需要完整对象时查询 User。
I understand that I can simply have Address be lazy loaded but I have a two tier system which unproxies all objects once it passes through the layers.
我知道我可以简单地将地址延迟加载,但我有一个两层系统,一旦它通过层,它就会取消代理所有对象。
edit:
编辑:
Perhaps my question was too ambigious but based on the answers it doesn't seem very possible to do what I want. Thanks anyway everyone.
也许我的问题太含糊了,但根据答案似乎不太可能做我想做的事。还是谢谢大家。
回答by Bartun
UserReference.java
用户参考.java
@Entity
@DiscriminatorColumn(name = "type")
@DiscriminatorValue(value = "UserReference")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class UserReference extends BaseEntity {
private String name;
}
User.java
用户.java
@Entity
@DiscriminatorValue(value = "User")
public class User extends UserReference {
@OneToOne
@JoinColumn(name = "address_id")
private Address address;
}
回答by Prasad Kharkar
Your question is not very clear to me but I'll try to answer as per my understanding. It depends upon what you really want to persist and what you want as a java entity.
你的问题对我来说不是很清楚,但我会尽量按照我的理解来回答。这取决于您真正想要持久化的内容以及您想要作为 Java 实体的内容。
Consider you want to represent User class with a database table i.e. represent User as entity in JPA. Now you can Create an Embeddable class which will be part of another class.
考虑您想用数据库表表示 User 类,即将 User 表示为 JPA 中的实体。现在您可以创建一个 Embeddable 类,它将成为另一个类的一部分。
So you can map an Address class in User Entity and make the Address class as Embeddable using @Embeddable annotationpackage com.thejavageek.embeddabledemo;
因此,您可以在 User Entity 中映射 Address 类,并使用@Embeddable 注释包 com.thejavageek.embeddabledemo将 Address 类设为可嵌入;
import javax.persistence.Embeddable;
@Embeddable
public class Address {
private String area;
private String city;
private String pincode;
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}
and you can embed this embeddable class as below
你可以嵌入这个可嵌入的类,如下所示
package com.thejavageek.embeddabledemo;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;
@Entity
public class Person {
@TableGenerator(name = "person_gen", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", allocationSize = 100)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "person_gen")
private String idperson;
private String name;
@Embedded
private Address address;
public String getIdperson() {
return idperson;
}
public void setIdperson(String idperson) {
this.idperson = idperson;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
回答by Adrian Shum
I think what you mean is you have a table like
我想你的意思是你有一张像
USER
- USER_ID
- USER_NAME
- ADDRESS_ID
and
和
ADDRESS
- ADDRESS_ID
- ADDRESS_DETAIL
something like that?
类似的东西?
And you want to have 2 User
entities, one of it contains reference to Address and another did not (so it save SQL join time, as you said), is my understanding correct?
并且您想要有 2 个User
实体,其中一个包含对 Address 的引用,另一个没有(因此它节省了 SQL 连接时间,正如您所说),我的理解是否正确?
If so, you do not need to do so.
如果是这样,则不需要这样做。
Simply have one User
entity and put its Address
relationship to be LAZY fetched.
只需拥有一个User
实体并将其Address
关系设置为 LAZY 获取。
If in any function you need Address
in your User
, do a join fetch.
如果你需要的任何功能Address
在你的User
,做一个连接抓取。
e.g.
例如
@Entity
public class User {
@Id
private Long id;
private String name;
@ManyToOne(fetch=FetchType.LAZY)
private Address address;
}
in such case, all normal retrieval of User
will not incur any join to Address
table. However it is flexible to do something like
在这种情况下,所有正常的检索User
都不会导致任何Address
表连接。然而,做类似的事情是灵活的
from User u left join fetch u.address where u.name like 'blabala'