Bear of the Year
Bear of the Year

Reputation: 333

JPA/Hibernate conditionally onetomany relationship?

I am using Hibernate Tools to generate the DAO and classes straight from database. There are two tables (table A and B) in the database, and there is a one to many relationship from A to B (multiple rows in B mapped to single A).

In the generated code of A (class A), there is a collection of class B, which reflects the one to many relationship. However, I don't need all the rows of B that belong to A to be in the result (say, I only want rows from B where column x is NULL). I don't see how this can be achieved. Any idea?

Thanks a lot!

Upvotes: 5

Views: 2279

Answers (2)

Bozho
Bozho

Reputation: 597362

You can use the @Where annotation

Upvotes: 6

Balint Pato
Balint Pato

Reputation: 1549

I suggest you using inheritance to solve the problem not a "flag" mechanism (i.e. using a "field value is null") as such, and then you'll have real OneToMany.

Though you could use Filters - the problem with this is that it's Hibernate specific and not JPA annotation.

Upvotes: 2

Related Questions