-->
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.  [ 7 posts ] 
Author Message
 Post subject: DetachedQuery, Subqueries.exists: HibernateSystemException
PostPosted: Mon Jun 26, 2006 9:12 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:54 am
Posts: 28
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
v3.1.3

Mapping documents:
A Notice has a reference to multiple Statusses.

Code between sessionFactory.openSession() and session.close():
DetachedCriteria criteria1 = DetachedCriteria.forClass(Notice.class);
DetachedCriteria criteria2 = DetachedCriteria.forClass(Status.class);
criteria1.add(Subqueries.exists(criteria2));
getHibernateTemplate().findByCriteria(criteria1);

Full stack trace of any exception that occurs:
org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.notary.app.taxsocnot.notice.model.Status; nested exception is org.hibernate.MappingException: Unknown entity: com.notary.app.taxsocnot.notice.model.Status
org.hibernate.MappingException: Unknown entity: com.notary.app.taxsocnot.notice.model.Status
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:46)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:333)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1514)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.springframework.orm.hibernate3.HibernateTemplate$37.doInHibernate(HibernateTemplate.java:988)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:978)
at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:971)
at com.notary.app.taxsocnot.notice.dao.NoticeDAOImpl.query(NoticeDAOImpl.java:48)
at com.notary.app.taxsocnot.notice.service.NoticeManagerImpl.query(NoticeManagerImpl.java:75)
at com.notary.app.taxsocnot.notice.service.NoticeManagerImpl$$FastClassByCGLIB$$7e317016.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:705)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:643)
at com.notary.app.taxsocnot.notice.service.NoticeManagerImpl$$EnhancerByCGLIB$$67d7ae35_2.query(<generated>)
at com.notary.app.taxsocnot.notice.test.NoticeManagerTestCase.testFindOnNoticeData(NoticeManagerTestCase.java:1102)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Name and version of the database you are using:
SQLServer 2000

The generated SQL (show_sql=true):
Exception is thrown before the sql has been generated.

Debug level Hibernate log excerpt:
N/A

-I would like to get all Notices for which the status is 'Open'. In order to achieve this, I would like to have something similar to this SQL statement: select * from t_notice notice where exists (select id from t_status status where status.state='Open' and status.noticeId = notice.id)

-When using a DetachedCriteria together with an "exists" subquery, a HibernateSystemException is generated. When using session.createQuery("from om.notary.app.taxsocnot.notice.model.Notice notice where exists (select id from com.notary.app.taxsocnot.notice.model.Status status where status.state = 'Open' and status.noticeId = notice.id)") the query returns a correct result. Therefor, I am pretty sure that the mapping documents are correct and that the required entity exists.

-Reversing Notice.class and Status.class, generates the underneath listed stack trace:
org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.notary.app.taxsocnot.notice.model.Notice; nested exception is org.hibernate.MappingException: Unknown entity: com.notary.app.taxsocnot.notice.model.Notice
org.hibernate.MappingException: Unknown entity: com.notary.app.taxsocnot.notice.model.Notice
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
...

-Is it possible that this issue is linked with http://opensource.atlassian.com/project ... se/HHH-158?

-If Subqueries.exists is not supported by the DetachedCriteria class, I would like to replace it with something similar to: criteria1.addWhereClause("where exists (select id from com.notary.app.taxsocnot.notice.model.Status status where status.state = 'Open' and status.noticeId = notice.id)". Is this possible or is there another alternative?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 26, 2006 11:36 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The sample code you produced, if it worked, would result in SQL something like "select * from Notice where exists (select * from Status)". No relationship between the subquery and the main query.. that's going to confuse things. You'll have to add a restriction to criteria2 so that hibernate knows how to associate the two criteria. I'm not sure that that will fix the issue, but that is at least part of the problem.

I'm sure that this can be done using subqueries, but I have to ask: why? If you want to limit the returned Notices, why not just do an inner join? createAlias works very well for this.
Code:
DetachedCriteria criteria1 = DetachedCriteria.forClass(Notice.class, "n");
criteria1.createAlias("n.Statuses", "s");
criteria1.add(Restrictions.eq("s.State", "Open"));

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 3:12 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:54 am
Posts: 28
The reason why I am using an exists subquery is because a Notice can have multiple 'Open' Statusses. In the case where a Notice has 2 'Open' statusses, an inner join returns 2 Notices whereas an exists subquery returns one Notice.

I have changed the code and included a Restriction. Adding this Restriction results however in the same MappingException.

Code:
DetachedCriteria criteria1 = DetachedCriteria.forClass(Notice.class,"n");
DetachedCriteria criteria2 = DetachedCriteria.forClass(Status.class,"s");
criteria2.add(Restrictions.eqProperty("s.noticeId","n.id");
criteria1.add(Subqueries.exists(criteria2));
getHibernateTemplate().findByCriteria(criteria1);


org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: com.notary.app.taxsocnot.notice.model.Status; nested exception is org.hibernate.MappingException: Unknown entity: com.notary.app.taxsocnot.notice.model.Status
org.hibernate.MappingException: Unknown entity: com.notary.app.taxsocnot.notice.model.Status
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:46)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:333)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1514)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.springframework.orm.hibernate3.HibernateTemplate$37.doInHibernate(HibernateTemplate.java:988)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:978)
at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:971)
at com.notary.app.taxsocnot.notice.dao.NoticeDAOImpl.query(NoticeDAOImpl.java:48)
at com.notary.app.taxsocnot.notice.service.NoticeManagerImpl.query(NoticeManagerImpl.java:75)
at com.notary.app.taxsocnot.notice.service.NoticeManagerImpl$$FastClassByCGLIB$$7e317016.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:705)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:643)
at com.notary.app.taxsocnot.notice.service.NoticeManagerImpl$$EnhancerByCGLIB$$67d7ae35_2.query(<generated>)
at com.notary.app.taxsocnot.notice.test.NoticeManagerTestCase.testFindOnNoticeData(NoticeManagerTestCase.java:1102)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

tenwit wrote:
The sample code you produced, if it worked, would result in SQL something like "select * from Notice where exists (select * from Status)". No relationship between the subquery and the main query.. that's going to confuse things. You'll have to add a restriction to criteria2 so that hibernate knows how to associate the two criteria. I'm not sure that that will fix the issue, but that is at least part of the problem.

I'm sure that this can be done using subqueries, but I have to ask: why? If you want to limit the returned Notices, why not just do an inner join? createAlias works very well for this.
Code:
DetachedCriteria criteria1 = DetachedCriteria.forClass(Notice.class, "n");
criteria1.createAlias("n.Statuses", "s");
criteria1.add(Restrictions.eq("s.State", "Open"));


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 5:32 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
stlecho wrote:
The reason why I am using an exists subquery is because a Notice can have multiple 'Open' Statusses. In the case where a Notice has 2 'Open' statusses, an inner join returns 2 Notices whereas an exists subquery returns one Notice.

Separate issue. The join is faster than the exists (one query instead of two), and to resolve the multiple returned Notices, set your result transformer to crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 5:03 am 
Newbie

Joined: Mon Jul 21, 2008 5:12 am
Posts: 13
Could you propouse some solution for this situation:

I have an Entity which has OneToMany relation to Attribute.
I want to select all entities that have
Attribute.name = 'attr1' and Attribute.value='val1'
and
Attribute.name = 'attr2' and Attribute.value='val2'

And there are many attributes may be added...
I decided to perform such filtering with sub selects, but got this exception:
org.hibernate.MappingException: Unknown entity: null

DetachedCriteria subquery = DetachedCriteria.forClass(Attribute.class, nm);
subquery.add(Restrictions.eq("name", "attr1"));
subquery.add(Restrictions.eq("value", attr1));
subquery.add(Restrictions.eqProperty("entityId", "main.id"));
subquery.setProjection(Projections.property("id"));
criteria.add(Subqueries.exists(subquery));

Hibernate: 3.2.6.ga
SQL Server 2005


Top
 Profile  
 
 Post subject: Re: DetachedQuery, Subqueries.exists: HibernateSystemExcepti
PostPosted: Tue Sep 02, 2008 10:11 am 
Newbie

Joined: Thu Aug 28, 2008 3:31 am
Posts: 5
Do you map the
Code:
com.notary.app.taxsocnot.notice.model.Status
and let hibernate know it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2008 4:35 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Gorbush wrote:
Could you propouse some solution for this situation:

I have an Entity which has OneToMany relation to Attribute.
I want to select all entities that have
Attribute.name = 'attr1' and Attribute.value='val1'
and
Attribute.name = 'attr2' and Attribute.value='val2'

It may be possible to do via query-by-example. But it's definitely possible if you map Attribute as a <map> and use HQL:
Code:
from Entity ent
join e.Attribute attr
where attr[ :attr1 ] = :val1
and attr[ :attr2 ] = :val2
...


BTW it's usually better to start a new thread than tack onto the end of an unrelated two-year-old thread.

aaron.zhang wrote:
Do you map the
com.notary.app.taxsocnot.notice.model.Status
and let hibernate know it?

That's a dead thread. Hopefully stlecho has solved his problem by now.

_________________
Code tags are your friend. Know them and use them.


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