-->
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.  [ 3 posts ] 
Author Message
 Post subject: fetch of a removed entity by natural id, with caching fails
PostPosted: Mon May 10, 2010 1:33 pm 
Newbie

Joined: Mon May 10, 2010 1:15 pm
Posts: 2
Hi

I am using hibernate with EHCache. I have removed a persisted entity using entityManager.remove(persistedEntity). In a later step, i want to add a new entity with the same natural id as the removed one. Before doing that, i am fetching any entity with that natural id to make sure that there is no such entity in the database :

Criteria c = ((Session) entityManager.getDelegate()).createCriteria(type);
c.add(Restrictions.naturalId().set("name", name));
c.setCacheable(true);
T entity=(T) c.uniqueResult();

I am getting the following exception:


javax.persistence.EntityNotFoundException: Unable to find xyz.application.auth.entity.Group with id 6
at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:113)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:171)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
at org.hibernate.type.ManyToOneType.assemble(ManyToOneType.java:219)
at org.hibernate.cache.StandardQueryCache.get(StandardQueryCache.java:155)
at org.hibernate.loader.Loader.getResultFromQueryCache(Loader.java:2184)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2147)
at org.hibernate.loader.Loader.list(Loader.java:2117)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1596)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:328)

it looks as though the removed entity was not evicted from the cache


Top
 Profile  
 
 Post subject: Re: fetch of a removed entity by natural id, with caching fails
PostPosted: Mon May 10, 2010 3:44 pm 
Newbie

Joined: Mon May 10, 2010 1:41 pm
Posts: 15
javax.persistence.EntityNotFoundException: Unable to find xyz.application.auth.entity.Group with id 6

So the EntityNotFoundException is stating that class "Group" with id 6 was not found... I don't know why this is coming back as an exception but you are getting the right result. Could wrap it in a try-catch, until you find a prettier way.

Since this is a natural id, is it possible to place the constraint with DB rather than in hibernate (as you are currently attempting, if it is not already), in which case you simply try to insert the row and check for success?


Top
 Profile  
 
 Post subject: Re: fetch of a removed entity by natural id, with caching fails
PostPosted: Tue May 11, 2010 3:59 am 
Newbie

Joined: Mon May 10, 2010 1:15 pm
Posts: 2
Actually i am not getting the right result. According the the javadoc of uniqueResult(), the function should return null if the query returns no result. Shouldn't the EntityManager.remove() invalidate the entry in the cache that maps the natural id to the persisted object?


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