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.


    public void saveObject(final Object object) {
        getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                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:

        <prop key="save*">PROPAGATION_REQUIRED</prop>


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


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)


-- 
Jason Voegele
"There is an essential core at the center of each man and woman that
remains unaltered no matter how life's externals may be transformed
or recombined. But it's smaller than we think."
    -- Gene Wolfe, The Book of the Long Sun



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
hibernate-devel mailing list
hibernate-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to