【发布时间】:2011-08-24 16:24:05
【问题描述】:
我正在使用 grails 1.3.7 和 zkoss,我的域模型如下,我在会话 1 中加载 Person 实体并通过 UI 对其进行更改。
在会话 2 中按保存我想保存实体。
所以从我的作曲家/控制器中,我调用了一个服务方法(事务性),然后执行 person.save(),当我看到 sql 查询被触发时,我看到一个尝试检索员工对象的查询。
之后触发保存并抛出非唯一对象异常 例外
org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话关联:[com.nthdimenzion.domain.Employee#2]
查询
Hibernate: select this_.id as id7_0_, this_.version as version7_0_, this_.emp_id as emp4_7_0_, this_.person_id as person5_7_0_ from person_role this_ where this_.class='com.nthdimenzion.domain.Employee' and this_.emp_id=?
class PersonService {
static transactional = true
def savePerson(Person person) {
person = person.save();
}
}
class Person extends Party{
String firstName;
String middleName;
static hasMany = [ personRoles : PersonRole ] -- lazy loaded
.... }
class PersonRole {
public static enum ROLETYPES{
EMPLOYEE,AUTHOR
};
public boolean hasRoleType (ROLETYPES roleType){
return false;
}
static transients = ['ROLETYPES']
static constraints = {
}
}
class Employee extends PersonRole{
def empRoleType = [ROLETYPES.EMPLOYEE]
String empId
static belongsTo = [person:Person]
static transients = ['empRoleType', 'uid']
static constraints = {
books(nullable:true)
empId(unique:true)
}
static hasMany = [books:Book]
static mapping = { books cascade:"all" }
static belongsTo = [person:Person]
......
}
这种行为正确吗?
【问题讨论】:
标签: hibernate grails grails-orm