I'm trying to create a one to one relationship between 2 entities where the "child" is referenced by the "parent" id as foreign key. Here's my 2 entities:
@Entity
@Table(name = "users")
public class User extends PanacheEntityBase implements Serializable {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
String id;
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL , optional = false)
Profile profile;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}
and
@Entity
@Table(name = "profiles")
public class Profile extends PanacheEntityBase implements Serializable {
@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "user"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "user_id")
String id;
@OneToOne
@MapsId
@JoinColumn(name = "user_id")
User user;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
On save I build the object and persist it:
User user = new User();
Profile profile = new Profile();
user.setProfile(profile);
userRepository.persist(user);
but I get this error
org.jboss.resteasy.spi.UnhandledException: javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [com.myne.entities.Profile.user]
What am I doing wrong?