如何使用Hibernate将UUID共享为两个表中的主键?

我正在使用MySql作为数据库,并旨在以二进制(16)格式存储生成的UUID。 在DB中我有两个表:

  1. 存储用户的详细信息。
  2. 存储用户的密码。

我有两个实体UserDetails和UserLogin来分别表示这些表。 他们如下:

UserDetails类:

package Entities import org.hibernate.annotations.GenericGenerator import org.hibernate.annotations.Type import java.io.Serializable import java.util.* import javax.persistence.* /** * class UserDetails */ @Entity @Table(name = "user_details") data class UserDetails( /** * The first name field for the user details table. */ @Column(name = "first_name") var firstName: String, /** * The last name field for the user details table. */ @Column(name = "last_name") var lastName: String, /** * The phone number field for the user details table. */ @Column(name = "phone_number") var phoneNumber: String, /** * The area code field for the user details table. */ @Column(name = "area_code") var areaCode: String, /** * The rating field for the user details table. */ @Column(name = "rating") var rating: Double, /** * The user login information containing the password for the user. */ @OneToOne(cascade = arrayOf(CascadeType.ALL), mappedBy = "user_id") @PrimaryKeyJoinColumn var userLogin: UserLogin ): Serializable { /** * The UserId field for the user details table. */ @Id @Type(type = "uuid-binary") @GeneratedValue(generator = "uuid2") @GenericGenerator(name = "uuid2", strategy = "uuid2") @Column(name = "user_id") lateinit var userId: UUID @Suppress("unused") constructor() : this("", "", "", "", 0.0, UserLogin()) companion object { @JvmStatic val serialVersionUID: Long = 1L } } 

UserLogin类别:

 package Entities import java.io.Serializable import java.util.* import javax.persistence.* /** * class UserLogin * * This table is supposed to be temporary. The frontend has to run on mobile devices which will be using the OTP through * SMS to login to the app. Once that is done there is no need for this table and can be safely deleted w/o having to * modify a huge user_details table. * * @author niksinghal */ @Entity @Table(name = "user_login_details") data class UserLogin( /** * The password field for the login table. * It has to store the encrypted password. */ @Column(name = "password", nullable = false) var password: String ): Serializable { /** * The user ID field for the login table. * This value is a shared primary key and it is populated by the UserDetails.userId field */ @Id @OneToOne lateinit var userId: UUID @Suppress("unused") constructor() : this("") companion object { @JvmStatic val serialVersionUID: Long = 1L } } 

目标是每个用户详细信息行(OneToOne Mapping)都有一个密码,并且对于主键userId具有相同的值。

我正在编译和获取 – 调用init方法失败; 嵌套的异常是org.hibernate.AnnotationException:@OneToOne或@ManyToOne对Entities.UserLogin.userId引用一个未知的实体:java.util.UUID

我对此很新,为了学习Hibernate的概念,所有这些都是严格的。 这就是说,我的理解是UserDetails类的userId将由生成器生成。 UserDetails的UserLogin上方的@OneToOne&@PrimaryKeyJoinColumn标注应该将生成的UUID推送到UserLogin的userId,该标识又由@OneToOne标记。

请让我知道为什么我得到错误? 如果我可以将二进制(16)中的UUID存储在数据库中?

OneToOne注释用于关联整个实体,而不是单个值。 你需要告诉Hibernate如何产生你的密钥。

还有现有的SOF-Threads回答你的问题。 例如这个线程

我在这里做了很多错误的事情:

  • 首先,我正在使用mappedBy错误。 mappedBy的值必须是类中的字段或名称以小写字母开头的类的名称。 我正在使用实际表中的列的名称。

  • 其次,我试图在两张表之间实现单向1-1关系(后面我学到了这一点)。 目的是分享主要关键。 这必须在UserDetails类的userLogin中使用@OneToOne和@PrimaryKeyJoinColumn注释完成。

  • 第三,德克斯特是对的。 @OneToOne只能用于实体。

  • 第四,我没有在DB中的表中设置约束。

    好消息是,二进制UUID工作! 好极了!!