gorm保存方法导致选择查询触发 [英] gorm save method causing a select query to fire

查看:99
本文介绍了gorm保存方法导致选择查询触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用grails 1.3.7和zkoss,我的域模型如下,我在会话1中加载Person实体,并通过UI对其进行更改。



在会话2中保存的按下我想保存实体。



因此,从我的作曲家/控制器我调用一个服务方法(事务),然后做人.save(),当我看到被解雇的sql查询时,我看到一个试图检索雇员对象的查询。



之后,保存被触发并抛出一个非唯一对象
异常



org.hibernate.NonUniqueObjectException:具有相同标识符值的另一个对象已经与会话相关:[com.nthdimenzion.domain.Employee#2]



查询< h>:选择this_.id作为id7_0_,this_.version作为version7_0_,this_.emp_id作为emp4_7_0_,this_.person_id作为person5_7_0_ from person_ro le this_ where this_.class ='com.nthdimenzion.domain.Employee'and this_.emp_id =?
$ b $ class PersonService {
static transactional = true
def savePerson(Person person){
person = person.save();
}

}

  class Person extends Party {

String firstName;
字符串中间名;
static hasMany = [personRoles:PersonRole] - lazy loaded

....


$ pre $ 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]
静态映射= {books cascade:all}
static belongsTo = [person:Person]
......
}

这种行为是否正确?

解决方案

Employee 映射中指定 empId 是主键 - 这可能是 NonUniqueObjectException



SQL查询必须来自 empId 字段上的唯一约束。



为什么不使用Grails'/ Hibernate隐式 id ,您是否在使用具有特定映射的遗留数据库?

编辑看看为什么独特的约束会导致 NonUniqueObjectException - 请问您可以在没有约束的情况下尝试吗?

如果问题仍然存在,你必须从同一会话中保存两次对象 - 不知道它是如何发生的,也许是由 merge() - 一个 Employee



SQL查询是由唯一性约束引起的,它是正确的。


I am using grails 1.3.7 and zkoss and my domain model is as below, I load the Person entity in session 1 and make changes to it through the UI.

The on press of save in session 2 I want to save the entity.

So from my composer/controller I call a service method(transactional) and then do person.save(), When i see the sql queries being fired I see a query which tries to retrieve the employee object.

After which the save is fired and throws a nonuniqueobjectexception Exception

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.nthdimenzion.domain.Employee#2]

Query

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]
  ......
}

Is this behavior correct ?

解决方案

You must have specified in Employee mapping that empId is the primary key - that's probably the only reason for NonUniqueObjectException.

The SQL query must come from an unique constraint on empId field.

Why not using Grails'/Hibernate implicit id, are you using a legacy database with a specific mapping?

edit I don't see why would unique constraint cause NonUniqueObjectException - can you please try it without the constraint?

If the problem remains, you must have saved the object twice from the same session - no idea how it can happen, maybe by merge()-ing an Employee from earlier session.

The SQL query is caused by uniqueness constraint and it's the right thing.

这篇关于gorm保存方法导致选择查询触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆