-->
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.  [ 4 posts ] 
Author Message
 Post subject: cant retrieve records from db
PostPosted: Wed Mar 28, 2007 11:53 am 
Newbie

Joined: Wed Mar 28, 2007 11:27 am
Posts: 2
hi, i am having problems retrieving records from my db.

Hibernate version: 3.2
MySQL version: 5.0.37
MySQL connector: 5.0.5

i created the following query on a table in my db...

Code:
public XXX findByName(String name) {
      try {
         XXX instance = (XXX ) sessionFactory.openSession().createCriteria(
               XXX.class, name).add(Expression.like("name", name)).uniqueResult();
         return instance;
      } catch (RuntimeException re) {
         log.error("findByName failed", re);
         throw re;
      }


when i run with queries with parameters "hihi." or "hello world", i got the following error...

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at org.hibernate.util.StringHelper.cleanAlias(StringHelper.java:356)
at org.hibernate.util.StringHelper.generateAliasRoot(StringHelper.java:337)
at org.hibernate.util.StringHelper.generateAlias(StringHelper.java:319)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaSQLAliasMap(CriteriaQueryTranslator.java:236)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:58)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:305)
at XXX.findByName(XXX.java:27)
at XXXTest.main(XXX.java:12)


however when i run with parameters "hihi" or "helloworld", it works fine.

i tried changing to the following but it also throw the same error.

Code:
XXXinstance = (XXX) sessionFactory.openSession().createCriteria(
               XXX.class, name).add(Restrictions.eq("name", name)).uniqueResult();


how do i fix this?? thanks in adv...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 12:08 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
You should include "%" wild card character for LIKE operations. Turn on the "hibernate.show_sql" parameter and see the SQL being generated.

To solve your problem, here's what you would have to do:


public XXX findByName(String name) {
try {
XXX instance = (XXX ) sessionFactory.openSession().createCriteria(
XXX.class, name).add(Expression.like("name", name+"%")).uniqueResult();
return instance;
} catch (RuntimeException re) {
log.error("findByName failed", re);
throw re;
}


Again depending on your requirement you can add the "%" to name prior to binding in the Criteria query.

_________________
--------------
Don't forget to Rate the post


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 12:48 pm 
Newbie

Joined: Wed Mar 28, 2007 11:27 am
Posts: 2
hmmm... it still doesn't work. i changed it to

Expression.like("name", "%" + name + "%")).uniqueResult();

and it still shows the same error. there isnt any sql generated shown when i turn on the "hibernate.show_sql". however if it runs fine, the sql is

Hibernate: select this_.id as id0_0_, this_.name as name0_0_ from XXX this_ where this_.name like ?

btw, "hihi." with the period is a valid entry in the db which i wish to query...

the following is the debug log from log4j

00:39:46,265 INFO Environment:509 - Hibernate 3.2.2
00:39:46,265 INFO Environment:542 - hibernate.properties not found
00:39:46,265 INFO Environment:676 - Bytecode provider name : cglib
00:39:46,265 INFO Environment:593 - using JDK 1.4 java.sql.Timestamp handling
00:39:46,328 INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
00:39:46,328 INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml

// mapping class removed

00:39:46,984 INFO Configuration:1541 - Configured SessionFactory: SessionFactory

// mapping collection removed

00:39:47,031 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
00:39:47,031 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 10
00:39:47,031 INFO DriverManagerConnectionProvider:45 - autocommit mode: false

// mysql connection properties removed

00:39:47,453 INFO SettingsFactory:89 - RDBMS: MySQL, version: 5.0.37-community-nt
00:39:47,453 INFO SettingsFactory:90 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.5 ( $Date: 2007-03-01 00:01:06 +0100 (Thu, 01 Mar 2007) $, $Revision: 6329 $ )
00:39:47,468 INFO Dialect:152 - Using dialect: org.hibernate.dialect.MySQLDialect
00:39:47,468 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
00:39:47,468 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
00:39:47,468 INFO SettingsFactory:143 - Automatic flush during beforeCompletion(): disabled
00:39:47,468 INFO SettingsFactory:147 - Automatic session close at end of transaction: disabled
00:39:47,468 INFO SettingsFactory:154 - JDBC batch size: 15
00:39:47,468 INFO SettingsFactory:157 - JDBC batch updates for versioned data: disabled
00:39:47,468 INFO SettingsFactory:162 - Scrollable result sets: enabled
00:39:47,484 INFO SettingsFactory:170 - JDBC3 getGeneratedKeys(): enabled
00:39:47,484 INFO SettingsFactory:178 - Connection release mode: auto
00:39:47,484 INFO SettingsFactory:202 - Maximum outer join fetch depth: 2
00:39:47,484 INFO SettingsFactory:205 - Default batch fetch size: 1
00:39:47,484 INFO SettingsFactory:209 - Generate SQL with comments: disabled
00:39:47,484 INFO SettingsFactory:213 - Order SQL updates by primary key: disabled
00:39:47,484 INFO SettingsFactory:382 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
00:39:47,484 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
00:39:47,484 INFO SettingsFactory:221 - Query language substitutions: {}
00:39:47,484 INFO SettingsFactory:226 - JPA-QL strict compliance: disabled
00:39:47,484 INFO SettingsFactory:231 - Second-level cache: enabled
00:39:47,484 INFO SettingsFactory:235 - Query cache: disabled
00:39:47,484 INFO SettingsFactory:369 - Cache provider: org.hibernate.cache.NoCacheProvider
00:39:47,484 INFO SettingsFactory:250 - Optimize cache for minimal puts: disabled
00:39:47,484 INFO SettingsFactory:259 - Structured second-level cache entries: disabled
00:39:47,484 INFO SettingsFactory:279 - Echoing all SQL to stdout
00:39:47,484 INFO SettingsFactory:286 - Statistics: disabled
00:39:47,484 INFO SettingsFactory:290 - Deleted entity synthetic identifier rollback: disabled
00:39:47,484 INFO SettingsFactory:305 - Default entity-mode: pojo
00:39:47,500 INFO SettingsFactory:309 - Named query checking : enabled
00:39:47,515 INFO SessionFactoryImpl:161 - building session factory
00:39:47,906 INFO SessionFactoryObjectFactory:86 - Factory name: SessionFactory
00:39:47,906 INFO NamingHelper:26 - JNDI InitialContext properties:{}
00:39:47,906 WARN SessionFactoryObjectFactory:98 - Could not bind factory to JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52)
at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)

00:39:47,953 ERROR XXXHome:40 - findByName failed
java.lang.ArrayIndexOutOfBoundsException: 0
at org.hibernate.util.StringHelper.cleanAlias(StringHelper.java:356)
at org.hibernate.util.StringHelper.generateAliasRoot(StringHelper.java:337)
at org.hibernate.util.StringHelper.generateAlias(StringHelper.java:319)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaSQLAliasMap(CriteriaQueryTranslator.java:236)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:58)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:305)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 1:01 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Be prepared to slap yourself in the forehead...

Code:
public XXX findByName(String name) {
      try {
         XXX instance = (XXX ) sessionFactory
              .openSession()
              .createCriteria(XXX.class, name) <============= Oops!
              .add(Expression.like("name", name))
              .uniqueResult();
         return instance;
      } catch (RuntimeException re) {
         log.error("findByName failed", re);
         throw re;
      }


You are aliasing the Criteria with the parameter you are passing in. Guess what happens when you pass in "hihi."? Kablammo! Just get rid of the alias on the createCriteria, it is not needed.


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