我正在Grails上写一个应用程序。我正在尝试使用addTo-method将子数据库记录添加到父表。我按照有关addTo-method的this文档进行操作。例如,文档说创建父类:
class Author { String name
static hasMany = [fiction: Book, nonFiction: Book]
}
按照这个我创建了我的父类:
class Cafee {
String cafeeName = ""
int totalReservationPlaces = 0
double placeCost = 0
String currencyType = ""
boolean isReservationAvailable = false
boolean reservationTimeLimit = false
boolean reservationDateLimit = false
int totalPlaces = 0
long startTimeLimit = 0
long endTimeLimit = 0
Date startDateLimit = new Date()
Date endDateLimit = new Date()
static constraints = {
cafeeName blank: false, unique: true
}
String getCafeeName(){
return cafeeName
}
static hasMany = [admin: Person]
}
文档说创建子类:
class Book { String title
static belongsTo = [author: Author]
}
按照这个我创建了我的孩子班:
class Person {
transient springSecurityService
String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
String firstName
String lastName
String email
String inn = ""
boolean isAdminCafee = false
static transients = ['springSecurityService']
static belongsTo = [cafee:Cafee]
static constraints = {
username blank: false, unique: true
firstName blank: false
lastName blank: false
password blank: false
email blank: false, unique: true
}
static mapping = {
password column: '`password`'
}
Set<Authority> getAuthorities() {
PersonAuthority.findAllByPerson(this).collect { it.authority }
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
}
}
并且文档说要向父母添加子记录我必须这样做:
def fictBook = new Book(title: "IT")
def nonFictBook = new Book(title: "On Writing: A Memoir of the Craft")
def a = new Author(name: "Stephen King")
.addToFiction(fictBook)
.addToNonFiction(nonFictBook)
.save()
在Bootstrap中关注它我已经完成了这个:
def user = Person.findOrSaveWhere(username: 'testerAndrewRes', password:'password', firstName:'Andrew', lastName:'Bobkov', email:'pragm@gmail.com', isAdminCafee: true,
inn: '1234567890')
println user
if(!user.authorities.contains(adminRole))
{
PersonAuthority.create(user, adminRole, true)
}
def newCafe = new Cafee(cafeeName: "Tarelka").addToAdmin(user).save()
但是我收到了一个错误:
ERROR context.GrailsContextLoaderListener - Error initializing the application: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person
Message: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person
我做错了什么?
答案 0 :(得分:1)
我认为错误信息很明确。
尝试添加:
user.save(flush:true)
之前:
def newCafe = new Cafee(cafeeName: "Tarelka").addToAdmin(user).save()