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.
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;
}
}
And the 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;
}
}
With the above entities, the following HQL query:
select u.firstName, u.userExt.moreInfo from User u
Generates the following SQL query:
select
userx0_.firstName as col_0_0_,
userextx1_.moreInfo as col_1_0_
from
User userx0_,
UserExt userextx1_
where
userx0_.id=userextx1_.id
Which is the expected result.
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.
Related Question