-->
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: HOWTO use filters with parameters
PostPosted: Fri Nov 11, 2005 9:13 am 
Beginner
Beginner

Joined: Tue Oct 18, 2005 10:47 am
Posts: 20
Location: Basel
Hello everyone !

I have a problem with filters. I know it seems quite trivial but I've been searching for a week now, and have still not found what I'm doing wrong. I'd like to do something like 'get all partners which name contains 'e', for example.

Hibernate version: The manifest shows 'Hibernate-Version: 3.0.5'

Mapping documents:

lf_partner.hbm.xml:
Code:
<hibernate-mapping auto-import="true" default-access="property" default-cascade="none" default-lazy="true" package="eserve">
<class abstract="false" dynamic-insert="false" dynamic-update="false" mutable="true" name="lf_partner" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="TABLF_PARTNER">
<id name="Id">
<generator class="assigned"/>
</id>
<property insert="true" lazy="false" name="geburtsdatum" optimistic-lock="true" unique="false" update="true"/>
<property insert="true" lazy="false" name="name" optimistic-lock="true" unique="false" update="true"/>
<property insert="true" lazy="false" name="ort" optimistic-lock="true" unique="false" update="true"/>
<property insert="true" lazy="false" name="partnernummer" optimistic-lock="true" unique="false" update="true"/>
<property insert="true" lazy="false" name="plz" optimistic-lock="true" unique="false" update="true"/>
<property insert="true" lazy="false" name="strasse" optimistic-lock="true" unique="false" update="true"/>
<property insert="true" lazy="false" name="strassennummer" optimistic-lock="true" unique="false" update="true"/>
<property insert="true" lazy="false" name="vorname" optimistic-lock="true" unique="false" update="true"/>
<filter name="SuchePartner" condition="name like ':name'"/>
</class>
<filter-def name="SuchePartner">
   <filter-param name="name" type="java.lang.String"/>
</filter-def>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
Session s = currentSession();
s.enableFilter("SuchePartner").setParameter("name","%e%");
List l = s.createQuery("FROM lf_partner").list();
int i = l.size();


Full stack trace of any exception that occurs:

Code:
org.hibernate.exception.GenericJDBCException: could not execute query
   at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
   at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.doList(Loader.java:1596)
   at org.hibernate.loader.Loader.list(Loader.java:1577)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at eserve.hibernate.HibernateRepositoryIntf.configure(HibernateRepositoryIntf.java:58)
   at eserve.components.hibernate.HibernateComponent.doStart(HibernateComponent.java:163)
   at eserve.framework.stdimpl.component.StdAbstractComponent.start(StdAbstractComponent.java:190)
   at eserve.framework.stdimpl.component.StdApplicationComponent$StartVisitor.visit(StdApplicationComponent.java:563)
   at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:365)
   at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:372)
   at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:360)
   at eserve.framework.stdimpl.component.StdApplicationComponent._start(StdApplicationComponent.java:274)
   at eserve.framework.stdimpl.component.StdApplicationComponent.start(StdApplicationComponent.java:312)
   at eserve.plugin.EservePlugin$EserveStartThread.run(EservePlugin.java:416)
Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
   at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2032)
   at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:1134)
   at org.hibernate.type.StringType.set(StringType.java:24)
   at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
   at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:44)
   at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1115)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1177)
   at org.hibernate.loader.Loader.doQuery(Loader.java:390)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
   at org.hibernate.loader.Loader.doList(Loader.java:1593)
   ... 15 more


Name and version of the database you are using: MySQL 5.0

The generated SQL (show_sql=true):
Hibernate: select lf_partner0_.Id as Id, lf_partner0_.geburtsdatum as geburtsd2_6_, lf_partner0_.name as name6_, lf_partner0_.ort as ort6_, lf_partner0_.partnernummer as partnern5_6_, lf_partner0_.plz as plz6_, lf_partner0_.strasse as strasse6_, lf_partner0_.strassennummer as strassen8_6_, lf_partner0_.vorname as vorname6_ from TABLF_PARTNER lf_partner0_ where '?' = lf_partner0_.name

Debug level Hibernate log excerpt:
INFO - Using Hibernate built-in connection pool (not for production use!)
INFO - Hibernate connection pool size: 10
INFO - autocommit mode: false
INFO - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/working
INFO - connection properties: {user=root, password=}
DEBUG - total checked-out connections: 0
DEBUG - opening new JDBC connection
DEBUG - created connection to: jdbc:mysql://localhost:3306/working, Isolation Level: 4
DEBUG - returning connection to pool, pool size: 1
INFO - Using default transaction strategy (direct JDBC transactions)
INFO - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG - opening JDBC connection
DEBUG - total checked-out connections: 0
DEBUG - using pooled JDBC connection, pool size: 0
DEBUG - select lf_partner0_.Id as Id, lf_partner0_.geburtsdatum as geburtsd2_6_, lf_partner0_.name as name6_, lf_partner0_.ort as ort6_, lf_partner0_.partnernummer as partnern5_6_, lf_partner0_.plz as plz6_, lf_partner0_.strasse as strasse6_, lf_partner0_.strassennummer as strassen8_6_, lf_partner0_.vorname as vorname6_ from TABLF_PARTNER lf_partner0_ where lf_partner0_.name like '?'
Hibernate: select lf_partner0_.Id as Id, lf_partner0_.geburtsdatum as geburtsd2_6_, lf_partner0_.name as name6_, lf_partner0_.ort as ort6_, lf_partner0_.partnernummer as partnern5_6_, lf_partner0_.plz as plz6_, lf_partner0_.strasse as strasse6_, lf_partner0_.strassennummer as strassen8_6_, lf_partner0_.vorname as vorname6_ from TABLF_PARTNER lf_partner0_ where '?' = lf_partner0_.name
DEBUG - preparing statement
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
ERROR - Parameter index out of range (1 > number of parameters, which is 0).
DEBUG - after autocommit

What I've already done :

- first : read and reread the Reference Documentation
- then I implemented a solution using <query>. I found that very handy.
For example :
Code:
<query name="SuchePartner">FROM lf_partner WHERE name like '%:name%'</query>

The first problem I had is that :name wasn't found in the list of parameters of the query. After digging in the code I found that ParserHelper.HQL_SEPARATORS did not contain the character %. Strange, but why not ? I would feed the query with "e%" instead of "e", that's not dramatic.
I modified the mapping to get something like :
Code:
<query name="SuchePartner">FROM lf_partner WHERE name like ':name'</query>

Then the filter found effectively the parameter (the HashMap actualNamedParameter in AbstractQueryImpl had the entry 'name'). But it wasn't over. The question mark are not bound to their value. I have done what I was able to (log4j modified, show-sql in the hibernate.cfg.xml...) but I didn't help. So if someone has an idea of how to deal with this problem, please let me know !

Regards,
Joe.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 11:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
don't quote parameters.

<filter name="SuchePartner" condition="name like :name"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 11:27 am 
Beginner
Beginner

Joined: Tue Oct 18, 2005 10:47 am
Posts: 20
Location: Basel
Oh. How simple.
Well then thank you a lot. Will % be supported as a separator in a more or less nearby future ? (or have I missed the point there too ?)


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.