-->
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.  [ 5 posts ] 
Author Message
 Post subject: Some odd Behavoir EntityManager
PostPosted: Mon May 26, 2008 9:42 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
Hi everybody,

i did some refactoring on my DAO's to optimize them.
The find method works fine. I got some trouble when i used em.find(..) and after that em.remove(..). i always got an exception like "Removing a detached instance..".

i solved this by changing find into getReference. However i'm not really satisfied with it cause i don't really understand why it now "works".

The odd behavoir is now i dont get an exception and i do get the Data with getReference but when i try to use remove nothing happens.

I don't get an exception, i kinda suspect that there is no commit but i thought em.remove(..) should cover all this (The EntityManager).

Same happens for em.persist(), i don't get any exceptions but it doesn't persist my data.

Maybe i'm missing something would be nice if someone could help me.

edit: i just found out that getReference somehow does not make a select on my database i dont know why, i guess thats the reason why remove doesnot work, but if i use the find method i'm back to square one ..


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 26, 2008 12:49 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I think you might be removing an instance that at some point has been evicted from the EntityManager. Is this possible? If it is evicted, it is no longer a persistent object, and instead a detached instance. That is certainly what is happening. You'd have to look deeper into the code to see exactly what it is happening.

Here's a friendly little tutorial called How Hibernate Works that might give you more insight into detached, persistent and transient instances with Hibernate and JPA.

http://www.ezhibernate.com/HiberBookWeb/learn.jsp?tutorial=07howhibernateworks

Good luck!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 4:40 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
Hello McKenzie,

thanks for your response. Frankly the tutorial was really good lighning some issues up. however i did not find a solution for my problem its like only em.find() is working.
The strange thing is if i'm looking at my log nothing happens when em.persit is called and i'm sure it is called.

And well i don't really know how to do a remove, i mean as i have understood it now if i make a find the object i get is detached therefore i can't do a operation like remove on it, i have to associate it with the session first but i'm using an shared EntityManager and cant use something like em.getTransaction().begin() and em.getTransaction.commit() i get an Exception then like "Not allowed to create transaction on shared EntityManager - use Spring transaction...". Basiclly i'm having an interface with the 4 CRUDS + and implementation of it and im using spring to inject this into an struts 2 action so how can i assosiate a object i read into a struts 2 action into the hibernate session??

Shouldn't the EntityManager taking care of this issues when i'm doing an service.remove(obj); ? I mean it would be nice if the EntityManager would check if the Object is detachted and if it is then assosiate it with the session.

edit: if i do a direct call like this em.remove(em.getReference(Entity.class, PK)); nothing happens either and if i look at the logs this command causes Hibernate todo 5 selects i'm really confused !?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 5:51 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
Hello again,

i'm pretty sure i found out why it isnt working and ive fixed it. But honestly i don't really understand it. First of all the solution is to add "@Transactional" at the Interface. After that persist works fine and merge too (didn't test merge but im sure it works).

So i have a workaround for the remove issue, since now everything is working in a transaction i can call the find method within the remove method so the object i retrieve has still the persistent state. However i kinda don't like this solution and for now ive just implemented it static like em.remove(em.find(Class,PK)); however to make this dynamic i have to change the params and i dont like to do that. Is there somewhere bestpractise for such issues? Basiclly i want to read the object into the struts 2 action and then call a remove.

edit: is there maybe a method which i can use for the shared entitymanager to associate an detached object with the session?

btw. excuse me for my bad english t.t


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 6:54 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
Hi,

here is part of my CRUD Interface.
Code:

T create(T newInstance);
T read (PK id);
void merge (T transientObject);
void remove(T persistentObject);



so i think possible solutions to my problem about remove are:

1. i change the remove method and give him too the param PK id
then i can call within remove find and there should be no problem to delete it.
2. i change merge method that i get a return value T so when i call find in my action i call merge after that and from what ive read so far my obj will change it state to perstistent and is ready to getting deleted.

are there any other suggestions maybe or which way is the better togo ?

edit: Solution 1. works pretty well, but 2. not i still get the exception about removing a detached object which kinda strange after a find the state should be detatched and after the merge it should be transient shouldn't it be? And i guess removing a transient obj should be a big deal ..strange.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.