问题描述
Kotlin Spring多对多关系存储库无法正常工作。我必须反对与许多多关系的类。当我尝试保存一些示例数据并稍后将其打印出来时。当在JSON数据的第2183行第295列中需要属性名称时,出现错误SyntaxError:JSON.parse:数据结尾。
屏幕截图错误:
这是我的代码:
data class Field(
var name: String = "",@ManyToMany(cascade = arrayOf(CascadeType.ALL))
@JoinTable(name = "field_subject",joinColumns = arrayOf(JoinColumn(name = "field_id",referencedColumnName = "id")),inverseJoinColumns = arrayOf(JoinColumn(name = "subject_id",referencedColumnName = "id")))
var subjects: List<Subject> = mutablelistof<Subject>(),@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = -1
){
override fun toString(): String{
return "{field: ${this.name},subjects: ${subjects.map { it->it.name }}}";
}
}
@Entity @Table(name = "subject")
data class Subject(
var name: String = "",@ManyToMany(mappedBy = "subjects")
var fields: List<Field> = mutablelistof<Field>(),@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long = -1
) {
override fun toString(): String{
return "{subject: ${this.name},fields: ${fields.map { it->it.name }}}";
}
}
@RestController
@RequestMapping("/field")
class FieldController {
@Autowired
lateinit var fieldRepository: FieldRepository
@Autowired
lateinit var subjectRepository: SubjectRepository
@GetMapping
fun getFields() = fieldRepository.findAll()
@PostMapping
fun postField(@RequestBody field: Field): Field{
return fieldRepository.save( Field(field.name));
}
@GetMapping("/save")
fun save(): String {
//prepare subjects
var subject = subjectRepository.findById(1).get()
var secondSubject = subjectRepository.findById(2).get()
println("==================ATTACH SUBJECTS")
println(subject.id.toString() + " - " + subject.id.toString())
// attached subjects for each student
val jack = Field("Field 0",listof(subject,secondSubject))
val peter = Field("Field 1",listof(subject))
// persist students to database
fieldRepository.save(jack)
fieldRepository.save(peter)
return "Done!"
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)