Reputation: 892
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?
Upvotes: 3
Views: 4037
Reputation: 892
So after more research I finally was able to deal with the problem. I used an hybrid between these 3 posts
https://howtodoinjava.com/hibernate/hibernate-one-to-one-mapping/
https://stackoverflow.com/a/52010963/3353167
https://stackoverflow.com/a/18622821/3353167
As suggested on the first post, using @MapsId will let hibernate know that the child entity will share the parent's id. So I changed my classes to:
@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
@MapsId
Profile profile;
...
And
@Entity
@Table(name = "profiles")
public class Profile extends PanacheEntityBase implements Serializable {
@Id
@Column(name = "user_id")
String id;
// removed the User Column
...
The issue then comes from Hibernate using GenerationType.Auto as suggested in the third post. So to instruct Hibernate to generate a UUID instead, I followed the example in the second post and changed the Profile id to look like:
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "user_id", updatable = false, nullable = false)
@Id
String id;
...
It worked like a charm. I hope it will help anyone else facing the same problem in the future
Upvotes: 5