I have a many-to-many association in my DB and want to retrieve my objects via the Query by Criteria (QBC).
I didnĀ“t found some code performing QBC with many-to-many.
Here are my mapping-documents and my Criteria and would be very happy about some more tips or code examples. If you need more information, please tell it to me.
Regards,
Saemmy
Hibernate version: 3.0
Mapping documents:
<hibernate-mapping default-access="field">
<class name="model.BusinessDivision" lazy="true" table="BusinessDivision">
<cache usage="nonstrict-read-write"/>
<id name="id">
<generator class="assigned"/>
</id>
<version name="dbVersion"/>
<property name="description" not-null="false" length="40"/>
</class>
<class name="model.BDSubProject" lazy="true" table="EttmSubProject">
<cache usage="nonstrict-read-write"/>
<id name="pspElement" length="24">
<generator class="assigned"/>
</id>
<version name="dbVersion"/>
<set name="businessDivisions" table="Bd_BusinessDiv" fetch="join">
<cache usage="nonstrict-read-write"/>
<key column="pspElement"/>
<many-to-many class="model.BusinessDivision" column="BusinessDiv_id" fetch="select"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Criteria crit = session.createCriteria(EttmSubProject.class);
//crit.setFetchMode("businessDivisions", FetchMode.JOIN);
//crit.add(Expression.eq("businessDivision", businessDivision));
if (!StringUtils.isEmpty(pspElement))
{
crit.add(Expression.like("pspElement", pspElement);
}
crit.setMaxResults(limit);
crit.addOrder(Order.asc("pspElement"));
return crit.list();
Name and version of the database you are using:
MySQL
Oracle
|