aspose file tools*
The moose likes Object Relational Mapping and the fly likes OneToOne bidirectionnal relationship with 2 foreign keys Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OneToOne bidirectionnal relationship with 2 foreign keys" Watch "OneToOne bidirectionnal relationship with 2 foreign keys" New topic
Author

OneToOne bidirectionnal relationship with 2 foreign keys

robert polidort
Greenhorn

Joined: Apr 09, 2010
Posts: 11
Hello,

I've got the following table structure from an existing project which is running with EJB2.0 Entities. I'm migrating those

entities to EJB3.0 JPA Objects. However, I'm faced to the following problem :

I've got a OneToOne bidirectionnal association between class A and class B. The fact is that the database table is made like

this :
table A(col_A_ID, col_A_1, col_A_2, col_B_ID)
table B(col_B_ID, col_B_1, col_B_2, col_A_ID)

We've got a primary key for table A (col_A_ID) and a primary key for table B (col_B_ID)
We've got a foreign key for table A (col_B_ID) and a foreign key for table B (col_A_ID)

I've sawn samples for Hibernate with only one side of the relation mapping the foreign key, but never both sides.

Like that the foreign key Table A.col_B_ID is missing :
---
public class A {
...
@OneToOne(mappedBy="a")
private B b;
...
}
---
public class B {
...
@OneToOne
@JoinColumn(name="col_A_ID")
private A a;
...
}
---

Is it possible to manage to do such a thing with JPA & Hibernate ? Why is it not done by default as with EJB2.0 ? If it is

possible to still do such a thing, is it much more costly concerning the performances of the application ?

Regards,
Blured.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
Yep, your mapping should work fine.
Every JPA relationship has an owning side. The JoinColumn (or JoinTable for ManyToMany) is applied to the owning side and does not have the mappedBy.
In your case as in other cases the owning side is just arbitrary.
robert polidort
Greenhorn

Joined: Apr 09, 2010
Posts: 11
The fact is I'd like to have really the both sides filled not only one. And I cannot manage to do that with hibernate.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
blured derulb wrote:The fact is I'd like to have really the both sides filled not only one. And I cannot manage to do that with hibernate.


Not sure I get that. For related entities to be affected you need to set the CascadeTypes on the mapping. Maybe explain a bit what you tried that didn't work.
robert polidort
Greenhorn

Joined: Apr 09, 2010
Posts: 11
The code sample is making a table structure like
A (col_A_ID)
B (col_B_ID, col_A_ID)

What I like to do is to have the following table structure :
A (col_A_ID, col_B_ID)
B (col_B_ID, col_A_ID)

Mapping the fact that A & B classes are OneToOne relationships

Regards,
Blured.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 973
    
    5

You say you haven't managed to do it but what have you done and what exception (if any) do you get?
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
I see, try using @JoinColumns and make sure to set both name and referencedColumnName for each @JoinColumn
robert polidort
Greenhorn

Joined: Apr 09, 2010
Posts: 11
Have you got sample of this kind of use ?
robert polidort
Greenhorn

Joined: Apr 09, 2010
Posts: 11
So I've tried :

public class A {
...
@OneToOne(mappedBy="a")
private B b;
...
}
---
public class B {
...
@OneToOne
@JoinColumn(name="col_A_ID", referencedColumnName="col_A_ID"))
private A a;
...
}

However, it fills only one side of the relation foreign key. Only B (col_B_ID, col_A_ID)



Regards,
Blured.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
The plural @JoinColumns with two @JoinColumn annotations

http://docs.oracle.com/javaee/5/api/javax/persistence/JoinColumns.html
robert polidort
Greenhorn

Joined: Apr 09, 2010
Posts: 11
The @joincolumns is when you've got composite foreign keys, which is not my case. I've got one key by class. The 2 classes are related by onetoone relationship. And I'd like that class A to have a foreign key pointing on class B, and that class B to have a foreign key pointing on class A. For the moment I've just manage to have class B to have a foreign key pointing on class A.
robert polidort
Greenhorn

Joined: Apr 09, 2010
Posts: 11
I've managed to do it by using the following code :
public class A {
...
@OneToOne
@JoinColumn(name="col_B_ID")
private B b;
...
}
---
public class B {
...
@OneToOne
@JoinColumn(name="col_A_ID")
private A a;
...
}

However, as you can see there is no mappedBy attribute. What does it implies ?

Regards,
Blured.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

robert,

Once again please UseCodeTags when posting code or configuration. It makes it much easier to read.

On a one-to-one relationship the the @JoinColumn annotation goes on the mapping of the entity that is mapped to the table containing the join column (This is the owning side). The mapped by goes on the entity that does not define the join column (the inverse side).

For a valid bi-directional association one side should have the @JoinColumn and the other should have the mappedBy.

If the mappedBy is absent from both sides the provider will treat each side as an independent unidirectional relationship and assume that each side is an owner and each side has a join column on the table.


[How To Ask Questions][Read before you PM me]
robert polidort
Greenhorn

Joined: Apr 09, 2010
Posts: 11
Sorry for the Code tag

So if I well understand it is impossible to have Hibernate to map exactly my existing table structure, apart if I use 2 JoinColumns and that it is not a valid bidir association..

Regards,
Blured.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Because both tables have a join column, what you have done is the best you can do. In this case there is not an owning side, both sides are owners hence there is not a bidirectional relationship but rather independent unidirectional relationships.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OneToOne bidirectionnal relationship with 2 foreign keys
 
Similar Threads
[Solved] [Problem] Referencing two objects with OneToOne relation
Best mapping approach for reference/LOV/lookup data
Simple JOIN in JPA proving difficult
JPA - foreign key filed question
mapping One-to-many relationships using NetBeans and Not Hibernate