Jpa 从实体获取结果的正确方法
我在kotlin有两个不同的小项目,一个是映射工作(我不使用数据类),另一个是映射不工作 工作示例 书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
@实体
教材(){
@身份证
@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"
}
}