-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: EntityManager fails to commit and looses associations
PostPosted: Mon May 26, 2008 1:30 pm 
Beginner
Beginner

Joined: Fri Jun 15, 2007 6:32 am
Posts: 23
Hi ,
i've written a genericdao impl that holds an entitymanager using hibernate under jbos 4.2 (latest ver. of hibernate) i get the entitymanager once using a static getEntitymanager that initiates a factory and fetches the entitymanager , when i merge it doesn't commit to db i have to em.flush for it to work tough the proper instance is retrieved when using em.find (it holds the updated values , it does not go to DB to fetch but brings frmo cache i know because i showsql ) , there is one more problem it looses the onetomany assoications after i merge i have to em.refres(em.find) in order to bring the entity with the assoications .
the method is under propagtion required and the method is called from within a jboss container managed bean when the persistnce unit is under jta managment .
the instance itself is brough from another jvm (either xml file import or webservice ) using @version
hope this makes some sense

thanks in advance .


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 26, 2008 5:00 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you really shouldn't get, share and reuse a single EntityManager instance for your whole application, but use a new one in each bean requiring it's services; best option is dependency injection;
If you really need a static variable you may try to keep reference to an EntityManagerFactory.

Also you shouldn't ever need to flush, I think it has to do with the previous issue but you may also need to integrate your configuration with JBoss, read about JTA:
http://www.hibernate.org/hib_docs/entitymanager/reference/en/html_single/

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 6:17 am 
Beginner
Beginner

Joined: Fri Jun 15, 2007 6:32 am
Posts: 23
s.grinovero wrote:
you really shouldn't get, share and reuse a single EntityManager instance for your whole application, but use a new one in each bean requiring it's services; best option is dependency injection;
If you really need a static variable you may try to keep reference to an EntityManagerFactory.

Also you shouldn't ever need to flush, I think it has to do with the previous issue but you may also need to integrate your configuration with JBoss, read about JTA:
http://www.hibernate.org/hib_docs/entitymanager/reference/en/html_single/

it's not for the whole application but per impl of a "t extends igenericdao"
i can't use injections because the class itself is not managed by the container but trough a static factory (legacy code) , i have some sort of helper factory
that retreives a helper from a static getinstance and i've created a new class that overides the class and the methods and insted of raw sql and complicated mapping utilize jpa and jpql , but since it's a stateless factory i have to use getEntitymanager (or do i ? )since the A.S doesn't run it's life cycle ( i tough about maybe in getInstace ask from the jndi the real instace ? and that might solve it ) i may have to have change it to a static getfacory and each time let the factory create a new manager ... ?
another problem is that it is packed inside an ear that all but this project are ejb2.1 jars and i'm ejb3 , so the jta is fetched from the appliactionserver .


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 6:34 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
it's not for the whole application but per impl of a "t extends igenericdao"

so you are using a different EntityManager for each of your persistent types? This makes it even worse and really explains you loosing associations.

Quote:
i can't use injections because the class itself is not managed by the container but trough a static factory (legacy code) , i have some sort of helper factory
that retreives a helper from a static getinstance and i've created a new class that overides the class and the methods and insted of raw sql and complicated mapping utilize jpa and jpql , but since it's a stateless factory i have to use getEntitymanager (or do i ? )since the A.S doesn't run it's life cycle ( i tough about maybe in getInstace ask from the jndi the real instace ? and that might solve it ) i may have to have change it to a static getfacory and each time let the factory create a new manager ... ?

Yes you definitely should modify the code in your helper class to ask JBoss using JNDI to give you a managed EntityManager.

I think you could really help yourself buying the book in the banners on this forum, it's all explained there. trust me I'm not biased, I don't get a cent for that and I bought it myself too.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 12:49 pm 
Beginner
Beginner

Joined: Fri Jun 15, 2007 6:32 am
Posts: 23
s.grinovero wrote:
Quote:
it's not for the whole application but per impl of a "t extends igenericdao"

so you are using a different EntityManager for each of your persistent types? This makes it even worse and really explains you loosing associations.

Quote:
i can't use injections because the class itself is not managed by the container but trough a static factory (legacy code) , i have some sort of helper factory
that retreives a helper from a static getinstance and i've created a new class that overides the class and the methods and insted of raw sql and complicated mapping utilize jpa and jpql , but since it's a stateless factory i have to use getEntitymanager (or do i ? )since the A.S doesn't run it's life cycle ( i tough about maybe in getInstace ask from the jndi the real instace ? and that might solve it ) i may have to have change it to a static getfacory and each time let the factory create a new manager ... ?

Yes you definitely should modify the code in your helper class to ask JBoss using JNDI to give you a managed EntityManager.

I think you could really help yourself buying the book in the banners on this forum, it's all explained there. trust me I'm not biased, I don't get a cent for that and I bought it myself too.

i'm using a diffrent entitymanager per dao but i only have one persistence unit in the persistence.xml but definatly getting the isntance from the jndi would make it better .
but i still haven't understood why is it loosing only the associations when merging and keeping the correct data on the regular attributes , can u think of a reason ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 1:36 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
but i still haven't understood why is it loosing only the associations when merging and keeping the correct data on the regular attributes , can u think of a reason ?

I think it's because of the multiple EntityManagers, this is not the way it is meant to be used.

Also, are you using bidirectional associations? did you remember to set the values from both sides?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.