-->
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.  [ 3 posts ] 
Author Message
 Post subject: Is posible to generate this Ids?
PostPosted: Thu Jul 22, 2004 3:10 pm 
Newbie

Joined: Mon Nov 17, 2003 11:04 am
Posts: 5
Hello, I have to change the way that I generate the ids and I have to now if Hibernate can help me.

In the past I had to generate the Ids (to record of contracts) in sequence. Then I used MS-SQL with Identity and with the following configuration everything works fine:
Code:
<id
  name="id"
  column="id"
  type="java.lang.Long"
  unsaved-value="null">
  <generator class="native"></generator>
</id>

But now I have a new requirement to generate the Ids, the new id have this form: YYXXXX, where
YY is two digit of the contract year ("04" for 2004) and XXXX is contract sequence number (starts with 0001 for that year).
So 030001 is the first contract of year 2003, 030002 is the second contract of year 2003 and 040001 is the first contract of year 2004.

Is there a way to hibernate help me to generate the sequence of this ids?

Thanks in advance, Claudio.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 23, 2004 9:31 am 
Regular
Regular

Joined: Mon Oct 06, 2003 7:17 am
Posts: 58
Location: Switzerland
Hi Claudio,

you can write your own generator (see Hibernate Reference chapter 5.1.4.1. ).


Reto


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 23, 2004 7:36 pm 
Newbie

Joined: Mon Nov 17, 2003 11:04 am
Posts: 5
Thanks for the response.

I've read that chapter. Then I've written my own generator whit a little change on TableGenerator class.
This is my new configuration:
Code:
<id
  name="id"
  column="id"
  type="java.lang.Long"
  unsaved-value="null">
  <generator class="net.sf.hibernate.id.CustomTableGenerator">
    <param name="table">app_primary_key</param>
    <param name="columnValue">next_id</param>
    <param name="columnKey">key</param> <!-- This is a custom column -->
  </generator>
</id>


Now when I save a new record (new contract) everything works fine. The new contract gets the correct id and the key table (app_primary_key) updates the value.
But I have a problem. If I try to save a new contract that it have a constraint error (for example duplicate value on a unique field) the key table is updated although the new record is not saved.
I prefer that if the contract is not saved the key table manteins the same value, is this possible?

Thanks, Claudio. (apologize for my english)


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