-->
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.  [ 2 posts ] 
Author Message
 Post subject: Can I use identity columns in element sets?
PostPosted: Mon Dec 19, 2005 8:58 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I want to set up a simple collection (set probably, but a list or array will do fine) of integers, where the integer is allocated by the DB in a strictly incrementing way. That is, the table implementing the set will have only two columns: one being the foreign key to the containing table, and the other is an identity column (I'm using SQLServer).

Is there any way to tell hibernate that the value being added is to be generated by the DB, instead of added by the user? If <set> supported sql-insert then I could do it that way.


Top
 Profile  
 
 Post subject: Problem solved.
PostPosted: Mon Dec 19, 2005 10:44 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Abusing the hard-coded positions in sql-insert, I gave this a go and it does exactly what I need:

Code:
<set name="Incrementing" table="IncrValues"  cascade="all">
  <key column="SetID"/>
  <element column="ValIncr" type="integer"/>
  <!-- The order and position of the ?s is vital (SetID, ValIncr),
    but I want to drop the ValIncr, as that is generated by the DB. -->
  <sql-insert>
    INSERT INTO IncrValues (SetID) VALUES ( ? ) -- ?
  </sql-insert>
</set>


Not especially elegant, but easily understood, I hope. The -- SQL comment causes the ValIncr set by the user to disappear. When I want a new value in the set, I just call

Code:
obj.getIncrementing().add(0);
session.flush();
session().refresh(obj);


and I have my new ValIncr. Now.. can I award myself a credit for solving my own problem?


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