Hi all
I've got a strictly Hibernate-related problem, but am still posting here as response is usually MUCH better! I've got an entity PARTICIPANT with a property birthyear, and an entity CATEGORY with a property age. Participants now have categories assigned according to the following business rules:
- If exists calculateAgeFrom(PARTICIPANT.birthyear) == CATEGORY.age, participant.category is that category
- If calculateAgeFrom(PARTICIPANT.birthyear) > max(CATEGORY.age), participant.category is NULL
- If calculateAgeFrom(PARTICIPANT.birthyear) < min(CATEGORY.age), participant.category is that category which has the min(age)
Categories have age 15 to 20. If a participant has birthyear 1980, he's got no category (NULL), birthyear 1990 gets category with age 17, birthyear 1997 gets category with age 15 (min).
I can easily implement such a getCategory() rule in a service layer, but using Hibernate formulas would give me the huge advantage (I believe) of having the correct category directly available in a JasperReport (HQL-based).
Code:
<many-to-one name="category" class="ch.want.jtrack.model.Category" fetch="select">
<formula>(select c from Category where age = (YEAR() - p.birthyear)</formula>
</many-to-one>
My participant.category property is transient and read-only (i.e. never persisted, always derived from birthyear), so I'd like something like the statement above, which fails. Can anyone help? I've seen the Hibernate tutorial on formulas, but I feel the scenario above is not covered.
Thanks a lot
Simon