use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class SingularAttributeJoinTest method testEntityModeMapJoinCriteriaQuery.
@Test
public void testEntityModeMapJoinCriteriaQuery(EntityManagerFactoryScope scope) {
scope.inEntityManager(entityManager -> {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
jakarta.persistence.metamodel.EntityType distributionEntity = getEntityType(scope, "Distribution");
From distributionFrom = criteriaQuery.from(distributionEntity);
From policyJoin = distributionFrom.join("policy");
Path policyId = policyJoin.get("policyId");
criteriaQuery.select(policyId);
TypedQuery typedQuery = entityManager.createQuery(criteriaQuery);
});
}
use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class CrtiteriaGetResultTypeTest method testEntityResultType.
@Test
public void testEntityResultType(EntityManagerFactoryScope scope) {
scope.inEntityManager(entityManager -> {
final CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery(Person.class);
final Class resultType = query.getResultType();
assertThat(resultType, notNullValue());
assertEquals(Person.class.getName(), resultType.getName());
});
}
use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class CrtiteriaGetResultTypeTest method testObjectResultType.
@Test
public void testObjectResultType(EntityManagerFactoryScope scope) {
scope.inEntityManager(entityManager -> {
final CriteriaQuery query = entityManager.getCriteriaBuilder().createQuery();
final Class resultType = query.getResultType();
assertThat(resultType, notNullValue());
assertEquals(Object.class.getName(), resultType.getName());
});
}
use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class QueryHintSQLServer2012Test method testQueryHint.
@Test
public void testQueryHint(SessionFactoryScope scope) {
Department department = new Department();
department.name = "Sales";
Employee employee1 = new Employee();
employee1.department = department;
Employee employee2 = new Employee();
employee2.department = department;
List result = scope.fromSession(session -> {
session.getTransaction().begin();
session.persist(department);
session.persist(employee1);
session.persist(employee2);
try {
session.getTransaction().commit();
session.clear();
// test Query w/ a simple SQLServer2012 optimizer hint
session.getTransaction().begin();
Query query = session.createQuery("FROM QueryHintSQLServer2012Test$Employee e WHERE e.department.name = :departmentName").addQueryHint("MAXDOP 2").setParameter("departmentName", "Sales");
List results = query.list();
session.getTransaction().commit();
session.clear();
assertEquals(2, results.size());
assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("OPTION (MAXDOP 2)"));
QueryHintTestSQLServer2012Dialect.resetProcessedSql();
// test multiple hints
session.getTransaction().begin();
query = session.createQuery("FROM QueryHintSQLServer2012Test$Employee e WHERE e.department.name = :departmentName").addQueryHint("MAXDOP 2").addQueryHint("CONCAT UNION").setParameter("departmentName", "Sales");
results = query.list();
session.getTransaction().commit();
session.clear();
assertEquals(results.size(), 2);
assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("MAXDOP 2"));
assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("CONCAT UNION"));
QueryHintTestSQLServer2012Dialect.resetProcessedSql();
// ensure the insertion logic can handle a comment appended to the front
session.getTransaction().begin();
query = session.createQuery("FROM QueryHintSQLServer2012Test$Employee e WHERE e.department.name = :departmentName").setComment("this is a test").addQueryHint("MAXDOP 2").setParameter("departmentName", "Sales");
results = query.list();
session.getTransaction().commit();
session.clear();
assertEquals(results.size(), 2);
assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("OPTION (MAXDOP 2)"));
QueryHintTestSQLServer2012Dialect.resetProcessedSql();
// test Criteria
session.getTransaction().begin();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Employee> criteria = criteriaBuilder.createQuery(Employee.class);
Root<Employee> root = criteria.from(Employee.class);
Join<Object, Object> departement = root.join("department", JoinType.INNER);
criteria.select(root).where(criteriaBuilder.equal(departement.get("name"), "Sales"));
// Criteria criteria = s.createCriteria( Employee.class ).addQueryHint( "MAXDOP 2" ).createCriteria( "department" )
// .add( Restrictions.eq( "name", "Sales" ) );
// results = criteria.list();
Query<Employee> criteriaQuery = session.createQuery(criteria);
criteriaQuery.addQueryHint("MAXDOP 2");
results = criteriaQuery.list();
session.getTransaction().commit();
return results;
} finally {
if (session.getTransaction().isActive()) {
session.getTransaction().rollback();
}
}
});
assertEquals(result.size(), 2);
assertTrue(QueryHintTestSQLServer2012Dialect.getProcessedSql().contains("OPTION (MAXDOP 2)"));
}
use of jakarta.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.
the class QueryCacheTest method testGetByCompositeId.
@Test
@TestForIssue(jiraKey = "HHH-4459")
public void testGetByCompositeId(SessionFactoryScope scope) {
scope.inSession(session -> {
session.beginTransaction();
try {
session.persist(new EntityWithCompositeKey(PK));
Query query = session.createQuery("FROM EntityWithCompositeKey e WHERE e.pk = :pk");
query.setCacheable(true);
query.setParameter("pk", PK);
assertEquals(1, query.list().size());
} finally {
session.getTransaction().rollback();
}
});
scope.inSession(session -> {
session.beginTransaction();
try {
EntityWithStringCompositeKey entity = new EntityWithStringCompositeKey();
StringCompositeKey key = new StringCompositeKey();
key.setAnalog("foo1");
key.setDevice("foo2");
key.setDeviceType("foo3");
key.setSubstation("foo4");
entity.setPk(key);
session.persist(entity);
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<EntityWithStringCompositeKey> criteria = criteriaBuilder.createQuery(EntityWithStringCompositeKey.class);
Root<EntityWithStringCompositeKey> root = criteria.from(EntityWithStringCompositeKey.class);
criteria.where(criteriaBuilder.equal(root.get("pk"), key));
session.createQuery(criteria).setCacheable(true);
assertEquals(1, session.createQuery(criteria).list().size());
// Criteria c = s.createCriteria(
// EntityWithStringCompositeKey.class ).add( Restrictions.eq(
// "pk", key ) );
// c.setCacheable( true );
// assertEquals( 1, c.list().size() );
} finally {
session.getTransaction().rollback();
}
});
}
Aggregations