[英]Hibernate OneToOne attempted to assign id from null one-to-one property
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.我正在尝试在 2 个实体之间创建一对一的关系,其中“子”被“父”ID 引用为外键。 Here's my 2 entities:
这是我的 2 个实体:
@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] org.jboss.resteasy.spi.UnhandledException:javax.persistence.PersistenceException:org.hibernate.id.IdentifierGenerationException:试图从空一对一属性[com.myne.entities.Profile.user]分配id
What am I doing wrong?我究竟做错了什么?
So after more research I finally was able to deal with the problem.所以经过更多的研究,我终于能够解决这个问题。 I used an hybrid between these 3 posts
我在这 3 个帖子之间使用了混合
https://howtodoinjava.com/hibernate/hibernate-one-to-one-mapping/ https://howtodoinjava.com/hibernate/hibernate-one-to-one-mapping/
https://stackoverflow.com/a/52010963/3353167 https://stackoverflow.com/a/52010963/3353167
https://stackoverflow.com/a/18622821/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.正如第一篇文章所建议的,使用@MapsId 会让休眠知道子实体将共享父实体的 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.然后问题来自使用 GenerationType.Auto 的 Hibernate,如第三篇文章中所建议的。 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:
因此,为了指示 Hibernate 生成 UUID,我按照第二篇文章中的示例将 Profile id 更改为如下所示:
@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
我希望它可以帮助将来遇到同样问题的其他人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.