shashikanthb
shashikanthb

Reputation: 379

How to apply hibernate filter for map datatype?

Is there a way to apply filter on map type attribute in a class using annotations? I have done something like below but the filter is not getting applied

public class EntityA
{
@id
@GeneratedValue
@Column(name="id")
private Long id;

@OneToMany(mappedBy="EntityA")
@MapKeyColumn(name = "entityB_id")
@Filter(name = "percentFilter", condition="MODEL_PERCENT> :percent")
private Map<Long, EntityB> entityBMap;
} 

public class EntityB
{
@Column(name = "MODEL_PERCENT")
private BigDecimal modelPercent;

@ManyToOne
@joincolumn(name="entityA_id") 
private EntityA entityA; 
}

But if i change the map attribute to list as below, the filter works

@OneToMany(mappedBy="EntityA")
@MapKeyColumn(name = "entityB_id")
@Filter(name = "percentFilter", condition="MODEL_PERCENT> :percent")
private Map<Long, EntityB> entityBMap;

to

@OneToMany(mappedBy="EntityA")
@JoinColumn(name = "entityA_id")
@Filter(name = "percentFilter", condition="MODEL_PERCENT> :percent")
private List<EntityB> entityBList;

So i guess i need some help on annotating the map attribute such that the filter works. Any suggestion or sample code would be of great help. Thanks.

Upvotes: 0

Views: 1276

Answers (1)

jelies
jelies

Reputation: 9290

You want to apply the filter condition to the association table, so, use @FilterJoinTable and use property name, not database column name:

@OneToMany(mappedBy="EntityA")
@MapKeyColumn(name = "entityB_id")
@FilterJoinTable(name = "percentFilter", condition="modelPercent > :percent")
private Map<Long, EntityB> entityBMap;

Check Filters of Hibernate Annotations documentation, you can find there an example.

Hope it helps.

EDIT:

Don't forget to enable filtering in your Hibernate Session:

session.enableFilter("percentFilter").setParameter("percent", "some-value");

Upvotes: 1

Related Questions