How to query method fields in Hibernate (OR/M)


EJB design: How to query method fields in Hibernate (OR/M)

  1. How to query method fields in Hibernate (OR/M) (1 messages)

    I'm confused about performing queries with O/R Mappers, when properties don't map directly to database fields.

    Cases like:
    * calculated fields ( eg person.getAgeOn(today) -> method returns result of calculation, not simple accessor)
    * different subclasses implement field differently (some may store it directly, some may calculate it)
    * Methods like getZipCode() { return getAddress().getZipCode(); } which access the fields of a member object

    Let's say I need to get all people whose age as of today is >18, or all people whose zipcode is 01234.

    I can think of two ways:
    A) Retrieve all the objects, and run the method on each one.
    B) Write special SQL to do the query.

    The obvious downside to A is performance.

    The obvious downside to B is that it ruins all the benefits of OO encapsulation, demeter, inheritance, etc. I need to incorporate all of that knoweledge into the finder methods. When different subclasses implement the field differently, this gets into some pretty messy SQL as well.

    How do you J2EE veterans handle it? How does Hibernate's HQL handle queries on non-accessor fields?

    Threaded Messages (1)

  2. hibernate formulas[ Go to top ]

    You can use formula's in hibernate, for example getAge() would be something like:
    <column name="age" formula="sysdate-dob"/>

    now you can query for age:
    select p from Person p where age > :age

    I don't know what kind of accessors you use for zipcodes, but you can use something similar..