These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: saveOrUpdate() issue
PostPosted: Sat Feb 25, 2006 7:39 am 
Newbie

Joined: Thu Sep 15, 2005 6:53 am
Posts: 7
hibernate verison: 3.0.5
issue
using hibernate in managed environment.
Using sessionFactory bound to jndi for getting sesison.
Binding Interceptor to Sesseionfactory.
code is :
sessionfactory = new Configuration().configure(url1).setInterceptor(interceptor).buildSessionFactory();
session =sessionfactory.getCurrentSession();
FuelType ft = new FuelType();
ft.setCreationDate(new Date());
ft.setName("petrol");
session.saveOrUpdate(ft);
session.flush();

However, if session.save() method is used then it works.

While persisting , following error comes:
org.hibernate.TransientObjectException: The given object has a null identifier: com.bluecycle.autosalvage.entities.FuelType
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.getUpdateId(DefaultSaveOrUpdateEventListener.java:225)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:212)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:91)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:463)
at com.hibernate.ejb.HibSessionTesterBean.testInsert(HibSessionTesterBean.java:117)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 25, 2006 5:46 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:21 pm
Posts: 61
Location: Hannover
Hi,

please post your mapping files. Seems like you're mapping is wrong or you're database has problems generating the identifier.

Joerg

Don't forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 26, 2006 4:59 pm 
Newbie

Joined: Tue Jan 25, 2005 3:03 am
Posts: 11
Location: Bangalore, India
jherbst wrote:
Hi,

please post your mapping files. Seems like you're mapping is wrong or you're database has problems generating the identifier.

Joerg

Don't forget to rate!


Joerg is right about the mapping files. However, I guess we can safely ignore the "problem while generating identifier" assumption, since you are able to get it working with save().

From the stacktrace it is evident that Hibernate is (wrongly) considering your FuelType object as detached instead of transient. Consequently, it is actually trying to update the object, instead of saving it. This also explains why it is (unnecessarily) cribbing about the id being null, and also why calling "save(...)" explicitly is working fine.

The relevant Hibernate code that does this check is the ForeignKeys.isTransient(...) method. Now, the first line of this method seems promising :

Code:
Boolean isUnsaved = session.getInterceptor().isTransient(entity);


....considering the fact that you have configured your own interceptor :

Code:
new Configuration().configure(url1).setInterceptor(interceptor).buildSessionFactory();


So, I guess you need to check your Interceptor implementation, w.r.t the isTransient(entity) method. Unless it is returning null (like the default EmptyInterceptor), there is a strong chance that your interceptor might be at fault.

If the interceptor comes clean, it could be that you have specified an incorrect value for the "unsaved-value" setting for the identity property of FuelType entity. The mapping files would come handy in figuring out that.

_________________
Rgds,
Vikas.

"Quidquid latine dictum sit, altum sonatur".
Whatever is said in Latin, sounds profound


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 12:34 am 
Newbie

Joined: Thu Sep 15, 2005 6:53 am
Posts: 7
thanks a lot Vikas.
in my Interceptor isTransient method was returning "false". Method return paramtere type is BOOLEAN. when i changed to null , saveorUpdate worked.

However, i noticed, preFlush and postFlush is getting called twice. any ideas..
Here is mapping file:
<property name="hibernate.session_factory_name">HibernateSessionFactory</property>
<property name="connection.datasource">jdbc/p4fDB</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</property>
<property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="hibernate.cglib.use_reflection_optimizer">false</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.transaction.auto_close_session">true</property>

Here is log:
Hibernate: select hibernate_sequence.nextval from dual|#]
[#|2006-02-27T09:40:51.688+0530|INFO|sun-appserver-pe8.1_02|com.bluecycle.audit.interceptor.HibernateAuditInterceptor|_ThreadID=13;|++INTERCEPTOR onSavecom.bluecycle.autosalvage.entities.FuelType@1fae7b1|#]
[#|2006-02-27T09:40:51.705+0530|INFO|sun-appserver-pe8.1_02|com.bluecycle.audit.interceptor.HibernateAuditInterceptor|_ThreadID=13;|++INTERCEPTOR preFlushjava.util.HashMap$ValueIterator@69fb6d|#]
[#|2006-02-27T09:40:51.710+0530|INFO|sun-appserver-pe8.1_02|javax.enterprise.system.stream.out|_ThreadID=13;|
Hibernate: insert into fuel_types (VERSION, name, creation_date, fuel_type_id) values (?, ?, ?, ?)|#]
[#|2006-02-27T09:40:51.726+0530|INFO|sun-appserver-pe8.1_02|com.bluecycle.audit.interceptor.HibernateAuditInterceptor|_ThreadID=13;|++INTERCEPTOR postFlushjava.util.HashMap$ValueIterator@14ce181|#]
[#|2006-02-27T09:40:51.728+0530|INFO|sun-appserver-pe8.1_02|javax.enterprise.system.stream.out|_ThreadID=13;|
09:40:51,726 DEBUG CacheSynchronization : transaction before completion callback
|#]
[#|2006-02-27T09:40:51.737+0530|INFO|sun-appserver-pe8.1_02|javax.enterprise.system.stream.out|_ThreadID=13;|09:40:51,737 DEBUG CacheSynchronization : automatically flushing session
|#]
[#|2006-02-27T09:40:51.738+0530|INFO|sun-appserver-pe8.1_02|com.bluecycle.audit.interceptor.HibernateAuditInterceptor|_ThreadID=13;|++INTERCEPTOR preFlushjava.util.HashMap$ValueIterator@68ef48|#]
[#|2006-02-27T09:40:51.738+0530|INFO|sun-appserver-pe8.1_02|com.bluecycle.audit.interceptor.HibernateAuditInterceptor|_ThreadID=13;|++INTERCEPTOR postFlushjava.util.HashMap$ValueIterator@1358e57|#]
[#|2006-02-27T09:40:51.740+0530|INFO|sun-appserver-pe8.1_02|javax.enterprise.system.stream.out|_ThreadID=13;|09:40:51,740 DEBUG CacheSynchronization : transaction after completion callback, status: 3
|#]
[#|2006-02-27T09:40:51.741+0530|INFO|sun-appserver-pe8.1_02|javax.enterprise.system.stream.out|_ThreadID=13;|09:40:51,741 DEBUG CacheSynchronization : automatically closing session
|#]


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 12:22 pm 
Newbie

Joined: Tue Jan 25, 2005 3:03 am
Posts: 11
Location: Bangalore, India
This behavior is expected, since you have enabled "hibernate.transaction.flush_before_completion" (which automatically flushes the session before transaction completion) but you are also explicitly calling session.flush() in your code. You should do only one of these. It is highly recommended to let hibernate manage the session lifecycle (flush, close etc.) rather than you doing it yourself in your app code. Infact, only in very infrequent cases would you explicitly need to call session.flush() yourself. Usecases for explicitly calling session.close are even more rare !

In short, just remove the session.flush() call from your code and you will be fine :-).

_________________
Rgds,
Vikas.

"Quidquid latine dictum sit, altum sonatur".
Whatever is said in Latin, sounds profound


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 4:18 pm 
Newbie

Joined: Fri Dec 23, 2005 1:18 pm
Posts: 7
Hi. Try to put "unsaved-value="null"" into the <id> element in the mapping file for your class.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.