如果您使用的是Hibernate,您可以尝试使用过滤器。
class Area {
@OneToMany(...)
@Filter(name = "regionFilter")
Set<Region> regions = new HashSet<Region>();
}
@FilterDef(name = "regionFilter",
defaultCondition = "country.id = :countryId",
parameters = {@ParamDef(name = nameParameter, type = typeParameter)})
class Region {
}
要在当前Hibernate会话中启用筛选,应启用此筛选器并将所需参数放入其中:
session.enableFilter("regionFilter")
session.getEnabledFilter("regionFilter").setParameter("countryId", value);