-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: How intelligent is Hibernate
PostPosted: Tue Mar 09, 2004 1:44 pm 
Newbie

Joined: Tue Mar 09, 2004 10:51 am
Posts: 16
Hi there,

Imagine the following situation:

I have an application that deals with user orders. An order can have one or more orderlines. The orderlines are the children of the order which is the parent. An orderline has a relationship with a ticket. A ticket (parent) can be connected to more than one orderline (children).

Order --1--------N--< Orderline >--N--------1-- Ticket

Question:

I create a new ticket object, assign this ticket object to one or more orderlines and finally the orderlines are connected to the order object. Is it possible to insert all the records with a simple 'save(order)'? Is Hibernate intelligent enough to see it must first insert the Ticket, then the Order and finally the Orderline object?

Erwin.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 2:12 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
As far as I know, you can cascade a save along a one-to-many association in the one-to-many direction (i.e. from 1 to N) but not along the many-to-one direction (i.e. from N to 1). So in your case, you would need to save the ticket and the order, but the orderlines could get saved via cascade.

But I may be wrong :-)
Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 2:15 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
<!ELEMENT many-to-one (meta*,column*)>
<!ATTLIST many-to-one cascade (none|all|save-update|delete) #IMPLIED> <!-- default: none -->

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 2:22 pm 
Newbie

Joined: Tue Mar 09, 2004 10:51 am
Posts: 16
Quote:
<!ELEMENT many-to-one (meta*,column*)>
<!ATTLIST many-to-one cascade (none|all|save-update|delete) #IMPLIED> <!-- default: none -->


I guess that it does not work in the way I want. I must first save the ticket than the order and at last the orderline. Am I right?

Erwin.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 2:27 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
No. What Christian is saying (in his endearingly cryptic way :-) is that you CAN cascade along a many-to-one relationship.

Basically, I think that you can probably do what you want. But you should try it yourself and see :-) Christian, can you cascade in BOTH directions of a bidirectional relationship? And if not, then in Erwin's case, should he cascade from Order to Orderline to Ticket?

(My guess is yes: Erwin, if I were you, I would first try cascade="all-delete-orphan" on the Order-to-Orderline association, and cascade="save-update" on the OrderLine-to-Ticket association. And then I think Hibernate will probably do the right thing.)

Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 2:30 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Guys, try it. It will work as you expect it to work.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 2:42 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
the question was : "How intelligent is Hibernate"
i think i have the answer: "extremely intelligent" or "as intelligent as the way you're writing your mapping file...."


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 6:14 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
Hibernate is as intelligent as the user who is using it...obviously the range of intelligence varies greatly.

;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 6:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i only wanted to say that the tool is not going to do all the work ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 8:39 am 
Newbie

Joined: Tue Mar 09, 2004 10:51 am
Posts: 16
Quote:
i only wanted to say that the tool is not going to do all the work ;)


What do you mean with that?
According to the previous answers Hibernate is intelligent enought to see when objects must be inserted and in which order (parent first then child)!

It is still not working, what kind of work must I do by myself?

Erwin.


Top
 Profile  
 
 Post subject: by the way...
PostPosted: Wed Mar 10, 2004 9:41 am 
Newbie

Joined: Fri Dec 05, 2003 2:12 pm
Posts: 2
Location: Montpellier France
Does anyone know if it is possible to to a lazy many-to-one association, I bet it is.


Top
 Profile  
 
 Post subject: Re: by the way...
PostPosted: Wed Mar 10, 2004 10:37 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
cedric dupont wrote:
Does anyone know if it is possible to to a lazy many-to-one association, I bet it is.


it is, just use outer-join = false on the many-to-one node, by default:
outer-join = true (always load the to-one object) and lazy = true (never load the to-many list)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 1:21 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
erbruijn wrote:
Quote:
i only wanted to say that the tool is not going to do all the work ;)

What do you mean with that?
According to the previous answers Hibernate is intelligent enought to see when objects must be inserted and in which order (parent first then child)!

It is still not working, what kind of work must I do by myself?

I think he just meant that you must understand exactly what cascades you want to implement, and which associations should be cascaded, and which direction those cascades should happen on.

Hibernate will do all the work for you *if* you tell it exactly what work you want it to do. Figuring out what you want, and describing it in your mapping, is *your* job :-)

Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 1:23 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
yes Rob, that was exactly what i wanted to say lol


Top
 Profile  
 
 Post subject: Re: by the way...
PostPosted: Wed Mar 10, 2004 5:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
delpouve wrote:
cedric dupont wrote:
Does anyone know if it is possible to to a lazy many-to-one association, I bet it is.


it is, just use outer-join = false on the many-to-one node, by default:
outer-join = true (always load the to-one object) and lazy = true (never load the to-many list)

Actually the common way it to proxy the class in the one side. Default for outer-join is auto.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.