-->
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.  [ 11 posts ] 
Author Message
 Post subject: generator class="assigned" or native ? or ?
PostPosted: Fri Jan 27, 2006 11:01 am 
Beginner
Beginner

Joined: Thu Apr 14, 2005 11:39 am
Posts: 31
I have two tables A and B. B is child table of A.
A have Primary Key and a seq assigned to it. B have a Primary key and seq assigned to it and alo FK which is the PK in Table A.

For table A I need to insert a PK value So I used <generator class="assigned" />

But for table B System has to assign a uniqID as PK Then what is my generater class value in the mapping file ?

Thanks in advance for the clarification

Sudhakar


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 12:01 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
This is more of a database design question than Hibernate question.
You wrote that table B was a child of table A. I'm assuming you're talking about many-to-one relationship (many B records link to a single A record).
If this is the case, then your problem is how to generate the A key, not the B key, since B simply references A key. Either way, I would suggest using sequence generators (for both tables) if your database supports it. If you don't know what it is, talk to your DBA.
This is example hbm mapping for table A (for Oracle db):

<class name="ClassA" table="TABLE_A" schema="MY_SCHEMA" lazy="true">

<!-- primary key -->
<id name="aId" type="long">
<column name="TABLE_A_ID" precision="10" scale="0" />
<generator class="sequence">
<param name="sequence">seq_table_a_id</param>
</generator>
</id>

<!-- example property -->
<property name="xxx" type="xxx">
<column name="xxx" precision="10" scale="0" />
</property>

<!-- collection of children records -->
<set name="bRecords" inverse="true" lazy="true">
<key>
<column name="TABLE_A_ID" precision="10" scale="0" />
</key>
<one-to-many class="ClassB" />
</set>

</class>
</hibernate-mapping>

"seq_table_a_id" is the name of the sequence generator that will give you new values for table_a_id (this needs to be created in the database).

This is example hbm mapping for table B:

<class name="ClassB" table="TABLE_B" schema="MY_SCHEMA" lazy="true">

<!-- primary key -->
<id name="bId" type="long">
<column name="TABLE_B_ID" precision="10" scale="0" />
<generator class="sequence">
<param name="sequence">seq_table_b_id</param>
</generator>
</id>

<!-- link to parent record (reference) -->
<many-to-one name="aRecord" class="ClassA" fetch="select">
<column name="TABLE_A_ID" precision="10" scale="0" />
</many-to-one>

<!-- example property -->
<property name="yyy" type="string">
<column name="yyy" length="20" />
</property>

</class>
</hibernate-mapping>

HTH,

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 12:04 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
PS. If you really have to insert a value for primary key, for example you have an algorithm that generates those keys for you, then just change generator class to assigned for ClassA. It would still be a good idea to use sequence generators for ClassB.

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 12:47 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Depending on your database there are different generators. increment, sequence, ...
Please look for this in the Reference, Wiki or FAQ. I am sure it is there.
google for hibernate mydatabase generator could also help.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 1:03 pm 
Beginner
Beginner

Joined: Thu Apr 14, 2005 11:39 am
Posts: 31
Thank you so much for your quick repply. I am using Oracle database and both tables have defined a sequence for primary Key.And I came to know I don not insert any value for this primaryky for any of the tables. "bratek" is right and he has given example I think I can just use it as it is without any changes.

correct ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 1:04 pm 
Beginner
Beginner

Joined: Thu Apr 14, 2005 11:39 am
Posts: 31
BTW I am using the middle gen tool to generate mapping files. Can I just use as it is or should I do any modifications for this relation of tables


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 1:38 pm 
Beginner
Beginner

Joined: Mon Jan 23, 2006 12:01 pm
Posts: 46
I would check the id elements of each class. In my case after importing mappings from an existing schema generator property was set by default to assign. You might need to change it to sequence.

Bratek


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 1:44 pm 
Beginner
Beginner

Joined: Thu Apr 14, 2005 11:39 am
Posts: 31
You are right. Middle gen identified generator and showing a dropdown to select sequence for some table and not for other tables even it has defined a sequnce for the primary key. I will have to manually modify those mapping files generator as sequence.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 2:57 pm 
Beginner
Beginner

Joined: Thu Apr 14, 2005 11:39 am
Posts: 31
I am a new user and if I am trying to post new thread, it is displaying a message saying I need atleast 1 credit to post a new topic. How can a new user can get a credit when he does not have a knowledge to answer any hibernate questions.

Thanks


Top
 Profile  
 
 Post subject: Hibernate documentation
PostPosted: Tue Jan 31, 2006 3:01 pm 
Beginner
Beginner

Joined: Thu Apr 14, 2005 11:39 am
Posts: 31
It seems to me I have consumed all my intial 10 credits. Beacuse of this reson I am posting a new question in my old thread. I want to lern about all attributes and their usage in mapping.hbm.xml file. Where can I find?

Example: cascade,lazy, inverse ...etc.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 3:11 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
The Online Reference at
http://www.hibernate.org/hib_docs/v3/re ... ml_single/
has all the things required to start doing work with Hibernate. Hibernate in Action is also a good reference. Go through the online reference (and the tutorial that comes with it). That should be a good start!

Good luck :)


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