Reputation: 2312
ClassA {
@Id
@GeneratedValue
@Column(name = "a_id")
private long id;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "classA")
private Set<ClassB> classBs;
}
ClassB {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "a_id")
private ClassA classA;
@JoinColumn(name = "size")
private int size;
}
How can I add constraint to ClassA's classBs when eager/lazy load it?
For example, I only want to load ClassB where size > 10 into ClassA's Set classBs.
I know that I can order the Set by using annotation @OrderBy(value = "size ASC"), but can I add constraints other than Order?
Can I achieve it using Hibernate?
I am able to do it in Laravel(a PHP framework). Eager Load Constraints in http://laravel.com/docs/eloquent#eager-loading.
Upvotes: 1
Views: 481
Reputation: 5429
Have a look at Hibernate Filters
:
Documentation
Sample
There is one thing that you should notice and that is the filters are not applied to the entity permanently and you need to enable them whenever you would like to use them:
session.enableFilter("myFilter");
Upvotes: 1
Reputation: 3475
Look into Filter
and FilterDef
, here is documentation
FilterDef
with conditionFilter
with name referred to filter defined at classB side.Look this answer for example.
Upvotes: 0