hibernate subquery throwing Null Pointer Exception - java

I am trying to fire a subquery from hibernate, but it is showing NPE:-(Anyone know how to resolve it? Below is the code:
DetachedCriteria criteria = DetachedCriteria.forClass(My.class,"A");
criteria.createAlias("A.a", "a");
criteria.createAlias("A.b", "b");
if(cValue !=null && cValue.size()>0){
criteria.add(Restrictions.in("b.c", cValue));
}
if(StringUtils.isNotEmpty(commodityList)){
DetachedCriteria commCriteria = DetachedCriteria.forClass(My.class,"A1");
commCriteria.createAlias("A1.a", "a1");
commCriteria.add(Restrictions.in("a1.name", commodityList.split(",")));
criteria.add(Subqueries.propertyIn("b.c", commCriteria));
}
criteria.addOrder(Order.asc("b.c"));
return getHibernateTemplate().findByCriteria(criteria);
Stack Trace:
java.lang.NullPointerException
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:318)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:56)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:334)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.springframework.orm.hibernate3.HibernateTemplate$36.doInHibernate(HibernateTemplate.java:1065)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1055)
at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1048)
at com.boeing.kmapi.base.skillset.dao.impl.EssSkillsetCommMapDAOImpl.getEssSkillsetCommMapforSkillsetIdList(EssSkillsetCommMapDAOImpl.java:51)
at com.boeing.kmapi.base.skillset.service.impl.EssGenericReportServiceImpl.filterCommodityDetails(EssGenericReportServiceImpl.java:87)
at com.boeing.kmapi.base.skillset.service.impl.EssGenericReportServiceImpl.getGenericReport(EssGenericReportServiceImpl.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
The issue was resoled by adding projection to inner query.
commCriteria.createAlias("A1.b", "b1");
commCriteria.setProjection(Projections.projectionList().add(Projections.property("b1.c")));

The issue was resoled by adding projection to inner query.
commCriteria.createAlias("A1.b", "b1");
commCriteria.setProjection(Projections.projectionList().add(Projections.property("b1.c")));

It is a bug in Hibernate https://hibernate.atlassian.net/browse/HHH-993
As #Renjith said, it is solved by adding a projection to the DetachedCriteria.

Related

Hibernate OGM With Mongodb Count query with $gt $gte $lt $lte not working with less than 1

I am using Hibernate OGM (5.2.0.Alpha1) with Mongodb (3.4)
I executed Mongodb Count Query Using Hibernate OGM by 2 below way
(1). Count query using Mongodb count query db.tablename.count({..})
(2). Count query by Mongodb Aggregate query...
Description
(1). Count query using Mongodb count query db.tablename.count({..})
public long executeCountQuery(){
//below line Works fine because value is 1.3 with $lt
//String query= "db.sales_stone_details.count({'$and':[{'size' : { '$lt' : 1.3}}]})"
//below line gots Error because value is 0.3(less than 1) with $lt
String query= "db.sales_stone_details.count({'$and':[{'size' : { '$lt' : 0.3}}]})"
long count = entityManager.createNativeQuery(query.toString()).getSingleResult();
return count;
}
(2). Count query by Mongodb Aggregate query...
public long executeCountQuery(){
//below line Works fine because value is 1.3 with $lt
//String query= "db.sales_stone_details.aggregate([{'$match': {'size': {'$lt': 1.3}}},{'$group': {'_id' : ObjectId(\"59df0d28f16b004af040c38c\"), 'countForAggregate' : {'$sum' : 1}}}])"
//below line gots Error because value is 0.3(less than 1) with $lt
String query= "db.sales_stone_details.aggregate([{'$match': {'size': {'$lt': 0.3}}},{'$group': {'_id' : ObjectId(\"59df0d28f16b004af040c38c\"), 'countForAggregate' : {'$sum' : 1}}}])"
List<Stone> stones = entityManager.createNativeQuery(query.toString() , Stone.class).getResultList();
long count = 0;
if (stones != null || !stones.isEmpty()){
count = stones.get(0).getCountForAggregate();
}
return count;
}
Error
org.bson.json.JsonParseException: JSON reader was expecting a value but found 'db'.
at org.bson.json.JsonReader.readBsonType(JsonReader.java:243) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:149) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.bson.Document.parse(Document.java:105) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.bson.Document.parse(Document.java:90) ~[mongo-java-driver-3.6.0-beta2.jar:na]
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.parseAsObject(MongoDBQueryDescriptorBuilder.java:252) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.parse(MongoDBQueryDescriptorBuilder.java:239) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.datastore.mongodb.query.parsing.nativequery.impl.MongoDBQueryDescriptorBuilder.build(MongoDBQueryDescriptorBuilder.java:211) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:892) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.datastore.mongodb.MongoDBDialect.parseNativeQuery(MongoDBDialect.java:169) ~[hibernate-ogm-mongodb-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.dialect.impl.ForwardingGridDialect.parseNativeQuery(ForwardingGridDialect.java:212) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.getCustomQuery(NativeNoSqlQueryInterpreter.java:50) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.query.impl.NativeNoSqlQueryInterpreter.createQueryPlan(NativeNoSqlQueryInterpreter.java:45) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:210) ~[hibernate-core-5.1.10.Final.jar:5.1.10.Final]
at org.hibernate.ogm.hibernatecore.impl.OgmSessionImpl.list(OgmSessionImpl.java:301) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.ogm.query.impl.NoSQLQueryImpl.list(NoSQLQueryImpl.java:130) ~[hibernate-ogm-core-5.2.0.Alpha1.jar:5.2.0.Alpha1]
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606) ~[hibernate-entitymanager-5.1.10.Final.jar:5.1.10.Final]
at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:529) ~[hibernate-entitymanager-5.1.10.Final.jar:5.1.10.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:372) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at com.sun.proxy.$Proxy7512.getSingleResult(Unknown Source) ~[na:na]
at com.techhive.sales.service.impl.GenericModelDaoImpl.executeCountQuery(GenericModelDaoImpl.java:621) ~[classes/:na]
...............
...........
I got same error with $gt , $lte , $gte operators also.
But it seems like $gt , $gte , $lt , $lte is not working with less than 1 value..
Is I am right???
UPDATE 11/01/2018: This issue should be fixed in the next release: 5.2
Yes, you are right. You found a bug. I've created an issue for it and it should be fixed in the next release: https://hibernate.atlassian.net/browse/OGM-1371

No signature of method: java.util.ArrayList.getAt() is applicable for argument types: (HashMap) values: [[:]]

I am using writing groovy code to create list of map. but when I am trying to add map in the list it giving me the exception
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.getAt() is applicable for argument types: (HashMap) values: [[:]]
Possible solutions: getAt(int), getAt(groovy.lang.Range), getAt(groovy.lang.EmptyRange), getAt(int), getAt(java.lang.String), getAt(java.util.Collection)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at
at sun.reflect.GeneratedMethodAccessor146.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1037)
at groovy.lang.Closure.call(Closure.java:415)
at groovy.lang.Closure.call(Closure.java:431)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2187)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2172)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2213)
at org.codehaus.groovy.runtime.dgm$166.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
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.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at ]
at Current(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:190)
at at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
So I have changed my code java style still I am getting same exception
Below is my code
List<Map<String,String>> data = new ArrayList()
Map<String,String> map = new HashMap()
datas.each{adv ->
if(adv.data.equalsIgnoreCase("myData)){
map.put("id",adv.id)
map.put("url", adv.url)
}
}
data.add(map)//getting exception at this point
Following code should work for you,
List data = []
Map map = [:]
datas.each{adv ->
map = [:] //emptying map
if(adv.data.equalsIgnoreCase("myData)){
map["id"] = adv.id
map["url"] = adv.url
}
data << map // inserting map into list
}
println data.dump() // printing list of maps

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

I have a Grails app where controller calls transactional service. 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.
static belongsTo = [owner: InnerObject]
InnerObject owner
and assignedTests is a property of InnerObject domain:
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)
When you call list, Hibernate tries to flush the session and it gets confused
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)

ConcurrentModificationException in foreach loop even though I don't remove anything from arraylist

I know there are similar questions, but I went through them and didn't find an answer.
My code is single threaded, it isn't removing anything or anyhow modifying the arraylist during the loop, just adding.
CODE:
ArrayList<Record> records = helper.get();
System.out.println("Records in arraylist: " + records.size());
for (Record x : records) {
System.out.println("Begin");
Record record = new Record(many parameters);
System.out.println("Middle");
records.add(record);
System.out.println("End");
}
System.out.println("Done reading records and inserting them to arrayList");
Output:
Records in arraylist: 1000
Begin
Middle
End
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Main.Main.main(Main.java:33)
... 5 more
records.add(record);
You modified the ArrayList this line.
You can turn your for-each loop to indexed-for loop to prevent ConcurrentModificationException, or maybe you can just create a new ArrayList to hold your records:
ArrayList<Record> records = helper.get();
ArrayList<Record> result = new ArrayList<Record>();
for (Record x : records) {
Record record = new Record(many parameters);
result.add(record);
}
records.add(record);
Here you modify your array.

Hibernate 4/Spring 3.2 NonUniqueObjectException

Okay, i am very very very new to Hibernate/Spring. I had to upgrade existing code from Hibernate 2 - Hibernate 4 and Spring 1 - Spring 3.
I keep getting this exception when trying to call saveOrUpdate when passing more than one message to the database (it works fine when I only send one message):
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
at org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:686)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:293)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:239)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:728)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:720)
at org.hibernate.engine.spi.CascadingAction$5.cascade(CascadingAction.java:258)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:423)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:264)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:136)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:728)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:720)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:716)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at $Proxy9.saveOrUpdate(Unknown Source)
I have googled till I am blue in the face! I have tried to use merge(). This does not give the exception, but merge inserts NULL values into the Database.
I have tried to use evict(), flush(), same error.
This is the method that causes the error:
public void saveOrUpdate(Object obj) {
Session session = null;
SessionFactory sf = getSessionFactory();
try {
try {
session = sf.getCurrentSession();
} catch (Exception e) {
session = sf.openSession();
}
System.out.println("Object: " + obj);
session.beginTransaction();
session.saveOrUpdate(obj);
session.getTransaction().commit();
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
Any help would be greatly appreciated!
Can you please provide the mappings and the code which calls the saveOrUpdate methods. It looks like the error is in the code which calls the saveOrUpdate
I solved it :)
I don't know if its the best solution, but it works for me and after 2 solid days of struggling I'm happy!
I removed Cascade from the parent object and explicitly called saveOrUpdate on parent and then on the children.
Thanks everyone for the help :)

Categories

Resources