-->
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.  [ 1 post ] 
Author Message
 Post subject: I need to get the ID form a newly persisted object
PostPosted: Fri Oct 06, 2006 3:20 pm 
Newbie

Joined: Fri Oct 06, 2006 2:48 pm
Posts: 2
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)


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.