I have two tables in a many to many relationship. User and Form
I'm trying to create something similar to this:
Code:
select * from users a, form_authors b where b.frm_id = '402882c5f7d7367100f7d736b3900002' and a.usr_id = b.usr_id and a.usr_id in ('id1','id5','id7','id31');
w/ the only exception being that I only need user data.
I've tried:
Code:
"select form.authors from test.Form form, test.User user
where user.id IN (" + uids + ")"
where uids is a comma separated list of ids. (doesn't work)
Which gives me the error:
Code:
net.sf.hibernate.QueryException: expecting 'elements' or 'indices' after: id [ select form.authors from testForm form, testUser user where user.id IN ('id1','id5','id7','id31')]
where uids is a comma separated set of ids like: 'id1','id5','id7','id31'.
I've also tried the Query by Criteria:
as:
Code:
List users = session.createCriteria(Form.class).add(Expression.eq("id",formId)).add(Expression.in("authors",searchUsers));
The latter gives me wrong # of parameters. The SQL generated is:
select this.frm_id as frm_id4_, this.frm_nam as frm_nam4_, this.frm_usg as frm_usg4_, this.bbd_id as bbd_id4_, this.dpt_id as dpt_id4_, this.lst_upd_ts as lst_upd_ts4_, this.crt_ts as crt_ts4_, this.upd_usr_id as upd_usr_id4_, this.crt_usr_id as crt_usr_id4_, bulletinbo1_.bbd_id as bbd_id0_, bulletinbo1_.bbd_nam as bbd_nam0_, bulletinbo1_.bbd_usg as bbd_usg0_, bulletinbo1_.lst_ts as lst_ts0_, bulletinbo1_.dpt_id as dpt_id0_, bulletinbo1_.lst_upd_ts as lst_upd_ts0_, bulletinbo1_.crt_ts as crt_ts0_, bulletinbo1_.upd_usr_id as upd_usr_id0_, bulletinbo1_.crt_usr_id as crt_usr_id0_, department2_.dpt_id as dpt_id1_, department2_.dpt_dsc_tx as dpt_dsc_tx1_, department2_.lst_upd_ts as lst_upd_ts1_, department2_.crt_ts as crt_ts1_, department2_.upd_usr_id as upd_usr_id1_, department2_.crt_usr_id as crt_usr_id1_, department3_.dpt_id as dpt_id2_, department3_.dpt_dsc_tx as dpt_dsc_tx2_, department3_.lst_upd_ts as lst_upd_ts2_, department3_.crt_ts as crt_ts2_, department3_.upd_usr_id as upd_usr_id2_, department3_.crt_usr_id as crt_usr_id2_, formconten4_.fc_id as fc_id3_, formconten4_.fie_nam_tx as fie_nam_tx3_, formconten4_.content as content3_ from form this left outer join bulletin_board bulletinbo1_ on this.bbd_id=bulletinbo1_.bbd_id left outer join department department2_ on bulletinbo1_.dpt_id=department2_.dpt_id left outer join department department3_ on this.dpt_id=department3_.dpt_id left outer join form_content formconten4_ on this.frm_id=formconten4_.fc_id where this.frm_id=? and this.frm_id in (?, ?)
Note, I left out some of the extra tables and attributes.
I've also been messing around with the criteria further to try:
Code:
Criteria criteria = session.createCriteria(Form.class)
.add(Expression.eq("id", formId))
.createCriteria("authors")
.add(Expression.in("id", listIds(ldapUsers)))
.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
This returned a Form which was not what I was looking for. I'm looking for the Users.
I suspect I might need a bidirectional relationship from User back to Form with an formsAuthored property to make this work.
I'm using Hibernate Version 2.1 and DB2 7.2.
Code:
<class
name="test.User"
table="users"
>
<!-- the unsaved value is set to none since checking of the database is required for the id -->
<id name="id" type="string" unsaved-value="none">
<column name="usr_id" sql-type="varchar(32)" length="32" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="firstName" type="string">
<column name="fst_nam_tx" sql-type="varchar(30)" length="30"/>
</property>
<property name="lastName" type="string" >
<column name="lst_nam_tx" sql-type="varchar(30)" length="30"/>
</property>
<property name="emailAddress" type="string">
<column name="eml_adr_tx" length="50"/>
</property>
<set name="trackings" table="tracking"
cascade="all" inverse="true" lazy="true">
<key column="usr_id"/>
<one-to-many class="test.Tracking"/>
</set>
</class>
<class
name="test.Form"
table="form"
>
<id
name="id"
type="java.lang.String" length="32" unsaved-value="null">
<column name="frm_id" sql-type="char(32)" not-null="true" />
<generator class="uuid.hex" />
</id>
<property
name="formName"
type="java.lang.String" length="50">
<column name="frm_nam" not-null="true"/>
</property>
<set name="authors" table="form_authors"
cascade="save-update" lazy="true">
<key>
<column name="frm_id" sql-type="char(32)"/>
</key>
<many-to-many class="test.User" >
<column name="usr_id" sql-type="varchar(32)"/>
</many-to-many>
</set>
</class>
Thanks for your time.
P.S. I've also tried the HQL which used:
Code:
select form.authors from test.Form form, "
+ " test.User user "
+ " where user.id IN (?)
and the attribute set was a list of IDs Still no luck