HHH-16899 add restrict() to CriteriaDefinition

This commit is contained in:
Gavin King 2023-07-13 20:26:44 +02:00
parent dc22773a9f
commit e125a818e3
2 changed files with 28 additions and 0 deletions

View File

@ -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);

View File

@ -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);
});
}