-->
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: Using sequence-generated identifier in a composite id.
PostPosted: Wed Dec 14, 2005 11:10 am 
Newbie

Joined: Wed Dec 14, 2005 10:40 am
Posts: 3
Hi all,

I need a helping hand on this one.
Consider this mapping, based on these 2 very classic entities :

Code:
<class name="Order" table="ORDERS" >
    <id name="id" column="ORDER_ID" type="long">
        <generator class="sequence">
       <param name="sequence">ORDER_ID_SEQ</param>
        </generator>
    </id>
    <!-- bidirectional one-to-many Order>Product -->
    <set name="products" inverse="true" cascade="save-update" >
        <key column="ORDER_ID" not-null="true" />
        <one-to-many class="Product" />
    </set>
    ...
</class>
...
<class name="Product" table="PRODUCTS" >
    <composite-id name="id" class="ProductId" >
        <key-property name="orderId" column="ORDER_ID" type="long" />
        <key-property name="productId" column="PRODUCT_ID" type="long" />
    </composite-id>
    <!-- bidirectional one-to-many Order>Product -->
    <many-to-one name="order" column="ORDER_ID" insert="false" update="false" not-null="true" />
    ...
</class>

(following these indications: http://www.hibernate.org/117.html#A34)

But, sadly, I can't get this to work... When I try to save an Order with its linked products (by cascade), Hibernate does not seem to be able to set the correct generated ORDER_ID :
Code:
GRAVE: ORA-01400: cannot insert NULL into ("xx"."PRODUCTS"."ORDER_ID")


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 12:34 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I switched away from using composite ids, but I seem to remember from when I was using them a while back that one of the gotchas of this approach is that you have to set both the order and the orderId, for things to work. Are you setting both, or only the order ( setOrder() ), in your Product object?

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 12:44 pm 
Newbie

Joined: Wed Dec 14, 2005 10:40 am
Posts: 3
Thanks for your answer.
Actually, I'm only setting the Order, and not the OrderId. But even if I wanted to, I could not, since the OrderId is generated by a sequence, and the inserts are cascaded.
Hibernate is /supposed/ to handle this, right ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 1:00 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Oh, I see what you are saying. I'm not sure how Hibernate should handle this. I bet if you changed your key-property to a key-many-to-one, and removed your many-to-one, it would work. But if that's not an option, then I'm not really sure how you can get cascade save to work in this case (not saying it's not possible, I just don't know how).

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 5:18 am 
Newbie

Joined: Wed Dec 14, 2005 10:40 am
Posts: 3
thank you, i'll just try to deal with it - maybe by deactivating the cascade. :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 9:39 am 
Newbie

Joined: Tue Jan 10, 2006 9:22 am
Posts: 6
Hi all,
I am having a problem like this. I would like to generate one of the fields of a composite id primary key.
I have read all the postgres documentation, and I found this email, which seems to solve my problem.
I had one single doubt.
I have a table Product like nathanmoon, but in this case, I would like to generate in sequence, the product column.
Does I have to map it separately from the product table?

Does I have to create an object ProductIdId with this column product long, to the do this sequence generation?


Thanks
Dirceu

_________________
Best regards
Dirceu Semighini Filho


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.