Bk Santiago
Bk Santiago

Reputation: 1573

Hibernate Adding where clause on one-to-many mapping

I have this set on my .hbm file.

<set name="subTopicsTb" table="subtopics_tb" inverse="true" lazy="false" fetch="select">
        <key>
            <column name="topic_id" />
        </key>
        <one-to-many class="com.topics.model.SubTopics" />
    </set>

now, the default is that, hibernate get's all subtopics where the topic_id is the id. i want to filter the subtopics. adding something like where subTopics.date is not null thanks

Upvotes: 3

Views: 10887

Answers (3)

Aritz
Aritz

Reputation: 31679

This actually works, being date the column name in your DB. where attribute in the set appends raw sql to your query, so you have to specify as it is in your database and NOT HQL:

<set name="subTopicsTb" table="subtopics_tb" inverse="true" lazy="false" 
    fetch="select" where="date is not null">
    <key>
        <column name="topic_id" />
    </key>
    <one-to-many class="com.topics.model.SubTopics" />
</set>

Upvotes: 4

Ben K
Ben K

Reputation: 1

Are you using HQL to retrieve your SubTopics? If so, you can include the filter in your selection. For example:

String query = "FROM SubTopic subtopic WHERE subtopic.date != null"

Upvotes: 0

evandongen
evandongen

Reputation: 2065

Add a where clause? I don't know how you set that in an XML config .. but you can check out the annotation version here.

I found something at stackoverflow on how to add the where to your XML.

Upvotes: 1

Related Questions