简体   繁体   English

查询已查询的域对象时,Grails GORM会给出休眠异常

[英]Grails GORM gives hibernate exception when querying domain object that was already queried

I have a Grails app where controller calls transactional service. 我有一个Grails应用,控制器在其中调用交易服务。 The service has the following body 该服务具有以下内容

def rep = Rep.findById(2708280)

def r = Rep.findById(2708280)
r.accountNumber = "123423565476"
r.save(failOnError: true)

def list = Rep.findAllByRtnAndAccountNumber(
        rep.rtn, rep.accountNumber)

When the last line in the method is called, I get the following hibernate exception: 当方法的最后一行被调​​用时,我得到以下休眠异常:

    ERROR an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session) [AssertionFailure]
org.hibernate.AssertionFailure: collection [com.mydomain.InnerObject.assignedTests] was not processed by flush()
at org.hibernate.engine.CollectionEntry.postFlush(CollectionEntry.java:228)
at org.hibernate.event.def.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:352)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:65)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1709)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
at org.codehaus.groovy.grails.orm.hibernate.metaclass.FindAllByPersistentMethod$1.doInHibernate(FindAllByPersistentMethod.java:113)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:348)
at org.codehaus.groovy.grails.orm.hibernate.metaclass.FindAllByPersistentMethod.doInvokeInternalWithExpressions(FindAllByPersistentMethod.java:73)
at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractClausedStaticPersistentMethod.doInvokeInternal(AbstractClausedStaticPersistentMethod.java:543)
at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractClausedStaticPersistentMethod.doInvokeInternal(AbstractClausedStaticPersistentMethod.java:417)
at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractStaticPersistentMethod.invoke(AbstractStaticPersistentMethod.java:79)
at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractStaticPersistentMethod.invoke(AbstractStaticPersistentMethod.java:72)
at sun.reflect.GeneratedMethodAccessor9819.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:233)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:58)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)
at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.doCall(GormStaticApi.groovy:102)
at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.call(GormStaticApi.groovy)
at org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod.invoke(ClosureStaticMetaMethod.java:62)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:46)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:91)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at com.mydomaint.MyService.$tt__process(MyService.groovy:118)

where 'com.mydomain.InnerObject' is an inner object of my domain object above. 其中“ com.mydomain.InnerObject”是我上面的域对象的内部对象。

static belongsTo = [owner: InnerObject]

InnerObject owner

and assignedTests is a property of InnerObject domain: 而namedTests是InnerObject域的一个属性:

static hasMany = [assignedVouchers: AssignedTests]

Is there anything I am missing here? 我在这里想念什么吗? What does this exception mean? 此异常是什么意思?

That happens probably because you have two objects of the same instance in the session with different values. 之所以发生这种情况,可能是因为您在会话中有两个具有相同实例的对象,但它们的值不同。

rep is unmodified and r is modified (and persisted) rep未修改,而r被修改(并保持)

When you call list , Hibernate tries to flush the session and it gets confused 当您调用list ,Hibernate尝试刷新会话,但会感到困惑

at org.hibernate.engine.CollectionEntry.postFlush(CollectionEntry.java:228)
at org.hibernate.event.def.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:352)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:65)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1709)

Instead you could try to hold the parameters you pass to the list in separate variables. 相反,您可以尝试将传递给列表的参数保存在单独的变量中。 Try that: 试试看:

def r = Rep.findById(2708280)
def p1 = r.rtn
def p2 = r.accountNumber

r.accountNumber = "123423565476"
r.save(failOnError: true)

def list = Rep.findAllByRtnAndAccountNumber(p1, p2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Grails / GORM 2.3 Hibernate寻找抽象域类的持久化表 - Grails/GORM 2.3 Hibernate looking for abstract domain class persisted table Grails:用休眠类替换GORM - Grails:Replacing GORM with Hibernate class 如何判断当前tx(Hibernate)中是否删除了Grails / GORM域实例? - How can I tell if a Grails/GORM domain instance has been deleted in the current tx (Hibernate)? 处理过时域对象引起的并发问题的策略(Grails/GORM/Hibernate) - Strategies for dealing with concurrency issues caused by stale Domain Objects (Grails/GORM/Hibernate) Grails / GORM动态查找器通过关系ID而不是关系对象本身来获取域 - Grails/GORM dynamic finder to get the domain by its relation ID instead of relation object itself 在Hibernate中,当数据库中已存在对象时,为什么saveOrUpdate会给出异常 - In Hibernate why does saveOrUpdate give an exception when object already exists in database 防止在Hibernate中查询子对象 - Prevent child object being queried in Hibernate Grails项目:用于旧式数据库映射的Gorm或JPA / Hibernate批注? - Grails project: Gorm or JPA/Hibernate annotations for legacy db mapping? 当对象不在持久化上下文中时,Hibernate合并不查询DB - Hibernate merge is not querying DB when object is not in persistence context Hibernate deepValidate:grails中的false异常 - Hibernate deepValidate:false exception in grails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM