如何与Hibernate和Kotlin建立正确的ManyToMany关系? (获取BeanCreationException)

时间:2020-01-13 07:56:14

标签: java spring hibernate kotlin

我有以下代码引发以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: project, for columns: [org.hibernate.mapping.Column(type)]

我认为我以错误的方式创建了@ManyToMany映射。这是有问题的主要课程:

@Entity
class Project (

    @GeneratedValue
    @Id
    var id : UUID? = null,

    var owner: Int,

    @get:ManyToMany(mappedBy = "project", fetch = FetchType.EAGER)
    var type: MutableSet<Type> = mutableSetOf()

    )



第二类是:

class Type(

    @GeneratedValue
    @Id
    var id: UUID? = null,

    var oldId: Int,

    var value: String,

    @get:ManyToMany(fetch = FetchType.EAGER)
    @get:JoinColumn(name = "project_id")
    var projects: MutableSet<Project> = mutableSetOf()
) 

一个项目可以有许多类型,而一个类型可以属于许多项目。是什么导致异常?

2 个答案:

答案 0 :(得分:1)

Project实体中,

@get:ManyToMany(mappedBy = "project", fetch = FetchType.EAGER)
    var type: MutableSet<Type> = mutableSetOf()

在这里,mappedBy的值为project

同时,在Type实体中,变量名称为projects

因此,如果您仅按以下方式更新Project实体的类型字段:

 @get:ManyToMany(mappedBy = "projects", fetch = FetchType.EAGER)
        var type: MutableSet<Type> = mutableSetOf()

这应该很好。希望有帮助。

答案 1 :(得分:0)

更改

@get:ManyToMany
@get:JoinColumn

@ManyToMany
@JoinColumn