This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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

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: 9405


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

Joined: Jul 29, 2008
Posts: 3600


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.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Ankit Garg

Joined: Aug 03, 2008
Posts: 9405

Oh Great! Thanks Devaka for confirming that
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Using OneToOne as OneToMany
jQuery in Action, 3rd edition