我如何在Kotlin中与超类一对一

时间:2018-08-05 04:12:23

标签: kotlin spring-data-jpa one-to-one mappedsuperclass

我有不同的用户类型:作者和评论者。我希望他们与User类保持OneToOne的关系。用户类将包含spring安全逻辑。我创建了BlogUser的超类,我的Author和Commenter对其进行了扩展。当我尝试设置休眠映射时,我得到:

  

未知的映射依据:   com.legge.blenderBlog.models.security.User.blogUser,已引用   属性未知:com.legge.blenderBlog.models.abstract.BlogUser.user

我的想法错了吗?

@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class BlogUser(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        val id: Long = 0,

        @OneToOne(fetch=FetchType.LAZY)
        @JoinColumn(name="USER_ID")
        val user: User


) : DateAudit()

这是我的Author.kt

@Entity
@Table(name = "author")
class Author(user:User
): BlogUser(user = user)

这是User类的一部分

@Entity
@Table(name = "sec_user", uniqueConstraints = [(UniqueConstraint(columnNames = arrayOf("username"))), (UniqueConstraint(columnNames = arrayOf("email")))])
open class User(

        ....

        @OneToOne(fetch=FetchType.LAZY, mappedBy="user")
        var blogUser: BlogUser?,

        ...

) : DateAudit()

这是dateAudit

@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
@JsonIgnoreProperties(value = ["dateCreated", "dateUpdated"], allowGetters = true)
abstract class DateAudit : Serializable {
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "date_created", nullable = false, updatable = false)
    @CreatedDate
    var dateCreated: Date = Date()

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "date_updated", nullable = false)
    @LastModifiedDate
    var dateUpdated: Date = Date()
}

0 个答案:

没有答案