wood burning stoves
The moose likes Object Relational Mapping and the fly likes one to one unidirectional mapping in jpa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "one to one unidirectional mapping in jpa" Watch "one to one unidirectional mapping in jpa" New topic

one to one unidirectional mapping in jpa

anagha desai

Joined: May 08, 2008
Posts: 22
Hello ,
Trying to learn JPA .

Am trying an example of Master Child relation but using One to One Mapping
The mapping is unidirectional .

"User" is the "Master Table" ( fields are :: id ( PK ) , name ( varchar )
"UserAddress" is Child Table with a foreign key referencing "User"
( fields are :: id ( PK ) , street (varchar) , uid( fk references id field of User table )

Here is my Java Code ::
User Class ::

Here is the Child class ::

Here is the test method ::

This works fine for me

However I dont want to compose my userAddress object with User Object
Want to do it the other way around
Also want to maintain unidirection one to one mapping
( From User to UserAddress )
No Change in Database tables ::
User will be master table with UserAddress As the Child table containing foreign key .

Here is the changed code that I tried ::
Parent class ::

Child Class ::

Test code ::

Here there are no errors .
Records are getting inserted in both parent and child
However the child table is getting a null in the foreign key column .

Any ideas will be greatly appreciated .

James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
The JPA spec does not allow a uni-direction OneToOne with the foreign key in the target table. It requires that you define a OneToOne in both directions and define the OneToOne in User to use a "mappedBy".

You need to either put the foreign key in the User table instead of the UserAddress, or add a OneToOne from UserAddress to User.

Depending on which JPA provider you are using, there may also be some non-standard solutions. You could try adding a userId basic mapping if you don't want a OneToOne, but a OneToOne would be better.

I would recommend you put the foreign key in the User table instead.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

I've got a little tutorial on just this topic on my website. You can find it through my signature links.

The annotations you will use will look something like this. On one side of the releationship you will have:

On the other side, you will have:

As James stated, the mappedBy attribute specifies the name of the instance variable in the used by the other side of the relationship to maintain the Java association.

Good luck!

-Cameron McKenzie
[ September 10, 2008: Message edited by: Cameron Wallace McKenzie ]
anagha desai

Joined: May 08, 2008
Posts: 22
Thanks James and Cameron .

Your replies are very much appreciated .

However I did try your suggestion and I still seem to be getting the foreign key inserted as null

Pardon me - but am using different classes from original example
But Concept is the same :: Master2 and Child2 are the two tables

Here is the excerpt of my code ::
This is in the class :: Master2

This is the code in Child2 ::

Here "pid" is the foreign key column in Child2 table .

For clarity pasting the DDLs of the tables used ::

My client code is ::

Thanks for all the help .

So not sure where I am goofing up
anagha desai

Joined: May 08, 2008
Posts: 22
My laziness and trying to get quick solutions from others without thinking it through .

The mistake was that in the test code didnt set the Master2 object to Child2 object

Once I did that the code works fine

Thanks James and Cameron for your invaluable tips .

This does however raise a few queries in my mind ::
1 >I am assuming my table design is fine ? ( with Child2 containing fk to Master2 )

2 >I am also assuming that my object design is good ::
( Master2 class containing Child2 class - ONLY unidirection )

3 >With above two assumptions :: think it is terrible jugglery to get the data persisted .

In simpler terms - would expect that once I set the child ( Child2 Object ) to Parent ( Master2 Object ) - internally Hibernate should be able to work out the insert

Your thoughts and comments please ?
Andreas Heidrich

Joined: Sep 08, 2009
Posts: 2
Hi anagha,
I read your post on the OneToOne problem with the key on the target side of the relation. I followed your solution, but found that the insert of the source table always tries to insert the attribute that is in fact not in the source table.
With reference to your example, it would try to insert a Child2 attribute when writing the master2 row into the database.

Did you had this problem too ?

Kind Regards
Andreas Heidrich

Joined: Sep 08, 2009
Posts: 2
Forget the question.

My mapping was not consistent. I had Annotations on attribute level in a superclass and on getter level in the subclass.

Everything is fine with your solution. We did solve the problem with exact the same solution and it is running fine now.
I agree. Here's the link: http://aspose.com/file-tools
subject: one to one unidirectional mapping in jpa
It's not a secret anymore!