【问题标题】:Proper way to get results from entitys从实体获取结果的正确方法
【发布时间】:2020-05-08 21:54:43
【问题描述】:

我在 kotlin 中有两个不同的小项目,一个是映射工作的(我不使用数据类),另一个是映射不起作用的。

工作示例 预订

@Entity
class Book()  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int? = null
    var name: String? = null

    @ManyToMany(cascade = [CascadeType.PERSIST, CascadeType.MERGE])
    @JoinTable(name = "BOOK_AUTHOR", joinColumns = [JoinColumn(name = "BOOK_ID", referencedColumnName = "ID")], inverseJoinColumns = [JoinColumn(name = "AUTHOR_ID", referencedColumnName = "ID")])
    private var authors: MutableSet<Author> = HashSet()

//    constructor(name: String?, authors: Set<Author>) : this() {
//        this.name = name
//
//        setAuthors(authors)
//    }

    fun getAuthors(): Set<Author> {
        return authors
    }

    fun setAuthors(authors: Set<Author>) {

        for (author in authors) {
            author.books.add(this)
            this.authors.add(author)
        }
    }
    }

作者

   @Entity
class Author()  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int? = null
    var name: String? = null

    @ManyToMany(mappedBy = "authors")
    val books: MutableSet<Book> = HashSet<Book>()


//    constructor(name: String?) : this() {
//        this.name = name
//    }



}

现在那个不工作了,有数据类

@Entity
data class Book  (
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        val id:Int,
        var name:String,
        @ManyToMany(cascade = [CascadeType.PERSIST, CascadeType.MERGE])
        @JoinTable(name = "BOOK_AUTHOR", joinColumns = [JoinColumn(name = "BOOK_ID", referencedColumnName = "ID")], inverseJoinColumns = [JoinColumn(name = "AUTHOR_ID", referencedColumnName = "ID")])
        private var authors: MutableSet<Author> = HashSet()


){
    constructor() : this(-1,"", mutableSetOf()) {}
//    constructor(name: String) : this() {
//        this.name = name
//        authors = HashSet()
//    }

    constructor(name: String, authors: Set<Author>) : this() {
        this.name = name
        //this.authors = authors;
        setAuthors(authors)
    }

    fun getAuthors(): Set<Author> {
        return authors
    }

    fun setAuthors(authors: Set<Author>) { //this.authors = authors;
//add relations manually
        for (author in authors) {
            author.books.add(this)
            this.authors.add(author)
        }
    }
    override fun toString(): String {
        return "heya"
    }
}

作者

@Entity
    data class Author(
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            val id:Int,
            var name:String,
            @ManyToMany(mappedBy = "authors")
            val books: MutableSet<Book> = HashSet<Book>()

    ){
        constructor() : this(-1,"", mutableSetOf()) {}
        constructor(name: String) : this() {
            this.name = name
        }

        override fun toString(): String {
            return "heya"
        }
    }

现在我可以在第一个示例中使用它,但我不使用 kotlin 提供的数据类,我不确定这是否重要, here 提到部分功能将不可用。

其次,我想了解数据类映射工作需要什么,因为现在它总是抛出堆栈溢出错误,

【问题讨论】:

    标签: jpa kotlin spring-data-jpa spring-data


    【解决方案1】:

    根据 JPS 规范,实体类应该是非最终的,并且应该至少有一个无参数构造函数,并且所有要持久的字段也应该是非最终的。

    所以 kotlin 数据类可能不起作用(但这取决于您使用的 JPA 实现)。我建议将open class 用于实体类。

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      相关资源
      最近更新 更多