• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OneToOne bidirectionnal relationship with 2 foreign keys

 
robert polidort
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, try using @JoinColumns and make sure to set both name and referencedColumnName for each @JoinColumn
 
robert polidort
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you got sample of this kind of use ?
 
robert polidort
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The plural @JoinColumns with two @JoinColumn annotations

http://docs.oracle.com/javaee/5/api/javax/persistence/JoinColumns.html
 
robert polidort
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
robert polidort
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic