This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

JPA - OneToOne - Unidirection - Parent key refer in child class  RSS feed

 
Ranch Hand
Posts: 118
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was in a situation where I need to refer parent key id in child table. This is one-to-one relationship.
The child table only get the record when certain situation occurred. So not all records from parent are in child table but whatever the records contain by child are in parent table.
I made the changes in the code, when the child records gets inserted into the table, making sure it has a parent id with it.
I want to refer the records from child table and when I fetch them I also need the parent record associated with it.
I am using JPA and I handled the situation by making the following changes into the POJO classes.

The Parent class:


The Child class:


The code is working fine and I don't have any issue with it but just want to understand the things I am doing are right to define and drive the relation between parent and child.
Or is there any other way/workaround to handle the situation in much better way.

The question I asked because I am new to JPA and want to understand its basics.


Thanks,
Atul
 
Bartender
Posts: 19997
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parent/child isn't One-to-One. It's one-to-Many on the parent and Many-to-One on the child entity. Whether you define the mappings unidirectionally or bi-directionally is up to you.

A fully bi-directional mapping would include a collection of Child Entity. You can make this lazy-fetch or eager-fetch, depending on how you expect to use it. The child Entity would have a ManyToOne relationship back to its parent.

Normally you do the linkages using the Entity property name as the reference key. That may or may not be the same name as the database column name, depending on how the Entity maps to the underlying database schema. In my case column names are often things like "CREATE_DATE", and the corresponding Entity property name is java-style (createDate).
 
Atul More
Ranch Hand
Posts: 118
1
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Sorry for late response and thanks for sharing the inputs.
I am new to JPA and related technologies. I want to understand that whatever I am doing is the right or any improvement is required.

If you again visit the post, I have done many things in one go and trying to achieve the result.

If you know what is best way to do the things then it definitely help to achieve a good result.


Thanks,
Atul
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!