In order to display the ID from the created object on a result web page I need to transfer the ID of newly saved objects to the corresponding original objects. I try the way spring suggest (on its org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener) but is not working, any suggestion?
Code:
protected void entityIsTransient(MergeEvent event, Map copyCache) {
super.entityIsTransient(event, copyCache);
SessionImplementor session = event.getSession();
EntityPersister persister = session.getEntityPersister(event.getEntityName(), event.getEntity());
// Extract id from merged copy (which is currently registered with Session).
Serializable id = persister.getIdentifier(event.getResult(), session.getEntityMode());
// Set id on original object (which remains detached).
persister.setIdentifier(event.getOriginal(), id, session.getEntityMode());
// the variable id is not holding the ID as the right_id column at DB, it holds the object's default value
}
Hibernate version: 3.2.0.cr1
Mapping documents:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Right" table="system_right">
<id name="id" column="right_id">
<generator class="native"/>
</id>
<property unique="true" name="name" length="50"/>
<property name="type"/>
<property name="description"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
tx = hsession.beginTransaction();
Right right = new Right();
...
set values
...
session.merge(right);
// the id still 0
// i also try session.refresh(right); after marge
// and query it by its onw example
Name and version of the database you are using: MySQL Server 5.0 (windows xp)