-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: updating model - error on em.refresh()
PostPosted: Thu Apr 27, 2006 11:02 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
I have a domain model like this: Company -* Department -* Employee

On a Company's page I list all Departments with their Employees. The code on the page (facelets) just iterates and displays the Departments and their Employees.

When I add/delete an Employee, the page has to be updated so I do this in the backing bean:

Code:
em.refresh(company);
departments = company.getDepartments();


This works ok when *adding* an Employee. But when I refresh the company after *deleting* an employee I get this:

Code:
javax.ejb.EJBTransactionRolledbackException: javax.persistence.EntityNotFoundException: org.hibernat
e.UnresolvableObjectException: No row with the given identifier exists: [com.example.Employee#231]   


Any idea why, or how to avoid this? Or maybe a different way of refreshing my view? Thanks.


P.S.

I also tried like this instead of refreshing the company:
Code:
departments = departmentDAO.getAll(); // straightforward em query call


No error now, but the the view isn't updated correctly after neither adding or deleting an Employee (new Employees don't show up immediately, deleted ones still show).

Any help definitely appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 8:24 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
I think it's a valid question. Maybe the answer is really obvious but so far I haven't been able to figure it by myself.
TIA


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 11:26 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Where / how do you add / delete these entities?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 3:41 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
I am using JSF with Seam; the page is backed by a SFSB with an extended persistence context. If the delete operation is done in this SFSB, it works ok, but if the change to the model is done in another bean and then I return to this page and call refresh in the SFSB -> error.

In a model like type A -* B -*C, this error always happens when I delete a B or a C and then call em.refresh(parentA).

Hope I'm giving enough clues here; I think it must be an obvious newbie mistake anyway. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 3:50 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Hmmm ... that extended context seems suspicious.

But this looks like more of a Seam question. Better try it on Seam's forum.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 4:18 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
I did.

gavin.king@jboss.com wrote:
This kind of discussion should be moved to the Hibernate or JBoss EJB3 forums, its not Seam specific.
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 4:40 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
When you delete - there is probably still a row in the db that has a reference to deleted employee. In SFSB object model is probably 'correct', but in the database there are still some 'leftovers' (if you delete from different bean). You should perhaps use OnDelete or the right combination of cascade / inverse attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 5:21 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
It's not that, the database model is enforced with foreign key constraints; also, the delete action is cascaded and all it's "children" are correctly deleted too.

I believe the problem is due to the cascading refresh that follows the em.refresh(company). If I don't cascade the refresh action in the Company.departments relationship, then I don't get the error anymore. But like this, the Department who's Employee got deleted isn't refreshed and the Employee still shows up, even if it's not in the database anymore.

Here's the full stack of what happens on the em.refresh(company) call.

Code:
10:37:22,366 ERROR [STDERR] javax.ejb.EJBTransactionRolledbackException: javax.persistence.EntityNotFoundException: org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [com.example.Employee#707]

10:37:22,386 ERROR [STDERR] Caused by: javax.persistence.EntityNotFoundException: org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [com.example.Employee#707]
10:37:22,386 ERROR [STDERR]    at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:169)
10:37:22,386 ERROR [STDERR]    at org.jboss.ejb3.entity.InjectedEntityManager.refresh(InjectedEntityManager.java:156)
10:37:22,386 ERROR [STDERR]    at com.friday.dao.ejb.GenericDAOBean.refresh(GenericDAOBean.java:44)
10:37:22,386 ERROR [STDERR]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:37:22,386 ERROR [STDERR]    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
10:37:22,386 ERROR [STDERR]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
10:37:22,386 ERROR [STDERR]    at java.lang.reflect.Method.invoke(Method.java:585)
10:37:22,386 ERROR [STDERR]    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:109)
10:37:22,387 ERROR [STDERR]    at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
10:37:22,387 ERROR [STDERR]    at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:98)
10:37:22,387 ERROR [STDERR]    at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
10:37:22,387 ERROR [STDERR]    ... 165 more

10:37:22,389 ERROR [STDERR] Caused by: org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [com.example.Employee#707]
10:37:22,389 ERROR [STDERR]    at org.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:42)
10:37:22,389 ERROR [STDERR]    at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:126)
10:37:22,389 ERROR [STDERR]    at org.hibernate.impl.SessionImpl.fireRefresh(SessionImpl.java:924)
10:37:22,389 ERROR [STDERR]    at org.hibernate.impl.SessionImpl.refresh(SessionImpl.java:907)
10:37:22,389 ERROR [STDERR]    at org.hibernate.engine.CascadingAction$4.cascade(CascadingAction.java:92)
10:37:22,389 ERROR [STDERR]    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
10:37:22,389 ERROR [STDERR]    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
10:37:22,389 ERROR [STDERR]    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
10:37:22,389 ERROR [STDERR]    at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
10:37:22,389 ERROR [STDERR]    at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
10:37:22,390 ERROR [STDERR]    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
10:37:22,390 ERROR [STDERR]    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
10:37:22,390 ERROR [STDERR]    at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
10:37:22,390 ERROR [STDERR]    at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:99)
10:37:22,390 ERROR [STDERR]    at org.hibernate.event.def.DefaultRefreshEventListener.onRefresh(DefaultRefreshEventListener.java:39)
10:37:22,390 ERROR [STDERR]    at org.hibernate.impl.SessionImpl.fireRefresh(SessionImpl.java:915)
10:37:22,390 ERROR [STDERR]    at org.hibernate.impl.SessionImpl.refresh(SessionImpl.java:899)
10:37:22,390 ERROR [STDERR]    at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:166)
10:37:22,390 ERROR [STDERR]    ... 175 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 5:30 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Since you have an extended context in SFSB - that deleted employee is still in your context and still attached to departement - when refreshing departement, the deleted employee wants to refresh himself - but he doesn't exist anymore in db.

Is this it?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 5:49 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
Yes, this is pretty much what I suspected too. Since I delete in another bean, with another PersistenceContext, the SFSB's PC is not aware of the changes, sounds sensible. But I don't know how to handle this. It must somehow detect the changes done outside of it and synchronize with them; maybe mark it as "dirty" somehow. This is what I was trying to do by refreshing the company, but it seems I didn't understand correctly what that does :(
So, how should it be done?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 5:53 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Why doing this in another session bean?

That's what Seam is all about - handling appropriate contexts - conversation, session, ...

Use that SFSB.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 6:38 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
I can't just have one god Facade SFSB that handles all operations on all entities. I find it logical to separate business logic into several beans, by use case.

For example, for Employees I have Reports. On the "main" page, near each Employee is the number of Reports related to him.
For CRUD operations on Reports, I have another SFSB that handles that. So, while browsing the reports section, I might delete a Report, then, sometimes later, return to the "main" page. I want the number of Reports updated correctly on that page.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 6:55 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Ok, different session beans for different ops - sounds reasonable.
Then you are misusing Seam in this case (not that I'm expert in Seam) - you are actually not holding an extended conversation over same context.

Maybe use plain SLSBs with transaction context.

Ok, but this is a totally different topic - not part of this ejb3 support - but architectual design.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 7:07 am 
Newbie

Joined: Tue Dec 06, 2005 12:02 pm
Posts: 13
I got it. I'll probably link this discussion on the Seam forum and ask there.

Sticking to the EJB3 persistence part of this: can I or can I not refresh the data in my PersistenceContext if I've done changes within another PC?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 7:50 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Hmmm ... probably this is the idea behind refresh - getting the changes from db which were a result of other requests.

The thing to ask here is how really refresh is doing.
For example (and like in your example):
Is this expected behaviour, to not update collections before refreshing the elements of this collection.

Like you would like to refresh a bean which was deleted in other request - an obvious exception.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.