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 )
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 .
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.
Cameron Wallace McKenzie
,
author and cow tipper
staff
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.
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
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.