File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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: 9465


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


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

Oh Great! Thanks Devaka for confirming that
I agree. Here's the link:
subject: Using OneToOne as OneToMany
jQuery in Action, 3rd edition