Forums Register Login

one to one unidirectional mapping in jpa

+Pie Number of slices to send: Send
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 .

Regards,
-anagha
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 52770 times.
Similar Threads
Multiple Column Join
org.hibernate.MappingException: Could not determine type for:... with @ManyToOne
EJB3/JPA @OneToOne
two parents, one child, ora-02291
non-standard join in ManyToOne
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:45:44.