Ok, heres my code as it is (somewhat simplified for this post):
Code:
// Uses Spring framework class HibernateDaoSupport
class MyDao extends HibernateDaoSupport {
public ObjectA loadById(String id) {
return getHibernateTemplate().load(ObjectA.class, id);
}
public void update(ObjectA myObject) {
getHibernateTemplate().update(myObject);
}
}
Code:
// Controller class does special thing for ObjectA in the object structure
class ObjectAController {
private MyDao myDao;
public ObjectA loadById(String id) {
return myDao.loadById(id);
}
public void update(ObjectA myObject) {
myDao.update(myObject);
}
}
Code:
// Service object uses Spring framework transaction management
class MyObjectAService {
private PlatformTransactionManager myTransactionManager;
private ObjectAController myController;
public ObjectA load(String id) {
return myController.loadById(id);
}
public ObjectA persistChanges(final ObjectA myObject) {
TransactionTemplate tt = new TransactionTemplate(
this.myTransactionManager);
return tt.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
myController.update(myObject);
return myController.loadById(myObject.getId());
}
});
}
}
Code:
class Client {
public static void main(String[] args) {
MyObjectService service = new MyObjectService();
// The object we're loading exists in the db
ObjectA oldA = service.load("someId");
oldA.setProperty("newProperty");
ObjectA newA = service.persistChanges(oldA);
// newA have the property set to "newProperty",
// but it's not persisted to db
}
}
ObjectA mapping:
Code:
<hibernate-mapping default-cascade="all">
<class name="ObjectA" table="A" dynamic-update="true" dynamic-insert="true">
<jcs-cache usage="read-write"/>
// Define id
// Define properties
<set name="BObjects" inverse="true" cascade="all">
<jcs-cache usage="read-write"/>
<key column="object_a_id"/>
<one-to-many class="ObjectB"/>
</set>
</class>
</hibernate-mapping>
ObjectB mapping:
Code:
<hibernate-mapping default-cascade="all">
<class name="ObjectB" table="B" dynamic-update="true" dynamic-insert="true">
<jcs-cache usage="read-write"/>
// Define id
// Define properties
<many-to-one name="objectA" column="object_a_id" not-null="true" cascade="all"/>
</class>
</hibernate-mapping>
JCS config file
Code:
jcs.default=
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=2000
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
Database does three updates when I call persistChanges():
Code:
UPDATE objectA // With new values set
UPDATE objectB
UPDATE objectA // With OLD values
It isn't more than that! Cannot se what's wrong with that. All I'm doing is to delegate a bunch of calls to different object within a Spring transaction managed method.
Regards, Andreas