This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Using OneToOne as OneToMany Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Using OneToOne as OneToMany" Watch "Using OneToOne as OneToMany" New topic

Using OneToOne as OneToMany

Ankit Garg

Joined: Aug 03, 2008
Posts: 9291


I'm having a problem with my application. In my application, there are two classes, which are related to each other via a one sided OneToOne relationship like this

Now I have a situation in which two rows of First have to refer to the same data of the class Second. So what I want to know is that can I use the same Second object with two First objects like this

If this will not create any problems. I'm not sure that this code might even create two copies of the Second object in the database ...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3011


Your code works fine as you expected. It is a responsibility of the JPA to read the entity and persist the data to the database. Even though the "sec" object is managed in the persistence context, you can use it to refer the same relationship again and again.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

Oh Great! Thanks Devaka for confirming that
It is sorta covered in the JavaRanch Style Guide.
subject: Using OneToOne as OneToMany
Similar Threads
Help understanding when to use em.find and query manager and their ability to fetch nested objects
Updated Objects Are Not Being Persisted
Auxiliary tables saving
merge issue in EJB3.0
why creating an Object of an servlet and calling it isn't possible