endrigoantonini
endrigoantonini

Reputation: 1221

How to map on Hibernate using annotations a Join with other fields with a specific value?

I have an entity A, and an entity B. The entity B is and three. And the entity A is always link with just one entity B.

Below represents this relationship as a tree:

A1
+-B1
 +-B2
 +-B3
 | +-B4
 |
 +-B5
 +-B6
   +-B7
     +-B8

And here is the data as it is stored on the database to represent the Entity B:

#    parent_id    entity_a_id    name
1    null         1              B1
2    1            1              B2
3    1            1              B3
4    3            1              B4
5    1            1              B5
6    1            1              B6
7    6            1              B7
8    7            1              B8

I want to map on the entity A to bring the root entity B, it means that I want to bring the entity B where entity_a_id is the same as the current (probably mappedBy) and the parent_id is null. Until now I didn't found how to add a custom query to a relationship (parent_id is null). Is it possible?

Upvotes: 0

Views: 2310

Answers (1)

kostja
kostja

Reputation: 61538

If I understand the requirement correctly, you want to restrict the relationship field to contain only certain entities based on criteria. In this case you might find Hibernate filters useful.

EDIT: Hibernate also features the @Where annotation for conditional joins, Take a look at This simple example

Upvotes: 1

Related Questions