Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 从实体获取结果的正确方法_Jpa_Kotlin_Spring Data Jpa_Spring Data - Fatal编程技术网

Jpa 从实体获取结果的正确方法

Jpa 从实体获取结果的正确方法,jpa,kotlin,spring-data-jpa,spring-data,Jpa,Kotlin,Spring Data Jpa,Spring Data,我在kotlin有两个不同的小项目,一个是映射工作(我不使用数据类),另一个是映射不工作 工作示例 书 @实体 教材(){ @身份证 @GeneratedValue(策略=GenerationType.IDENTITY) 变量id:Int?=null 变量名称:字符串?=null @ManyToMany(cascade=[CascadeType.PERSIST,CascadeType.MERGE]) @JoinTable(name=“BOOK\u AUTHOR”,joinColumns=[Joi

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

工作示例 书

@实体
教材(){
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
变量id:Int?=null
变量名称:字符串?=null
@ManyToMany(cascade=[CascadeType.PERSIST,CascadeType.MERGE])
@JoinTable(name=“BOOK\u AUTHOR”,joinColumns=[JoinColumn(name=“BOOK\u ID”,referencedColumnName=“ID”)],inverseJoinColumns=[JoinColumn(name=“AUTHOR\u ID”,referencedColumnName=“ID”))
private-var作者:提到了MutableSet,其中一些功能将不可用


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

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

所以kotlin数据类可能不起作用(但这取决于您使用的JPA实现)

@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"
        }
    }