AbhiShek SinGh
AbhiShek SinGh

Reputation: 11

How to filter data in Hibernate many to many mapping

I have one table user which has mapping for Groups and companies.

User{
int id;
private Set groups;
private Set companies;
}

group and companies both have one filed named "deleted" which is used to know the status of entity. I have done mapping with XML, now i want when i retrieve user record only those groups and companies will retrieve which is not deleted. (deleted=false) I am just stuck in this, so help me out.

Thank you.

Upvotes: 1

Views: 230

Answers (3)

user3520616
user3520616

Reputation: 101

In XML it would look like this:

<set name="xyz" cascade="save-update" table="xyz">
  <key>
    <column name="xyz" not-null="true" />
  </key>
  <many-to-many entity-name="xyz">
    <column name="xyz" not-null="true" />
    
    <filter name="tenantFilter" condition="xyz"></filter>
  </many-to-many>
</set>

Upvotes: 0

Learner
Learner

Reputation: 21425

One more way of doing this is using Criteria queries

Upvotes: 0

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153950

While you can use Filters, you need to manually enable those for every Session.

A much simpler approach is to use @Where:

User{
   int id;

   @Where(clause="deleted <> true")
   private Set groups;

   @Where(clause="deleted <> true")
   private Set companies;
}

This way you can filter out the deleted children.

Upvotes: 1

Related Questions