This page looks plain and unstyled because you're using a non-standard compliant browser. To see it in its best form, please upgrade to a browser that supports web standards. It's free and painless.

Robert Marcano's blog
Get Firefox Use OpenOffice.org hacker emblem

Hibernate criteria API limitation

robert | 07 July, 2005 23:30

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

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?

Yes

Robert | 09/07/2005, 03:56

In my situation because the design has evolved from may reimplemetations, from AlphaBasic, C, Smalltalk and now Java and a role never required nothing more than a String. Why waste an entire class when in 17 years never was required another property? :-P

And remember, if it can be mapped, it must be posible to being Queried

Add comment

 authimage
 
Valid XHTML 1.0 Strict and CSS.
Copyright 2003-2007 Robert Marcano. All Rights Reserved
Powered by LifeType
Original Design by Book of Styles