-->
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.  [ 2 posts ] 
Author Message
 Post subject: Refresh After Insert
PostPosted: Fri Jun 17, 2005 9:56 am 
Newbie

Joined: Fri Jun 17, 2005 9:44 am
Posts: 1
Hibernate version: 3.0.5

In the Hibernate application that we're working on, we've got a generic saveObject method in one of our DAOs. The intent of this method is to insert the object if it is transient or "update" it if it is detached. The database we're using has a set of triggers that execute before inserts and updates that set some auditing fields such as created_date and modified_date. Since these fields are set by a trigger, Hibernate doesn't know that they've been modified and the updated values are not populated/updated in my objects. I would like for the saveObject method to reload the data from the database after inserts/updates so that these values show up in the objects that we're saved.

Code:
    public void saveObject(final Object object) {
        getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                // we only want to call save for transient or detached objects
                if (!session.contains(object)) {
                    session.saveOrUpdate(object);
                }
                session.flush();
                session.refresh(object);
                return null;
            }
        });
    }


The idea is that a flush is forced after the save, and then a call to refresh forces Hibernate to reload the object's state from the database. This code works fine for persistent objects. However, for transient objects that need to be inserted, I'm getting an UnresolvableObjectException on the call to session.refresh(object). This led me to believe that the insert wasn't happening, but I've enabled P6Spy and verified that there is a SQL INSERT statement, followed by a SQL SELECT (some fields masked):

1118933686257|10|0|statement|insert into STATUS (NAME, CREATED_BY, MODIFIED_BY, STATUS_ID) values (?, ?, ?, ?)|insert into STATUS (NAME, CREATED_BY, MODIFIED_BY, STATUS_ID) values ('Insert Test', '*******', '*******', 292)

1118933686267|10|0|statement|select status0_.STATUS_ID as STATUS1_0_, status0_.NAME as NAME35_0_, status0_.CREATED_DATE as CREATED3_35_0_, status0_.CREATED_BY as CREATED4_35_0_, status0_.MODIFIED_BY as MODIFIED5_35_0_, status0_.MODIFIED_DATE as MODIFIED6_35_0_ from STATUS status0_ where status0_.STATUS_ID=?|select status0_.STATUS_ID as STATUS1_0_, status0_.NAME as NAME35_0_, status0_.CREATED_DATE as CREATED3_35_0_, status0_.CREATED_BY as CREATED4_35_0_, status0_.MODIFIED_BY as MODIFIED5_35_0_, status0_.MODIFIED_DATE as MODIFIED6_35_0_ from STATUS status0_ where status0_.STATUS_ID=292

Note that we're using Spring's support for declarative transactions, and the transactionAttributes relevant to this method are:
Code:
        <prop key="save*">PROPAGATION_REQUIRED</prop>


Does anyone have any insight into this problem? The relevant portion of the exception stack trace is below:

Code:
org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [com.fmr.eaccess.domain.eaccess.Status#298]
   at org.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:42)
   at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:105)
   at org.hibernate.impl.SessionImpl.refresh(SessionImpl.java:679)
   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:585)
   at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1004)
   at $Proxy2.refresh(Unknown Source)
   at com.fmr.eaccess.dao.hibernate.HibernateSystemAdminDao$2.doInHibernate(HibernateSystemAdminDao.java:109)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:288)
   at com.fmr.eaccess.dao.hibernate.HibernateSystemAdminDao.saveObject(HibernateSystemAdminDao.java:92)
   at com.fmr.eaccess.service.SystemAdminServiceImpl.saveObject(SystemAdminServiceImpl.java:71)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 1:25 pm 
Newbie

Joined: Fri Aug 25, 2006 1:22 pm
Posts: 1
Has anyone else faced this issue? I am in the same boat as the original poster.

If anyone has any recommendations, please advise.

Thanks in advance for your help,
Richard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.