• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

one to one unidirectional mapping in jpa

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Andreas Heidrich
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic