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.