aspose file tools*
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
Author

Using OneToOne as OneToMany

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Hi!

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: 2885
    
  13

Ankit,

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.

Devaka.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Oh Great! Thanks Devaka for confirming that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using OneToOne as OneToMany
 
Similar Threads
merge issue in EJB3.0
why creating an Object of an servlet and calling it isn't possible
Help understanding when to use em.find and query manager and their ability to fetch nested objects
Auxiliary tables saving
Updated Objects Are Not Being Persisted