-->
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.  [ 5 posts ] 
Author Message
 Post subject: Last Register
PostPosted: Tue Nov 04, 2003 2:34 pm 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Hi all,

I have a table that uses sequences (Oracle) in the
column id.
When I insert into database, I need to know this column id,
into session

Code:
   Transaction tx = null;
   Session session = null;

   session = factory.openSession();
   tx = session.beginTransaction();
        
   // this object has an Id that
   // uses sequence
   obj1.setName("blablabla");
     
   session.save(obj1, session);

   // After, I need the Id of obj1, where I will use in another obj
           
   obj2.setValue(1);
   obj2.setAddress("St. 10");

   obj2.setObj1(obj1);

   session.save(obj2, session);



How can I know the last identification (obj1) inserted into database?

Thanks!!!!

[]

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 2:48 pm 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
Quote:
When I insert into database, I need to know this column id,
into session


Are you mapping this particular with hibernate?

Cause if so, it would be already set for you!

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 2:55 pm 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Yes, I am, or I think... :-)

The mapping is the following:

Code:
<hibernate-mapping>
    <class name="Obj1" table="obj1">
   <id name="idObj1" column="id_obj1">
      <generator class="native">
         <param name="sequence">seq_obj1</param>
      </generator>
   </id>
   <property name="name" column="name"/>
   <bag name="Obj2" inverse="true" lazy="true">
      <key column="id_obj1"/>
      <one-to-many class="Obj2"/>
                </bag>
    </class>
</hibernate-mapping>

_________________
Tads


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2003 3:25 pm 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
is the sequence column de one mapped by id?

cause if so, then you look all set to go! It should work. I do this all the time with mysql and MS SQL server and it works fine. After I insert a new object to the DB it gets its id assigned by hibernate.

For this to work properly you must set the dialect property in you hibernate.cfg.xml file (or properties file), in your case the Oracle Dialect.

hope it helps

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2003 12:34 pm 
Senior
Senior

Joined: Wed Sep 24, 2003 3:01 pm
Posts: 158
Location: Bragan�a Paulista - Brasil
Sorry,

I found the problem... It

_________________
Tads


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