[Logo] Terracotta Discussion Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
org.hibernate.TransientObjectException  XML
Forum Index -> Ehcache
Author Message
girish_k

journeyman

Joined: 06/06/2012 04:06:11
Messages: 13
Offline

I am getting org.hibernate.TransientObjectException
I activated EhCache and
running Sample project for simple entity Country1(id,name,code)

StackTrace:
Hibernate: select this_.cn_id as cn1_1_0_, this_.cn_code as cn2_1_0_, this_.cn_name as cn3_1_0_ from COUNTRY1 this_
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Country1
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.ManyToOneType.disassemble(ManyToOneType.java:163)
at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:80)
at org.hibernate.loader.Loader.putResultInQueryCache(Loader.java:2194)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2138)
at org.hibernate.loader.Loader.list(Loader.java:2096)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at com.wakaleo.articles.caching.dao.CountryDAO.getCountries(CountryDAO.java:32)
at com.wakaleo.articles.caching.dao.CountryDAOTest.testGetCountries(CountryDAOTest.java:35)
at com.wakaleo.articles.caching.dao.CountryDAOTest.main(CountryDAOTest.java:91)
Java Result: 1


Code:
List resultList = null;
resultList = SessionManager.currentSession().createCriteria("Country1").setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).setCacheable(true).list();
return resultList;

Please Reply solution

alexsnaps

consul

Joined: 06/19/2009 09:06:00
Messages: 464
Online

So you mean making it non-cacheable solves the issue ?

Alex Snaps (Terracotta engineer)
girish_k

journeyman

Joined: 06/06/2012 04:06:11
Messages: 13
Offline

.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
.setCacheable(true)

the above combination giving issue.
alexsnaps

consul

Joined: 06/19/2009 09:06:00
Messages: 464
Online

Looking closer at the stacktrace, it seems you have an unsaved entity in ... I had expected the params used on that query. Don't know what version of hibernate that is, so can't match the stacktrace for sure. Anyways, but it looks like it happens when disassembling the actual result. Now, if I read this right, the results won't be actual entities, they can't be disassembled to their PK... is this right ? As I see it this is an Hibernate (usage?) issue though... One question that comes to my mind is "are such results even cacheable ?"

Alex Snaps (Terracotta engineer)
girish_k

journeyman

Joined: 06/06/2012 04:06:11
Messages: 13
Offline

Well ... this entity(Country) is independent so no FK associated with it
Hibernate ver:3.2.5ga
Still not clear about :
Unsaved ? disassemble ?
why hibernate is handling queryCache , if EhCache is already configured?

alexsnaps

consul

Joined: 06/19/2009 09:06:00
Messages: 464
Online

Hibernate _is_ the client for ehcache. Ehcache knows nothing abou Hibernate, but for the glue code (as the cache is abstracted in Hibernate).

Is that message from Hibernate clear to you:
Code:
TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Country1 

That's why I referrer to unsaved entity : transient.
As for disassemble, that's how Hibernate stores your entities in the Cache, it doesn't store the instances themselves...

Alex Snaps (Terracotta engineer)
girish_k

journeyman

Joined: 06/06/2012 04:06:11
Messages: 13
Offline

Still debugging with the source code
and observed that hibernate throws exception before putting it in cache


this time with following code :
Criteria fetchCriteria = SessionManager.getSession(EntityMode.MAP)
.createCriteria("Country1")
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)
.setCacheable(true);
fetchCriteria.add(Restrictions.ilike("name", (Object) "B%"));

resultList = fetchCriteria
.setFirstResult(0)
.list();

And works if setResultTransformer is commented.
 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team