-->
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.  [ 10 posts ] 
Author Message
 Post subject: Unidirectional composition
PostPosted: Wed Jan 26, 2005 8:37 am 
Beginner
Beginner

Joined: Thu Dec 16, 2004 11:54 am
Posts: 26
Location: Brussels, Belgium
Hi,
I've got a probably real stupid problem here, but I just can't help it for now, so I'll ask for help:

I have 2 classes: Order and OrderLine, the last composing the first.
When I try to save an order, when the SQL INSERTs get executed for the OrderLines, they don't include the id_order field.
Is that because there is no navigability from OrderLine to Order?
I guess there is surely a way to work around that... But I just didn't found it yet...
Hopes someone takes the time to lead a Hibernate Newbie by the hand.
Thanks
Alex

Hibernate version:
2.1.7c

Mapping documents:
for Order
Code:
    <class name="Order" table="orders">
        <cache usage="read-write"/>
        <id name="id" type="long" column="id">
            <generator class="sequence">
                <param name="sequence">orders_id_seq</param>
            </generator>
        </id>
        <many-to-one name="customer" column="id_user" class="User" not-null="true" />
        <many-to-one name="deliveryAddress" class="Address" column="id_address_delivery"
            unique="true" cascade="save-update" not-null="true" />
        <property name="creationDate" column="ts" type="date" not-null="true" insert="false" />
        <property name="shippingCost" column="shipping" type="integer"/>
        <property name="payment" column="payment" type="string" length="16"/>
        <property name="status" column="status" type="char"/>
        <set name="lines" table="order_lines" lazy="false" cascade="all" batch-size="10" >
            <cache usage="read-write"/>
            <key column="id_order"/>
            <one-to-many class="OrderLine"/>
        </set>
    </class>

and for OrderLine
Code:
    <class name="OrderLine" table="order_lines">
        <cache usage="read-write"/>
        <id name="id" type="long" column="id">
            <generator class="sequence">
                <param name="sequence">order_lines_id_seq</param>
            </generator>
        </id>
        <many-to-one name="referenceProduct" column="id_product" class="be.barracuda.model.products.Product" not-null="true" />
        <many-to-one name="referenceOption" column="id_option" class="be.barracuda.model.products.Option" not-null="true" />
        <property name="quantity" column="quantity" type="int" not-null="false"/>
        <property name="price" column="price" type="int" not-null="false"/>
    </class>


The generated SQL (show_sql=true):
Code:
Hibernate: insert into order_lines (id_product, id_option, quantity, price, id) values (?, ?, ?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 9:22 am 
Beginner
Beginner

Joined: Thu Dec 16, 2004 11:54 am
Posts: 26
Location: Brussels, Belgium
I finally found this:
Quote:
Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, Hibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter.

from http://www.hibernate.org/hib_docs/reference/en/html/collections.html

So I removed the NOT NULL constraint on my table and it fixed the problem...

I also noticed that I had another mapping problem that actually occured before the previously stated problem...

So it seems my problem is fixed... But is this kind of relation still handled the same in Hibernate3?
While I should probably rather stfu and be thankful for this marvelous ORM, I still hear Gavin stating that Hibernate got as far as it could, and is the best ORM tool out there. While I agree on point 2 (well I use it, ain't I?), point 1 here fails to me somehow... Or doesn't it? Having to alter my code to a bidirectional association (in case of a composition on top of that) is somewhat intrusiv, or not?
Well anyhow, it works, still would enjoy comments...
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 9:26 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The problem you are encountering is an FAQ, usually asked by people who still have a fuzzy picture about object and data models and how they connect. A bidirectional association is the natural and correct "solution" for your problem. I've explained this again in HiA.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 9:43 am 
Beginner
Beginner

Joined: Thu Dec 16, 2004 11:54 am
Posts: 26
Location: Brussels, Belgium
christian wrote:
the natural and correct "solution" for your problem.

Hmmm...
On a pure OO basis, I don't really see why a bidirectional association is natural on a composition (since one object just can't exist without the other), while I don't see it more or less correct than unidirectionnal.
While not claiming that your statement is hence false, could you please explain a bit futher why a unidirectional "solution" isn't correct in your point of view...
Thanks
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 10:01 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If done so many times, please search.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 10:14 am 
Beginner
Beginner

Joined: Thu Dec 16, 2004 11:54 am
Posts: 26
Location: Brussels, Belgium
Heh! Didn't even understood your reply...
... But I'll search for reason why Composition should be bidirectional associations rather than unidirectional.
For now I just see the "sync" problem on bidirectional ...
Hmmm, I must be too stupid maybe!
But what is "done so many times"? To have a uni- rather than bidirectional association on composition? Do I get you right?
Any how, I'm on my search for knowledge!
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 10:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Ok, once more: A NOT NULL constraint is, as the name implies a constraint derived from your business rules. The rule for a NOT NULL usually is: "This entity has to have this attribute."

Let's translate database entities to entities in your object model: Some might be first class entities, just like in the database, some might end up being dependent value types, like your component. Your business rule still applies: "This thing needs this property." Easy? Yes.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 10:30 am 
Beginner
Beginner

Joined: Thu Dec 16, 2004 11:54 am
Posts: 26
Location: Brussels, Belgium
Easy, ok... and I got that far...
But, and I guess I'm wrong, we can then never dream of a model that just doesn't care about the DB and what ever happens to (or not) these objects.
While, again, I should probably not complain, since we are really far from what we had with Entity Beans CMP or even BMP, and that, I guess thanks to Gavin, Hibernate and you, we are going to in 3.0...
So the answer is I should definitively stfu and simply go with "you thought of it". What is really an answer I can even take!
I really think I am going down the wrong way here... I HAVE TO, as you stated, and always will have to take RBDMS in account when designing!
Maybe I count to much on UML to get my model done, and then get match that model to some tables, somehow...
Still thanks for the reply, even we got each other wrong at some point, since I was really speaking OOP, not taking the RDBMS part in account, which, as you saw, probably is what makes me a not too good programmer...
Thanks,
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 10:37 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Uhm, it helps if you repeat this every day when you wake up and before you even start writing "object-oriented" code (whatever that means): "My Java application is irrelevant, the data and the database will live 20 years longer."


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 26, 2005 10:47 am 
Beginner
Beginner

Joined: Thu Dec 16, 2004 11:54 am
Posts: 26
Location: Brussels, Belgium
christian wrote:
"My Java application is irrelevant, the data and the database will live 20 years longer."


He! Not only that but I also have to admit that there is probably a somewhat deeper problem with my design (which ever DB or Model).
Since the NOT NULL constraint (DB), states that whatever OrderLine will always compose (Model) some Order, but over time could be reference something else too... So that, by then, my NOT NULL constraint should then be removed and so could my bidirectional relation too then. But in the meantime to ensure consistency over these "20 years", I should probably go for "useless" bidirectionality in my model.
Which could leave me another 20 years to find some use for that persistent class!
Thanks a lot...
I may be a pain in the ass, but I always find it useful to talk about these issues, makes thing clearer in my mind, than to go for assertions as stated in TFM that we should all read!
So again thanks for your patience that, I guess, almost got to an end...
Alex


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