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.
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 ?
posted 11 years ago
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.
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop