-->
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.  [ 2 posts ] 
Author Message
 Post subject: JPA / bidirectionnal OneToOne & 2 external keys
PostPosted: Tue Sep 04, 2012 9:02 am 
Newbie

Joined: Tue Sep 04, 2012 9:00 am
Posts: 3
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.

The following code emphasis the fact that Table A.col_B_ID is missing, there will be only Table B.col_A_ID :
---
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 OneToOne bidir with 2 foreign keys (one for each side) 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.


Top
 Profile  
 
 Post subject: Re: JPA / bidirectionnal OneToOne & 2 external keys
PostPosted: Tue Sep 04, 2012 11:53 am 
Newbie

Joined: Tue Sep 04, 2012 9:00 am
Posts: 3
I've managed to do it 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.


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