Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
I am brand new to hibernate, and fairly new to Java.
I am attempting to execute a query that finds ClassBeans that should be visible to a user based on his groups. the classBean has a collection of groups mapped as a set , my method will be passed an ArrayList of Groups. How do I set up the query? I have it working for one group, but cannot get it right with a collection. The code posted is working for the single group, I have an ArrayList I would like to substitute. Or do I need to iterate through the list ?
Hibernate version:
3.05
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="ClassBean" table="TRAINING_CLASSES" lazy="false">
<id name="classId" type="long" column="CLASS_ID" >
<generator class="native">
</generator>
</id>
<timestamp name="classModifyOn" unsaved-value="undefined"/>
<property name="classModifyBy"/>
<property name="classActiveFlag" type="boolean"/>
<property name="classCapacity" type="integer"/>
<property name="classCurrEnroll" type="integer"/>
<property name="classMaxEnroll" type="integer"/>
<property name="classEnd" type="timestamp"/>
<property name="classStart" type="timestamp"/>
<many-to-one name="classCourse"
class="com.joann.registration.beans.CourseBean"
column="COURSE_ID"
cascade="all" />
<many-to-one name="classLocation"
class="com.joann.registration.beans.LocationBean"
column="LOCATION_ID"
cascade="all"/>
<set name="classGroups"
table="TRAINING_GROUPS"
lazy="false"
cascade="save-update">
<key column="CLASS_ID"/>
<element type="string" column="GROUP"/>
</set>
<set name="classPayTypes"
table="TRAINING_PAYTYPES"
lazy="false"
cascade="save-update">
<key column="CLASS_ID"/>
<element type="string" column="PAYTYPE"/>
</set>
<property name="classShareFlag"/>
<property name="classCostAmount" type="big_decimal">
<column name="classCostAmount" sql-type="NUMERIC(11, 2)" not-null="true"/>
</property>
<set name="students" inverse="true" lazy="false">
<key column="CLASS_ID"/>
<one-to-many class="StudentBean"/>
</set>
</class>
<query name="ClassbyId">
<![CDATA[
from ClassBean as c
where class_id = :cid
]]>
</query>
<query name="Classbydate">
<![CDATA[
from ClassBean as c
where classstart > :currdate
]]>
</query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public static List selectClasses() {
List clsList=null;
Transaction insTx = null;
Session sess = null;
try {
sess =TrainingHibernateDAO.getHibSession();
insTx = sess.beginTransaction();
ArrayList userGroups = new ArrayList();
userGroups.add("Gold");
userGroups.add("Silver");
Query test = sess.createQuery("from ClassBean as cls " +
"where cls.classStart > :currdate " +
"and :groupname in elements (cls.classGroups)");
test.setTimestamp("currdate", new Timestamp(System.currentTimeMillis()));
test.setString("groupname","Gold");
clsList = test.list();
insTx.commit();
sess.flush();
}catch (HibernateException he) {
System.out.println(he.toString());
insTx.rollback();
}catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
insTx.rollback();
}
finally {
sess.close();
return clsList;
}
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
DB2 7.2 on AIX
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
[code][/code]