I tried a few hours to solve the following problem: having a class named Group that has zero or more roles, where each role is a String, and the mapping is the following:
<set
name="roles"
table="GROUPROLES"
lazy="true"
<key column="GROUP"/>
<element column="ROLE"/>
</set>
create a Criteria based query that returns the groups that has assigned a particular role. Well, this is not posible without resorting to direct SQL, and this is a limitation of Hibernate when one of relationship sides is not a mapped class, like this case that is a String.
The workaround: use a SQL expresion with a susbselect:
Expresion.sql("EXISTS (SELECT * FROM GROUPROLES"
+ " WHERE GROUP = {alias}.GROUP"
+ " AND ROLE = ?)",
"myrole",
Hibernate.STRING)










Comments (2) |
Trackbacks (0)
Re: Hibernate criteria API limitation
Gavin | 08/07/2005, 23:27
Do you really think it is a good idea to model a Role as an instance of java.land.String?