-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to map the latest child of a @OneToMany association
PostPosted: Thu May 25, 2017 2:00 am 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
I have a 3 tables with the current relationships

Event
Project
Task

Event to Project is 1:1 Uni Directional currently
Project to Task is 1:Many Uni Directional currently

I have realised that because I want to "navigate" up the relationship from task back to Event I need to make the relationships Bi-Directional.

Is there any reason I shouldn't use a join table (as opposed to recording the foreign keys on each respective table?

Second question:

I think I will need to change Event -> Project from 1:1 to Many:1, but if I want to retain the ability to navigate up the relationship from task to Event - which event should the system "choose" as it will have many to pick from?

I would like to use the most recent Event to de displayed to use, so I assume the best way to do this is to put a creationDate on event and then the system can choose the newest?

THoughts?

Thanks

-AL


Top
 Profile  
 
 Post subject: Re: Relationship setup
PostPosted: Thu May 25, 2017 3:32 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
Is there any reason I shouldn't use a join table (as opposed to recording the foreign keys on each respective table?


Join tables are suitable for many-to-many associations, not for one-to-many associations which only require a single FK on the client-side. Check out this article for more details.

[url]I would like to use the most recent Event to de displayed to use, so I assume the best way to do this is to put a creationDate on event and then the system can choose the newest?[/url]

You can even map the latest child of a one-to-many association using @JoinFormula. Check out this article for more details.


Top
 Profile  
 
 Post subject: Re: Relationship setup
PostPosted: Thu May 25, 2017 4:22 am 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
vlad wrote:
Quote:
Is there any reason I shouldn't use a join table (as opposed to recording the foreign keys on each respective table?


Join tables are suitable for many-to-many associations, not for one-to-many associations which only require a single FK on the client-side. Check out this article for more details.


So if I understand that article correctly, reading the last heading in the article "Just @ManyToOne" then :

Many (Event) to One (Project) is best done by @ManyToOne on the child side. How do you determine which is the child? (I'm guessing it has to be the many (Event)).

Then the article goes on to say in order to make it bidrectional and be able to get the Project associated with a given Event you need to setup a JPQL query to get the Project?

Just want to check I am on the right track....

Cheers

Al


Top
 Profile  
 
 Post subject: Re: How to map the latest child of a @OneToMany association
PostPosted: Thu May 25, 2017 4:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
Many (Event) to One (Project) is best done by @ManyToOne on the child side. How do you determine which is the child? (I'm guessing it has to be the many (Event)).


It's very easy to determine which one is the child side in a one-to-one or one-to-many association. If you look at the database tables, the table that has the FK (e.g. Event) and cannot live without its parent (e.g. Project) is a child.

So, can you have an Event without a Parent? Well, even if you can, can you have multiple Projects assigned to an Event or multiple Events assigned to a Project? The many side distates the child side, hence the placement of the FK.

Quote:
Then the article goes on to say in order to make it bidrectional and be able to get the Project associated with a given Event you need to setup a JPQL query to get the Project?


Nope. Bidirectional means that you can navigate the association both ways. However, you can also use a JPQL query for the parent side. It's not like you always have to use bidirectional associations.


Top
 Profile  
 
 Post subject: Re: How to map the latest child of a @OneToMany association
PostPosted: Thu May 25, 2017 5:25 am 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
vlad wrote:
Quote:
Many (Event) to One (Project) is best done by @ManyToOne on the child side. How do you determine which is the child? (I'm guessing it has to be the many (Event)).


It's very easy to determine which one is the child side in a one-to-one or one-to-many association. If you look at the database tables, the table that has the FK (e.g. Event) and cannot live without its parent (e.g. Project) is a child.

So, can you have an Event without a Parent? Well, even if you can, can you have multiple Projects assigned to an Event or multiple Events assigned to a Project? The many side distates the child side, hence the placement of the FK.

Quote:
Then the article goes on to say in order to make it bidrectional and be able to get the Project associated with a given Event you need to setup a JPQL query to get the Project?


Nope. Bidirectional means that you can navigate the association both ways. However, you can also use a JPQL query for the parent side. It's not like you always have to use bidirectional associations.



So how then would you make it bi-directional? With the FK on event, if you have a project how do you determine which events are related to it?


Top
 Profile  
 
 Post subject: Re: How to map the latest child of a @OneToMany association
PostPosted: Thu May 25, 2017 5:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
So how then would you make it bi-directional? With the FK on event, if you have a project how do you determine which events are related to it?


This article explains how to map a one-to-one association and why making it bidirectional is not very useful.


Top
 Profile  
 
 Post subject: Re: How to map the latest child of a @OneToMany association
PostPosted: Thu May 25, 2017 6:57 am 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
But we are talking about ManyToOne - not OneToOne.


Top
 Profile  
 
 Post subject: Re: How to map the latest child of a @OneToMany association
PostPosted: Thu May 25, 2017 10:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
But we are talking about ManyToOne - not OneToOne.


According to your initial description, there is a 1:1 association between Event and Project

Quote:
Event to Project is 1:1 Uni Directional currently


Top
 Profile  
 
 Post subject: Re: How to map the latest child of a @OneToMany association
PostPosted: Thu May 25, 2017 1:53 pm 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
Hi Vlad

Yes but first sentence after second question I state I will have to change it to Many:1

Shoudl end up as:

Event(Few)---Project(1)----Tasks(Few)

And be fully bidirectional.

Cheers

Al


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