Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » (no subject)
(no subject) [message #720628] Wed, 31 August 2011 00:20 Go to next message
AmFreak Missing name is currently offline AmFreak Missing nameFriend
Messages: 25
Registered: June 2011
Junior Member
Hi,

i have a database with 2 tables with a many to many relation.
One table with projects and one with persons. Persons can be in projects or not.
I fill a list with all projects and one with all persons:

Query query = em.createQuery("select p from Project p");
List<Project> projects = query.getResultList();

Query query = em.createQuery("select p from Person p");
List<Person> persons = query.getResultList();

Every person object has a list with the projects the person is in and every project object has a list of all persons in it.

The problem now is that a person in the persons list is a different object as the same person in a project. E.g. if i change the name of a person in the persons list, the person's name isn't changed in the projects which have that person in it cause it's a different object. But shouldn't it be the same one? Or what can i do to make it so it's the same one?
(no subject) [message #720978 is a reply to message #720628] Wed, 31 August 2011 15:37 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
If you are using the same EntityManager, they should be the same instances, as long as they have not been detached/serialized. Can you describe the read process?

If they are detached or different EM instances, the changes need to be merged back into an EntityManager before they will be writen to the database and read by other EMs. You can merge into the one you are reading from or commit the changes back to the first before attempting a read elsewhere.

Best Regards,
Chris
(no subject) [message #721125 is a reply to message #720628] Wed, 31 August 2011 22:56 Go to previous message
AmFreak Missing name is currently offline AmFreak Missing nameFriend
Messages: 25
Registered: June 2011
Junior Member
Thanks for the answer - i am currenty using two EntityManagers. They look like this:

public class ProjectsModel extends MainModel {

public ProjectsModel() throws Exception {
super();
}

public List<Project> readProjects() {

Query query = em.createQuery("select p from Project p");
List<Project> projects = query.getResultList();
return projects;


The PersonsModel looks the same and the MainModel looks like this:

public class MainModel {
protected EntityManager em;
protected EntityTransaction transaction;

public MainModel() throws Exception {
em = EntityManagerFactory.createEntityManager();
transaction = em.getTransaction();
transaction.begin();
}

code for getting the data:


model = new ProjectsModel();
List<Project> projects = model.readProjects();

PersonsModel pm = new PersonsModel();
List<Person> persons = pm.readPersons();

I tried to use one EntityManagaer by making the em and the transaction in the MainModel static and checking if the em exist before making one. The persons then were indeed the same ones, but then i started to get duplicate key errors on different occasions (when changing a persons names 2 times or deleting persons which are in projects) or "Entity must be managed to call remove" when i try to delete persons 2 times. So is using one entitymanager for the 2 model's really a valid or the right solution?
Previous Topic:Different objects in different queries
Next Topic:java.lang.ClassCastException: com.sun.xml.bind.v2.runtime.JAXBContextImpl cannot be cast to javax.xm
Goto Forum:
  


Current Time: Thu Mar 28 23:02:25 GMT 2024

Powered by FUDForum. Page generated in 0.05796 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top