I have two tables, plan
and account
, and I also have two classes, Plan
and Account
, that represent these tables.
Account
has fields, id
, plan_id
, vdn
and type
. Plan
has fields, id
and a few other irrelevant ones. It's structured so that a plan can be assigned to multiple accounts.
My Plan
class has a field called accounts
, which is mapped like this:
@OneToMany(mappedBy = "plan", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@OrderBy(value = "type")
@LazyCollection(value = LazyCollectionOption.FALSE)
private List<Account> accounts;
So a plan has a list of all the accounts it's associated with. What I'm trying to do is filter the list of plans using vdns
that belong to its corresponding accounts.
For example, if one plan belongs to an account with vdn
"abc" and another plan belongs to an account with vdn
"123", the user can search by the string "ab" and "12" to find them both.
Here is what I have so far:
criteria.createCriteria("accounts").add(Restrictions.in("vdn", new String[] { "abc", "123" }));
This code works, but it only matches the strings exactly. If a user enters "abc" then it will find that plan. If a user enters "ab" it will not find the plan.
So far, I've also tried something like:
criteria.createCriteria("accounts").add(Restrictions.in("vdn", new String[] { "%ab%", "%12%" }));
but that hasn't worked.
Is there a way to use the "like" restriction along with the "in" restriction?