Grails/GORM:org.hibernate.AssertionFailure:xyz中的null id(发生异常后不要刷新会话)
我有一个grails/jaxrs应用程序无法自动持久化嵌套对象图,我想知道我是否可以通过数据模型来实现它 资源正确地反序列化对象,然后将保存父对象(Tautor),但无法自动保存子对象(Tooks)。子项具有空ID,以及对父项的空引用 我可以手动创建子对象,但我正在寻找更好的方法来管理它 域类Grails/GORM:org.hibernate.AssertionFailure:xyz中的null id(发生异常后不要刷新会话),hibernate,grails,gorm,Hibernate,Grails,Gorm,我有一个grails/jaxrs应用程序无法自动持久化嵌套对象图,我想知道我是否可以通过数据模型来实现它 资源正确地反序列化对象,然后将保存父对象(Tautor),但无法自动保存子对象(Tooks)。子项具有空ID,以及对父项的空引用 我可以手动创建子对象,但我正在寻找更好的方法来管理它 域类 class Tauthor { String nameShort Integer age static hasMany = [tooks:Took] } class Too
class Tauthor {
String nameShort
Integer age
static hasMany = [tooks:Took]
}
class Took {
String title;
static belongsTo = [tauthor:Tauthor]
}
资源
@Consumes([MediaType.APPLICATION_JSON, "application/json"])
@Produces([MediaType.APPLICATION_JSON, "application/json"])
@Path('/api/tauthor')
class TauthorResource {
TauthorService tauthorService
@POST
Tauthor create(Tauthor dto) {
Tauthor created = tauthorService.save(dto)
if(!created.hasErrors()) {
return created
}
}
}
class TauthorService {
Tauthor save(Tauthor dto) {
dto.validate()
if (dto.hasErrors()) {
return dto
}
return dto.save()
}
}
class TauthorService {
Tauthor save(Tauthor dto) {
dto.validate()
if (dto.hasErrors()) {
return dto
}
// remove tooks, and create them separately
Set<Took> tooks = []
tooks += dto.tooks
tooks.each { took ->
dto.removeFromTooks(took)
}
Tauthor created = dto.save()
// readd tooks to tauthors
tooks.each { took->
took.tauthor = created
took.save()
}
tooks.each { took->
created.addToTooks(took)
}
created.save()
return created
}
}
需要但中断的服务
@Consumes([MediaType.APPLICATION_JSON, "application/json"])
@Produces([MediaType.APPLICATION_JSON, "application/json"])
@Path('/api/tauthor')
class TauthorResource {
TauthorService tauthorService
@POST
Tauthor create(Tauthor dto) {
Tauthor created = tauthorService.save(dto)
if(!created.hasErrors()) {
return created
}
}
}
class TauthorService {
Tauthor save(Tauthor dto) {
dto.validate()
if (dto.hasErrors()) {
return dto
}
return dto.save()
}
}
class TauthorService {
Tauthor save(Tauthor dto) {
dto.validate()
if (dto.hasErrors()) {
return dto
}
// remove tooks, and create them separately
Set<Took> tooks = []
tooks += dto.tooks
tooks.each { took ->
dto.removeFromTooks(took)
}
Tauthor created = dto.save()
// readd tooks to tauthors
tooks.each { took->
took.tauthor = created
took.save()
}
tooks.each { took->
created.addToTooks(took)
}
created.save()
return created
}
}
工作服务
@Consumes([MediaType.APPLICATION_JSON, "application/json"])
@Produces([MediaType.APPLICATION_JSON, "application/json"])
@Path('/api/tauthor')
class TauthorResource {
TauthorService tauthorService
@POST
Tauthor create(Tauthor dto) {
Tauthor created = tauthorService.save(dto)
if(!created.hasErrors()) {
return created
}
}
}
class TauthorService {
Tauthor save(Tauthor dto) {
dto.validate()
if (dto.hasErrors()) {
return dto
}
return dto.save()
}
}
class TauthorService {
Tauthor save(Tauthor dto) {
dto.validate()
if (dto.hasErrors()) {
return dto
}
// remove tooks, and create them separately
Set<Took> tooks = []
tooks += dto.tooks
tooks.each { took ->
dto.removeFromTooks(took)
}
Tauthor created = dto.save()
// readd tooks to tauthors
tooks.each { took->
took.tauthor = created
took.save()
}
tooks.each { took->
created.addToTooks(took)
}
created.save()
return created
}
}