Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA same object different reference (copy)

 
Alaneus Kingingestingest
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm learning JPA, so I have questions about how does it work and about best practices...

Let's say that I have Object A which has OneToMany connection with Object B.
I have method for fetching objects A which creates EntityManager and fetches all Objects A (and its referenced Objects B).
I have also method for fetching objects B which also creates EntityManager and fetches all Objects B.

But here comes the problem: objects B created in first method and objects B created in second method do not have same references (memory location in application), and if I change some property of one object B which is result of second method, same (equal) object B which is created in first method does not change instantly in application because it is not the same object, actually it is copy of that object.

If I use global EntityManager (If I do not create in methods EntityManagers) I do not get this problem, but I saw that creating in method EntityManager is best practice.

Can I somehow say that I do not want duplicate objects?

Sorry for bad english.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Essentially, you should let the entity manager load all of your objects. The entity manager can hand over objects to you, and make sure that the objects you get are always in sync, regardless if you got the object through a direct load, or if you got it through an associated object. The entity manager keeps track of everything, and ensures that any changes are always co-ordinated, snychronized and finally, updated when the database is hit.

One of my most popular tutorials on Hibernate and JPS is one named How Hibernate Works? It's obviously based on Hibernate with JPA annotations, but the corollary is great between the Hibernate Session Manager and the JPA/EJB3 entity manager. Here it is:

How Hibernate and the Java Persistence API Works

I think one question you have might be why you have this same object loaded into different detached references at the same time in your application. Refactoring this scenario out of your program will greatly simplify your life, and make your applications easier to maintaing and manage, as the hard work is being left to the Entity Manager. After all, managing entities is what the Entity Manager does best!

By the way, I think your English is just fine. I wouldn't have thought it wasn't your mother tongue if you hadn't brought any attention to it.

Kindest regards,

-Cameron McKenzie
 
Victor Lindberg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I SOLVED THIS.

I created a component that makes this whole process for you based on the annotations of the package (javax.persistence).

Component already sets the id of the entity to null. He does all the analysis of the algorithm to be applied based on the type of each attribute relationship @ OneToMany @ OneToOne or @ ManyToMany.


Example

Person person = personDAO.find(1);
PersistenceCloner cloner = new PersistenceCloner(person);
Pessoa personCopy = cloner.generateCopyToPersist();

Download JAR and SOURCES: lindbergframework-cloner
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic