HHH-16899 add restrict() to CriteriaDefinition
This commit is contained in:
parent
dc22773a9f
commit
e125a818e3
|
@ -161,6 +161,12 @@ public abstract class CriteriaDefinition<R>
|
|||
return entityManager.createQuery( query );
|
||||
}
|
||||
|
||||
@Incubating
|
||||
public JpaCriteriaQuery<R> restrict(Predicate predicate) {
|
||||
final JpaPredicate existing = getRestriction();
|
||||
return existing == null ? where( predicate ) : where( existing, predicate );
|
||||
}
|
||||
|
||||
@Override
|
||||
public JpaCriteriaQuery<R> select(Selection<? extends R> selection) {
|
||||
return query.select(selection);
|
||||
|
|
|
@ -47,6 +47,13 @@ public class CriteriaDefinitionTest {
|
|||
orderBy(asc(message.get("text")));
|
||||
}};
|
||||
|
||||
var query4 = new CriteriaDefinition<>(factory, Message.class) {{
|
||||
var message = from(Message.class);
|
||||
restrict(like(message.get("text"), "hell%"));
|
||||
restrict(message.get("id").equalTo(1));
|
||||
orderBy(asc(message.get("id")));
|
||||
}};
|
||||
|
||||
scope.inSession(session -> {
|
||||
var idAndText = session.createSelectionQuery(query1).getSingleResult();
|
||||
assertNotNull(idAndText);
|
||||
|
@ -61,6 +68,10 @@ public class CriteriaDefinitionTest {
|
|||
var messages = session.createSelectionQuery(query3).getResultList();
|
||||
assertEquals(2,messages.size());
|
||||
|
||||
var msg = session.createSelectionQuery(query4).getSingleResult();
|
||||
assertNotNull(msg);
|
||||
assertEquals(1L,msg.id);
|
||||
assertEquals("hello",msg.text);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -91,6 +102,13 @@ public class CriteriaDefinitionTest {
|
|||
orderBy(asc(message.get("text")));
|
||||
}};
|
||||
|
||||
var query4 = new CriteriaDefinition<>(factory, Message.class) {{
|
||||
var message = from(Message.class);
|
||||
restrict(like(message.get("text"), "hell%"));
|
||||
restrict(message.get("id").equalTo(1));
|
||||
orderBy(asc(message.get("id")));
|
||||
}};
|
||||
|
||||
scope.inTransaction(entityManager -> {
|
||||
var idAndText = entityManager.createQuery(query1).getSingleResult();
|
||||
assertNotNull(idAndText);
|
||||
|
@ -105,6 +123,10 @@ public class CriteriaDefinitionTest {
|
|||
var messages = entityManager.createQuery(query3).getResultList();
|
||||
assertEquals(2,messages.size());
|
||||
|
||||
var msg = entityManager.createQuery(query4).getSingleResult();
|
||||
assertNotNull(msg);
|
||||
assertEquals(1L,msg.id);
|
||||
assertEquals("hello",msg.text);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue