Hi there,
I'm quite new to Hibernate. I'm having a problem to define an association in my .hbm.xml file. Suppose I have the following entities:
- a Person
- a Message
One person can have many messages: that is, a Message has a foreign key to Person (e.g.: Message.personId => Person.id).
Now, I want to map the inverse association in Person, so that Person.getMessages() returns all the messages of the person. So, in Person.hbm.xml I do:
Code:
<set name="messages" inverse="true" cascade="all-delete-orphan">
<key column="personId" />
<one-to-many class="Message" />
</set>
So far so good. Now, I want to change Person.getMessages() behaviour a bit. Suppose Message has a boolean flag like Message.published and that I want that Person.getMessages() returns only those messages that have published=true. This is a very basic scenario. I searched the Internet a lot, I found a couple of people having a very similar problem, but I couldn't understand the correct way to go. What I understood is that something like the following should work:
Code:
<set name="messages" inverse="true" cascade="all-delete-orphan" where="published = true">
<key column="personId" />
<one-to-many class="Message" />
</set>
but this does not work. Hibernate gives some org.hibernate.util.JDBCExceptionReporter errors (which are quite incomprehensible to me... if I didn't know for sure the problem is here, I wouldn't even have thought it was because of this!) and Person.getMessages() seems to be always empty.
So, what is the right way to do so?
I read about filters, but I really don't like the fact that I have to set parameters on the session to enable the filtering. In my case, the filtering criteria is fixed an very very simple, so I really hope to be able to enclose the whole thing in the .hbm.xml file.
Thanks in advance,
Mauro.