I have a User class that has @Embedded a class Profile. How can I give the instances of Profile a reference to their owner the User class?
@Entity
class User imp
Assuming JPA rather than strictly Hibernate, you might do this by applying @Embedded
to a getter/setter pair rather than to the private member itself.
@Entity
class User implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Access(AccessType.PROPERTY)
@Embedded
private Profile profile;
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
this.profile.setUser(this);
}
// ...
}
However, I would question whether an embedded entity is what you want at all in this case, as opposed to a @OneToOne relationship or simply "flattening" the Profile class into User. The main rationale for @Embeddable is code reuse, which seems unlikely in this scenario.