-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Help with setting up an association
PostPosted: Sun Mar 04, 2012 1:35 pm 
Newbie

Joined: Sun Mar 04, 2012 1:23 pm
Posts: 7
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.


Top
 Profile  
 
 Post subject: Re: Help with setting up an association
PostPosted: Mon Mar 05, 2012 1:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 11, 2009 2:26 am
Posts: 29
I just tried this, and no errors

Code:
    <class name="Person">
        <id name="id">
            <generator class="native"/>
        </id>
        <set name="messages" inverse="true" cascade="all-delete-orphan" where="published = 'true'">
            <key column="personId"></key>
            <one-to-many class="Message"/>
        </set>
    </class>
    <class name="Message">
        <id name="id">
            <generator class="native"/>
        </id>
        <many-to-one name="person" column="personId" class="Person"  >

        </many-to-one>
        <property name="published"/>

    </class>


    select
        messages0_.personId as personId0_1_,
        messages0_.id as id1_,
        messages0_.id as id1_0_,
        messages0_.personId as personId1_0_,
        messages0_.published as published1_0_
    from
        Message messages0_
    where
        (
            messages0_.published = 'true'
        ) 
        and messages0_.personId=?



NOTE, you may need quote the 'true'


Top
 Profile  
 
 Post subject: Re: Help with setting up an association
PostPosted: Mon Mar 05, 2012 3:46 am 
Newbie

Joined: Sun Mar 04, 2012 1:23 pm
Posts: 7
Thank you very much, it does work! The trick is to quote the 'true', actually. Don't know why, though... Even if the where condition should be written in SQL rather than HQL, since I'm using a PostgreSQL 9.0 database and the boolean data type is recognized by it, as well as the TRUE (without quotes, all uppercase) literal, writing where="published = TRUE" does not work.
The error given by Hibernate is something like:
ERROR: column messages0_.true does not exist
So, it seems like Hibernate is considering the right-hand side of the equals like a column rather than a literal if it's not quoted.

Thanks for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.