-->
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: Using Criteria API to recreate any elements(collection)
PostPosted: Mon Feb 12, 2007 2:47 pm 
Newbie

Joined: Mon Feb 12, 2007 2:31 pm
Posts: 4
I have been searching for 2 days to try to find this. Hopefully it is simple and I have just overlooked something. I have an object (Contact) with a Set element and I would like to use the Criteria API to limit search results to those results that have one of the elements of that Set in another Set. I will post the code below.

Contact.java
Code:
private Integer id;
private String firstName;
...
private String secondarySpecialty;
private Set licenseTitles; //This is the Set I want to search
private String practiceSetting;
...


Contact.hbm.xml
Code:
<hibernate-mapping package="net.cyahpn.model">
    <class name="Contact" table="contact">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="firstName" type="java.lang.String">
            <column name="first_name" />
        </property>
        ...
        <property name="secondarySpecialty" type="java.lang.String">
            <column name="secondary_specialty" />
        </property>
        <set name="licenseTitles" table="title" lazy="false">
          <key column="contact_id"/>
          <element column="name" type="string"/>
      </set>
        <property name="practiceSetting" type="java.lang.String">
            <column name="practice_setting" />
        </property>
        ...
     </class>
</hibernate-mapping>


Here is where I want to use it. Basically, I pass in a set of filters which are Criterion and filter my results by these passed in Criterion.

Code:
Criteria criteria=this.sessionFactory.getCurrentSession()
      .createCriteria(Contact.class);
      while (filterIter.hasNext()){
         Criterion crit=(Criterion) filterIter.next();
         criteria.add(crit);
      }
      criteria.addOrder(Order.asc("lastName"));
      
      List results= criteria.list();


This is what I would like to basically accomplish (where userLicenseTitles is a Set of titles provided by the user):
Code:
filters.add(Expression.in("licenseTitles", userLicenseTitles));


But when I do, I get this error (userLicenseTitles is not null):
Quote:
Caused by: java.sql.SQLException: No value specified for parameter 2
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:1257)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:1205)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:969)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:120)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1272)
at org.hibernate.loader.Loader.doQuery(Loader.java:391)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)


Please help a first time poster. Thanks.


Top
 Profile  
 
 Post subject: 2 days later
PostPosted: Wed Feb 14, 2007 11:41 am 
Newbie

Joined: Mon Feb 12, 2007 2:31 pm
Posts: 4
I've seen that alot of you have looked at my problem but have no solution. I have found something that could be the source of the issue. I turned on the show_sql option on hibernate and found that hibernate is querying:

Code:
where this_.id in (?)


For this criterion:
Code:
Expression.in("licenseTitles", licenseTitles)


Does anyone know why it is trying to use the id field when clearly I have asked for the "licenseTitles" field? Could this be a bug in the sql creation?

Also could the reason for my previous error be that sql can't find a parameter to match the id from my collection variable I am passing in?

fyi: See the above post for the .hbm files. I think part of the issue with this is that I am using the <element> within the set definition.


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.