I'm struggling to successfully implement a OneToOne mapping on my play framework application.
examples I have are:
@Entity
public class Profile extends GenericModel {
@Id
@GeneratedValue(generator = "foreignGenerator")
@GenericGenerator(name = "foreignGenerator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "user"))
public static int id;
@OneToOne(mappedBy="profile", cascade = {CascadeType.ALL})
public static User user;
}
and :
@Entity
public class User extends Model {
@Required
public String firstName;
@Required
public String surname;
}
in this setup it throws:
org.hibernate.AnnotationException: No identifier specified for entity: models.Profile
EDIT: As per Christian Boariu's answer, I have modified Profile to what you have suggested and User to:
@Entity
public class User extends GenericModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long user_id;
@Required
public String firstName;
@Required
public String surname;
@OneToOne(cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "profile_id")
public Profile profile;
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}
Also added getter/setter to Profile:
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
But I am now getting hibernate.id.IdentifierGenerationException: null id generated for:class models.Profile
.. not sure how to correct?
The problem is about your Id definition.
It should not be
static
.Also, user should not be static as well.
UPDATE:
So your class should be like this:
Fixed. suggesstions, as above fixed the @OneToOne issue and the hibernate.id.IdentifierGenerationException: null id generated for:class models.Profile was due to trying to persist an entity with a null id - due to using @primaryKeyJoin, so changed to @JoinColumn