Home > Software engineering >  Hibernate filter default condition is not working
Hibernate filter default condition is not working

Time:04-12

I am trying to integrate filters to append SQL based on logged in User role but looks like the default condition is not working.

@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "ASSET_TYPE")
@FilterDef(name="PartnerFilter",
        parameters=@ParamDef( name="isDomainRelevant", type="boolean" ), defaultCondition = "isDomainRelevant= true")
@Filter(name = "PartnerFilter")
public class AssetTypeEntity implements IdEntity<UUID> {

Hibernate code to enable filter:

            Session session = ((EntityManager) retVal).unwrap(Session.class);
            session.enableFilter("PartnerFilter");

Above code throws an exception saying isDomainRelevant is not set but if set it explicitly then it works something like below

            Session session = ((EntityManager) retVal).unwrap(Session.class);
            session.enableFilter("PartnerFilter").setParameter("isDomainRelevant", true);

CodePudding user response:

The problem was we should not define variables in @ParamDef( name="isDomainRelevant", type="boolean" ) if there are variables defined then it thinks we want to supply them dynamically. So the default condition and definition of parameters are independent of each other. We don't need to define the parameters we use in default condition. In fact what we write in default condition is a native sql query.

The below configuration works well

@FilterDef(name="PartnerFilter",
    defaultCondition = "isDomainRelevant= true")
  • Related