-->
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.  [ 8 posts ] 
Author Message
 Post subject: [Newbie] Oracle sequences and generators
PostPosted: Thu Oct 30, 2003 12:58 pm 
Newbie

Joined: Thu Oct 30, 2003 5:00 am
Posts: 3
Location: Spain
Hi all,

I'm a bit lost with the different types of generators available with Hibernate. Basically, I have an Oracle DB which has sequences for (most of) its tables. Now, I gathered from other posts that the way to use those sequences is to specify the following in my class.hbm.xml :

Code:
<id column="IDSTRUCT" name="idstruct" type="java.lang.Long">
  <generator class="sequence">
    <param name="sequence">my_schema.my_seq_name</param>
  </generator>
</id>

However, it seems to me that I could also use class="assigned" and use the

Code:
OracleDialect.getSequenceNextValString(my_seq_name)

method to achieve the same result. My question, therefore, is two-fold :
1. Will those two methods actually do the same thing?
2. If so, which should I use? I'd be very tempted to go with the first one, if not for the fact that I got a sample .hbm.xml mapping file from another programmer here where he used class "assigned". Unfortunately, that's all he sent me (I don't have the underlying code) and he has so far failed to answer my e-mails, so I don't know the rest of the story.

Thanks in advance for shedding some light on this.

Laurent


Top
 Profile  
 
 Post subject: Re: [Newbie] Oracle sequences and generators
PostPosted: Thu Oct 30, 2003 1:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Laurent Leconte wrote:
2. If so, which should I use?

Use the first one (sequence) it is way easier for your developpers:
they do not need to bother with id settings,
they can use the saveOrUpdate semantic,
if you decide to change to another generator, only mapping files will be changed
if you want to change sequence (name, share sequences etc), only mapping files will be changed.

etc...

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: [Newbie] Oracle sequences and generators
PostPosted: Fri Oct 31, 2003 5:49 am 
Newbie

Joined: Thu Oct 30, 2003 5:00 am
Posts: 3
Location: Spain
epbernard wrote:
Use the first one (sequence) it is way easier for your developpers:


That's what I plan on doing. However I have one table with two columns, one of them the PK, the other one containing a copy of the PK (in other words, two identical columns, one of which is marked as PK). I don't see how to create a new element in this DB, because if I use the "sequence" class there's no way to know the new id before saving the element. Any idea how to do this without doing a save/load/update every time? (perhaps some kind of link between the two columns in the mapping file?)

Laurent


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 9:02 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
Obj obj = new Obj();
// fill obj except fancy duplicated property
session.save(obj); //get the next sequence nbr from Oracle and fill obj.setId()
obj.setFancyProperty(obj.getId());
obj.flush(); //do 1 DB insert

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 11:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Nah, that doesn't work, actually. The INSERT always uses a snapshot of the state at save().

There are some reasons for this decision, though it is not always the best.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 31, 2003 12:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you doesn't have null constraints on your copy column, my method will work and do:

insert ... (with copyColumn to null)
update ... (with copyColumn to the appropriate value)

If you have constraints, then I use the assigned strategy.

Just curious, why do you store information twice in the same row ? Are you paid to DB size ;-) ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2003 5:39 am 
Newbie

Joined: Thu Oct 30, 2003 5:00 am
Posts: 3
Location: Spain
epbernard wrote:
Just curious, why do you store information twice in the same row ? Are you paid to DB size ;-) ?


I wish I knew... The answer would be "don't ask me, I'm just an intern". Basically, there are two columns with very similar names (something like ID_METODO and IDMET), one of them is the PK and the other one isn't used as far as I can tell. No one's bothered explaining it to me, and until it crashes the whole system I'll assume they always have the same value.

On a completely unrelated point: is there some sort of XML parser for Hibernate mapping files? (sort of like struts-console for Strus config files)

Laurent


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 03, 2003 5:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
On a completely unrelated point: is there some sort of XML parser for Hibernate mapping files? (sort of like struts-console for Strus config files)


Max has implemented something in Hibern8IDE, but it does not have the scope of some of the struts GUIs.

If you want to /write/ one, I know some people would use it. Consider integrating it in Hibernator (if you like eclipse) or Hibern8IDE (if you like Swing).


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