-->
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.  [ 4 posts ] 
Author Message
 Post subject: Confusion mapping IDs
PostPosted: Mon Dec 29, 2003 4:00 pm 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
I'm using postgreSQL and mapping a simple database table. The primary ID of my table is a sequence. When adding rows to the table manually, I never specify the ID - it is autogenerated by the database because the sequence is specified as the default value.

After reading the Hibernate manual, I am unsure how to model this behavior in the mapping. The closest I can see is something like this:

<id name="id" type="int" column="id" unsaved-value="0">
<generator class="sequence"/>
<param name="sequence">mmseq</param>
</generator>
</id>

but the docs say using this method requires two SQL statements to insert a new row into the database: one to get the sequence value, and the other to do the insert. That seems wasteful to me. I've never used two statements with plain old JDBC - just don't specify the ID and the db generates it.

Can hibernate do that?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2003 4:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
How did you get the id for your object without querying for it ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2003 4:21 pm 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
Hmmm, your question makes me realize my head isn't completely wrapped around the Hibernate paradigm yet.

I was thinking along the lines that all I want to do is add a new row to the database and be done with it. In this particular case, I probably won't refer back to the row I just added for quite awhile. If I was doing this the old JDBC way, it would be a simple SQL insert statment and I would not specify a value for the ID column.

But I'm now guessing Hibernate can't do that because it wants to associated my persistant java bean with the row I just added to the DB. And to do that, it needs to set the bean with the ID of row just added. Doing that with a sequence requires two queries.

Do I have any of that right?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2003 4:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Exactly, for example after saving the object you could change some of its properties, and hibernate must know which row to update ...


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