Secondarytables或OnetoOne关联? [英] Secondarytables or OnetoOne associations?
问题描述
考虑以下模型":
USER
Long: PK
String: firstName
String: lastName
USER_EXT
Long: PK
String: moreInfo
Date: lastModified
我正在尝试查找/创建正确的Hibernate映射(使用注释),以便使用像"from User"这样的HQL查询,它会生成以下SQL:
I'm trying to find/create the correct Hibernate mapping (using Annotations) such that, with an HQL query as simple as "from User", it would generate the following SQL:
select firstName, moreInfo from USER, USER_EXT where user.pk = user_ext.pk
我已经尝试了一切,从使用@Secondarytable到@OneToOne关联,但我无法使其正常工作.
I've tried everything, from using @Secondarytable to @OneToOne association, but I can't make it work.
我现在最好的结果是使用@OneToOne关联,该关联会生成多个SQL查询,一个查询用于获取USER中的行,并为结果集中的每一行从USER_EXT中选择查询.
The best result I have now is with the @OneToOne association which generate multiple SQL queries, one to fetch rows in USER and for each rows in the resultset a select query from USER_EXT.
这是无效的.
有什么主意吗?
推荐答案
在OneToOne
和Secondarytable
之间进行选择在某种程度上取决于对象模型(即,是否要为用户扩展名提供实体).我选择使用OneToOne
关联和两个实体.
Choosing between OneToOne
and Secondarytable
somehow depends on the object model (i.e. if you want an entity for the user extension). I chose to use a OneToOne
association and two entities.
对于User
(请注意将PrimaryKeyJoinColumn
用于共享主键):
For the User
(note the use of the PrimaryKeyJoinColumn
for the shared primary key):
@Entity
public class User {
@Id private Long id;
private String firstName;
private String lastName;
@OneToOne
@PrimaryKeyJoinColumn
private UserExt userExt;
public UserExt getUserExt() {
return userExt;
}
public void setUserExt(UserExt userExt) {
this.userExt = userExt;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
和UserExt
:
@Entity
public class UserExt {
@Id private Long id;
private String moreInfo;
@Temporal(TemporalType.DATE)
private Date lastModified;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMoreInfo() {
return moreInfo;
}
public void setMoreInfo(String moreInfo) {
this.moreInfo = moreInfo;
}
public Date getLastModified() {
return lastModified;
}
public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}
}
使用上述实体,可以进行以下HQL查询:
With the above entities, the following HQL query:
select u.firstName, u.userExt.moreInfo from User u
生成以下SQL查询:
select
userx0_.firstName as col_0_0_,
userextx1_.moreInfo as col_1_0_
from
User userx0_,
UserExt userextx1_
where
userx0_.id=userextx1_.id
这是预期的结果.
PS:JPA 1.0实际上不能很好地支持派生标识符(JPA 2.0中的情况要好得多),除非您使用特定于Hibernate的foreign
生成器,否则您必须在UserExt
上手动设置Id
. .有关详细信息,请参见下面的问题.
PS: JPA 1.0 actually provides poor support of derived identifiers (things are much better in JPA 2.0) and you will have to set the Id
manually on the UserExt
, unless you use an Hibernate specific foreign
generator. See the question below for details.
这篇关于Secondarytables或OnetoOne关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!