First a small sketch of how the situation is :
I have a criteria that works, but now something needs to be added because the business changed.
Now the new thing needs to be added as an OR function over all the rest that already existed.
Example :
old : SELECT .... FROM .... WHERE A AND B AND C
new : SELECT .... FROM .... WHERE (A AND B AND C) OR D
This wouldn't be a problem if it where all normal restrictions. But with the use of Examples it seems to give a problem.
The code is just an altered version of my original code, because I can't place the original here, company policy.
Code:
Old code :
Criteria criteria = inSession.createCriteria(SomeObject1.class);
criteria.add(Restrictions.isNotNull("value1"));
criteria.add(Restrictions.eq("test", value2));
SomeObject2 object2 = new SomeObject2();
Example example1 = Example.create(object2).ignoreCase().enableLike(MatchMode.ANYWHERE);
criteria.createCriteria("someObject2").add(example1);
New code :
Criteria criteria = inSession.createCriteria(SomeObject1.class);
Conjunction conjunction1 = Restrictions.conjunction();
Conjunction conjunction2 = Restrictions.conjunction();
Disjunction disjunction = Restrictions.disjunction();
disjunction.add(conjunction1).add(conjunction2);
criteria.add(disjunction);
conjunction1.add(Restrictions.isNotNull("value1"));
conjunction1.add(Restrictions.eq("test", value2));
SomeObject2 object2 = new SomeObject2();
Example example1 = Example.create(object2).ignoreCase().enableLike(MatchMode.ANYWHERE);
// Problem is here how do you add that example to a conjunction, where you need to define that the example is of type "SomeObject2"?
//criteria.createCriteria("someObject2").add(example1); //Old code before I added conjuctions
conjunction1.add(example1); // Gives a ClassCastException
// New Code the reason why I added conjuctions
SomeObject3 object3 = new SomeObject3();
Example example2 = Example.create(object3).ignoreCase().enableLike(MatchMode.ANYWHERE);
// Same problem here how do you define the type?
//criteria.createCriteria("someObject3").add(example2);
conjunction2.add(example2); // Gives a ClassCastException
So the question is how do you define, for an example you add to a conjunction, what the type of object it is?
Is this possible or is there an other solution?