-->
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.  [ 7 posts ] 
Author Message
 Post subject: Help with associations
PostPosted: Thu Dec 10, 2015 5:23 am 
Newbie

Joined: Thu Dec 10, 2015 4:57 am
Posts: 4
I have two tables, and need help with the associations:

table_a (id int primary key, description text);

table_b(id int primary key, link_1 int foreign key (table_a id), link_2 int foreign key (table_a id) unique);

So from table_a's perspective, it's a one to many relation ship if joining on link_1 column, but a one to one when joining on link_2.

Any suggestions on what associations I can use?


Top
 Profile  
 
 Post subject: Re: Help with associations
PostPosted: Thu Dec 10, 2015 5:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
So both link1 and link2 reference the table_a identifier.

Because the link2 has a unique constraint, it means that you might have multiple table_b rows with a link1 value that point to one row in table_a, but those table_b rows would need to still have unique link2 values.

You could map two @ManyToOne associations using link1 and link2 in TableB entity and have the mappedBy sides (with @OneToMany and @OneToOne) in the TableA entity.


Top
 Profile  
 
 Post subject: Re: Help with associations
PostPosted: Thu Dec 10, 2015 7:07 am 
Newbie

Joined: Thu Dec 10, 2015 4:57 am
Posts: 4
mihalcea_vlad wrote:
So both link1 and link2 reference the table_a identifier.

Because the link2 has a unique constraint, it means that you might have multiple table_b rows with a link1 value that point to one row in table_a, but those table_b rows would need to still have unique link2 values.

You could map two @ManyToOne associations using link1 and link2 in TableB entity and have the mappedBy sides (with @OneToMany and @OneToOne) in the TableA entity.


But as soon as I do @ManyToOne in TableB Entity with @JoinColumn("link_2") and @OneToOne in TableA Entity with mappedBy="link2", Eclipse throws an error saying "In attribute EntityA, the mapped by attribute link2 has an invalid mapping type for this relation".

It seems, it's not possible to have ManyToOne on one entity and OneToOne on another entity of the relations.


Top
 Profile  
 
 Post subject: Re: Help with associations
PostPosted: Thu Dec 10, 2015 7:47 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Try this

Code:
public class A {

     @OneToMany(mappedBy = "a1")
     private Set<B> bs;

     @OneToOne(mappedBy = "a2")
     private B b;
}


Code:
public class B {
     @ManyToOne
     @JoinColumn(name = "link1")
     private A a1;

     @ManyToOne
     @JoinColumn(name = "link2", unqiue = true)
     private A a2;
}


Top
 Profile  
 
 Post subject: Re: Help with associations
PostPosted: Thu Dec 10, 2015 7:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Actually I might have given you the wrong suggestion, because the TableB should have a @ManyToOne for link1 and a @OneToOne for link2.
Then TableA has a OneToMany mappedBy to link1 and a @OneToOne mappedBy to link2.

So, there are not two @ManyToOne but a @ManyToOne and a @OneToOne is TableB.


Top
 Profile  
 
 Post subject: Re: Help with associations
PostPosted: Thu Dec 10, 2015 2:47 pm 
Newbie

Joined: Thu Dec 10, 2015 4:57 am
Posts: 4
babyfish wrote:
Try this

Code:
public class A {

     @OneToMany(mappedBy = "a1")
     private Set<B> bs;

     @OneToOne(mappedBy = "a2")
     private B b;
}


Code:
public class B {
     @ManyToOne
     @JoinColumn(name = "link1")
     private A a1;

     @ManyToOne
     @JoinColumn(name = "link2", unqiue = true)
     private A a2;
}


Thank you, this worked despite Eclipse claiming it to be an error. Had disable JPA validation to be able to make it work.


Top
 Profile  
 
 Post subject: Re: Help with associations
PostPosted: Thu Dec 10, 2015 2:48 pm 
Newbie

Joined: Thu Dec 10, 2015 4:57 am
Posts: 4
mihalcea_vlad wrote:
Actually I might have given you the wrong suggestion, because the TableB should have a @ManyToOne for link1 and a @OneToOne for link2.
Then TableA has a OneToMany mappedBy to link1 and a @OneToOne mappedBy to link2.

So, there are not two @ManyToOne but a @ManyToOne and a @OneToOne is TableB.


Yes, this is what finally worked. Thanks!


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