-->
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.  [ 6 posts ] 
Author Message
 Post subject: ID generation...
PostPosted: Wed Dec 08, 2004 3:38 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
I have this kind of mapping....

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.test.hibernate.AttachFile" table="AttachFile">
       <id name="fileId" column="fileId">
          <generator class="sequence">
             <param name="sequence">SEQ_AttachFile</param>
          </generator>
       </id>
        <property name="fileName" column="fileName"/>
        <property name="filePath" column="filepath"/>       
    </class>
</hibernate-mapping>


and i have a trigger in my ORACLE database like:
Code:
create trigger attachFile_trigger
before insert on AttachFile
for each row
begin
select SEQ_AttachFile.nextval into :new.fileId from dual;
end;


as you know, the ID will be generated from a sequence.
however, also in my database (ORACLE), i also define a TRIGGER that will get the next value of the sequence.

this is the reason why everytime i use may application and do insert a row on this table, the SEQUENCE will be incremented twice!. That is:
1. hibernate get the next value from SEQUENCE.
2. upon inserting the row, the trigger kicks off and take the next value from the SEQUENCE.

my question is, what generator class should I use to make the SEQUENCE gets incremented only once?.

or how will I redefine my mapping on this case?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: ID generation...
PostPosted: Wed Dec 08, 2004 3:45 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
atn473 wrote:
I have this kind of mapping....

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class name="com.test.hibernate.AttachFile" table="AttachFile">
       <id name="fileId" column="fileId">
          <generator class="sequence">
             <param name="sequence">SEQ_AttachFile</param>
          </generator>
       </id>
        <property name="fileName" column="fileName"/>
        <property name="filePath" column="filepath"/>       
    </class>
</hibernate-mapping>


and i have a trigger in my ORACLE database like:
Code:
create trigger attachFile_trigger
before insert on AttachFile
for each row
begin
select SEQ_AttachFile.nextval into :new.fileId from dual;
end;


as you know, the ID will be generated from a sequence.
however, also in my database (ORACLE), i also define a TRIGGER that will get the next value of the sequence.

this is the reason why everytime i use may application and do insert a row on this table, the SEQUENCE will be incremented twice!. That is:
1. hibernate get the next value from SEQUENCE.
2. upon inserting the row, the trigger kicks off and take the next value from the SEQUENCE.

my question is, what generator class should I use to make the SEQUENCE gets incremented only once?.



You can do lots of things. the easiest is to use the native id generator, and create a sequence in the DB named hibernate_sequence (all tables share this). Now if you have external accesses to the table, use that sequence in the trigger.

You dont have to do that though, if you need the trigger, before incrementing, see if it is null. If not, do not assign.

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 08, 2004 6:51 am 
Newbie

Joined: Fri Nov 19, 2004 6:35 am
Posts: 19
that's CLEVER!. thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 12:31 pm 
Newbie

Joined: Tue Oct 31, 2006 8:42 am
Posts: 12
Think this is a similar problem to what I am experiencing - which value are you testing for null?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 2:52 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
in the trigger, see if the id field is null, if so, use the sequence, if not, leave it alone, it was already retrieved via sequence from hibernate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 4:16 am 
Newbie

Joined: Tue Oct 31, 2006 8:42 am
Posts: 12
Sorry shopuld have replied yesterday afternoon - I put the not null test in the trigger and everything works a treat!!

Thanks for the info - much appreciated!


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