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.
You want to apply the filter condition to the association table, so, use
@FilterJoinTableand use property name, not database column name: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: