【发布时间】:2017-10-06 21:09:22
【问题描述】:
我使用 MySql 作为数据库,旨在以 Binary(16) 格式存储生成的 UUID。在数据库中,我有两个表:
- 存储用户的详细信息。
- 存储用户的密码。
我有两个实体 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 映射)设置一个密码,并且主键 userId 具有相同的值。
我正在编译它并得到 - init 方法的调用失败;嵌套异常是 org.hibernate.AnnotationException: @OneToOne 或 @ManyToOne on Entities.UserLogin.userId 引用了一个未知实体:java.util.UUID
我对此非常陌生,并且严格执行所有这些操作以学习 Hibernate 概念。话虽如此,我的理解是 UserDetails 类的 userId 将由生成器生成。 UserDetails 的 UserLogin 上方的 @OneToOne 和 @PrimaryKeyJoinColumn 注释应该将生成的 UUID 推送到 UserLogin 的 userId,该 userId 再次由 @OneToOne 标记。
请告诉我为什么会收到错误消息?如果我能够将 UUID 以二进制 (16) 形式存储在数据库中?
【问题讨论】:
标签: java mysql hibernate kotlin