-->
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.  [ 13 posts ] 
Author Message
 Post subject: Getting sequence.nextval from oracle database
PostPosted: Thu Mar 09, 2006 1:56 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
I am trying to execute this query using hibernate "select test_seq.nextval from dual" but am not sure how to do it. I need to get a new sequenceId for a class before I can insert it into the database. I cannot generate using <generator class="hilo"></generator> because the id is for a child and am trying to save it by putting it in a set of its parent.

P p = new P

C c1 = new C
c1.setPId = p.getId
c1.setSequenceId = need to get this "select test_seq.nextval from dual"

C c2 = new C
c2.setPId = p.getId
c2.setSequenceId = need to get this "select test_seq.nextval from dual"

p.addChild( c1 )
p.addChild( c2 )

Session.save( p ) - this will save the parent and children.

I need sequenceId when adding it to the parent, to differ the children from each other.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 2:04 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Hey, I guess it is a fairly bad idea to set the sequence number yourself in the db-layer.

Why don't you just save your Child before adding it to the parent collection?
Code:
P p1 = new P();
session.save(p1);
C c1 = new C();

c1.set...(p1.get...);
session.save(c1);


simon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 2:17 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
There is a foreign key between the parent and child, so the parent must be saved before the child for this link to work correctly


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 2:24 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Grogan wrote:
There is a foreign key between the parent and child, so the parent must be saved before the child for this link to work correctly


nope. Hibernate can accomodate this situation. The child class just needs to have an association pointing back to the parent and the columns it needs from the parent. Even if the values are generated in the database when the parent is saved.


For an example, this could be used in the composite-key of a child
Code:
    <key-many-to-one
                    
           name="parentClass"
           class="some.package.ParentClass"
           column="my_seq_no" 
      
    >

    </key-many-to-one>

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 2:24 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Grogan wrote:
There is a foreign key between the parent and child, so the parent must be saved before the child for this link to work correctly


In my first post I do save the parent first and the id is available via the specified getter method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 2:42 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
Here are my xml mappings Company is the parent and product and employee are children. I have a set for each child in the company.java class. After I got the sequenceId from the database, I thought I could just make one save method call to save everything to the database.


<class name="Company" table="Company">
<id name="companyId" column="companyId" type="long"
unsaved-value="null">
<generator class="hilo"></generator>
</id>
<property name="name" column="companyName" type="string"
length="15" not-null="true"/>
<property name="address" column="address" type="string"
length="25" not-null="true"/>

<set name="products" cascade="all" inverse="true">
<key column="companyId"/>
<one-to-many class="Product"/>
</set>
<set name="employees" cascade="all" inverse="true">
<key column="companyId"/>
<one-to-many class="Employee"/>
</set>
</class>

------------
<class name="Product" table="Product">
<composite-id name="ProductPK" class="ProductPK">
<key-property name="productId" type="long" column="productId"/>
</composite-id>
<property name="productName" column="productName" type="string" length="15" not-null="false"/>
<property name="productPrice" column="productPrice" type="double" not-null="false"/>
<property name="productDescription" column="productDescription" type="string"
length="50" not-null="false"/>
<many-to-one name="company" class="Company" column="companyId" not-null="true"/>
</class>


---------------

<class name="Employee" table="Employee">
<composite-id name="EmployeePK" class="EmployeePK">
<key-property name="ssn" type="integer" column="ssn"/>
<key-property name="firstName" column="firstName" type="string" length="15"/>
</composite-id>

<property name="lastName" column="lastName" type="string"
length="15" not-null="true"/>
<property name="salary" column="salary" type="double" not-null="true"/>

<many-to-one name="company" class="Company" column="companyId" not-null="true"/>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 2:49 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
those mappings look fine.

Just create the product and employee, make sure to set the company for each of them like product.setCompany(parentCompany), add them to the set, then save the parent

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 3:27 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
I changed my product mapping to this, to get a generated ID from the db. When I did this, hibernate will try and update instead of inserting the data into the database. Not sure why on this.

<class name="Product" table="Product">
<id name="productId" column="productId" type="long"
unsaved-value="null">
<generator class="hilo"></generator>
</id>
<property name="productName" column="productName" type="string" length="15" not-null="false"/>
<property name="productPrice" column="productPrice" type="double" not-null="false"/>
<property name="productDescription" column="productDescription" type="string"
length="50" not-null="false"/>
<many-to-one name="company" class="Company" column="companyId" not-null="true"/>
</class>





Hibernate: select employee_.ssn, employee_.firstName, employee_.lastName as lastName1_, employee_.salary as salary1_, employee_.companyId as companyId1_ from Employee employee_ where employee_.ssn=? and employee_.firstName=?
Hibernate: select employee_.ssn, employee_.firstName, employee_.lastName as lastName1_, employee_.salary as salary1_, employee_.companyId as companyId1_ from Employee employee_ where employee_.ssn=? and employee_.firstName=?
Hibernate: insert into Company (companyName, address, companyId) values (?, ?, ?)
Hibernate: insert into Employee (lastName, salary, companyId, ssn, firstName) values (?, ?, ?, ?, ?)
Hibernate: insert into Employee (lastName, salary, companyId, ssn, firstName) values (?, ?, ?, ?, ?)
Hibernate: insert into Company (companyName, address, companyId) values (?, ?, ?)
Hibernate: insert into Company (companyName, address, companyId) values (?, ?, ?)
Hibernate: update Product set productName=?, productPrice=?, productDescription=?, companyId=? where productId=?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 10:38 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
this is different from what you had above, did you mean this?

edit: by different I mean that product now has a generated id

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 10:52 am 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
Yes,

I changed it to see if I could get an auto generated id. I want the db to create the id for the product if possible.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 3:59 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I'm not familiar with the hilo generator, but do you need to specify a name for the generator?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 5:28 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 2:41 pm
Posts: 29
In your original post, you showed code like:
P p = new P

C c1 = new C
c1.setPId = p.getId
p.addChild(c1);

but the "setPId" line should be
c1.setParent(p);

...unless addchild() does that.
There shouldn't even be a "setPId" method.


Top
 Profile  
 
 Post subject: Another approach
PostPosted: Fri Mar 24, 2006 3:39 pm 
Newbie

Joined: Fri Mar 24, 2006 3:35 pm
Posts: 1
Maybe try using the native generator and map your classes following the suggestions in the Inheritance portion of the documentation. This lets hibernate worry about the relationships and the ids.


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