-->
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.  [ 2 posts ] 
Author Message
 Post subject: No Exception thrown by Session.createCriteria
PostPosted: Fri Jan 09, 2009 1:32 am 
Newbie

Joined: Thu Jan 08, 2009 11:51 pm
Posts: 1
Hi

I have found a peculiar thing in Hibernate :

Session.createCriteria(Class persistentClass) doesn't throw any exception even if class is not loaded in the Session through mappings file.

e.g.
Code:

List<TempVO> cbdList = new ArrayList<TempVO>();
      String cbd = null;
      Session session = null;
      try {
         session = HibernateUtil.currentSession();
         Criteria currentCriteria = session
               .createCriteria(TempVO.class);
         currentCriteria.add(Restrictions.eq("is_current", 'Y'));
         cbdList = currentCriteria.list();


returns blank list even though TempVO class is not specified in mapping.hbm.xml


Is there any way we can capture this type of error?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 6:16 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
when we are query DB without mapping resource in hibernate.cfg.xml throu HQL, it shows Error. but in Criteria it doesn't through any error.

It's a sample code and it's stack Trace.

Configuration config=new Configuration();
config.configure("/hibernate.cfg.xml");
SessionFactory sf=config.buildSessionFactory();
Session session=sf.openSession();
Criteria c=session.createCriteria(Report1.class);
List test=c.list();
for(int i=0;i<test.size();i++)
System.out.println(test.get(i));
Iterator results=session.createQuery("from Report1 r").iterate();
while(results.hasNext())
{
Report1 r=(Report1)results.next();
System.out.println(r.getName());
}

Stack Tarce
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Report1 is not mapped [from Report1 r]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at testing.main(testing.java:29)

_________________
If u feel it will help you, don't forget to rate me....


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