use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class TreatListJoinTest method testTreatJoin.
@Test
public void testTreatJoin() {
EntityManager em = createEntityManager();
try {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Tuple> query = cb.createTupleQuery();
final Root<TestEntity> testEntity = query.from(TestEntity.class);
final List<Selection<?>> selections = new LinkedList();
selections.add(testEntity.get("id"));
final ListJoin<TestEntity, AbstractEntity> entities = testEntity.joinList("entities", JoinType.LEFT);
entities.on(cb.equal(entities.get("entityType"), EntityA.class.getName()));
final ListJoin<TestEntity, EntityA> joinEntityA = cb.treat(entities, EntityA.class);
selections.add(joinEntityA.get("id"));
selections.add(joinEntityA.get("valueA"));
final ListJoin<TestEntity, AbstractEntity> entitiesB = testEntity.joinList("entities", JoinType.LEFT);
entitiesB.on(cb.equal(entitiesB.get("entityType"), EntityB.class.getName()));
final ListJoin<TestEntity, EntityB> joinEntityB = cb.treat(entitiesB, EntityB.class);
selections.add(joinEntityB.get("id"));
selections.add(joinEntityB.get("valueB"));
query.multiselect(selections);
final List<Tuple> resultList = em.createQuery(query).getResultList();
assertThat(resultList.size(), is(10));
} finally {
em.close();
}
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class AggregationResultTest method testSumOfIntegers.
/**
* Sum of Integers should return an Integer; note that this is distinctly different than JPAQL
*/
@Test
public void testSumOfIntegers() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Integer> criteria = builder.createQuery(Integer.class);
Root<Product> productRoot = criteria.from(Product.class);
criteria.select(builder.sum(productRoot.get(Product_.quantity)));
Object sumResult = em.createQuery(criteria).getSingleResult();
assertReturnType(Integer.class, sumResult);
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class AggregationResultTest method cleanUpTestData.
@After
public void cleanUpTestData() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.createQuery("delete Product").executeUpdate();
em.getTransaction().commit();
em.close();
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class EntitySuperclassCollectionTest method createPerson.
private PersonBase createPerson(PersonBase person, String address) {
EntityManager em = createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
person.addresses.add(new Address(address));
person = em.merge(person);
tx.commit();
return person;
}
use of javax.persistence.EntityManager in project hibernate-orm by hibernate.
the class ManipulationCriteriaTest method testJoinsAndFetchesDisallowed.
@Test
public void testJoinsAndFetchesDisallowed() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
{
try {
CriteriaDelete<Customer> deleteCriteria = builder.createCriteriaDelete(Customer.class);
Root<Customer> root = deleteCriteria.from(Customer.class);
root.join(Customer_.spouse);
em.createQuery(deleteCriteria).executeUpdate();
fail("Expected failure dues to attempt to join");
} catch (IllegalArgumentException expected) {
}
}
{
try {
CriteriaDelete<Customer> deleteCriteria = builder.createCriteriaDelete(Customer.class);
Root<Customer> root = deleteCriteria.from(Customer.class);
root.fetch(Customer_.spouse);
em.createQuery(deleteCriteria).executeUpdate();
fail("Expected failure dues to attempt to fetch");
} catch (IllegalArgumentException expected) {
}
}
{
try {
CriteriaUpdate<Customer> updateCriteria = builder.createCriteriaUpdate(Customer.class);
Root<Customer> root = updateCriteria.from(Customer.class);
root.join(Customer_.spouse);
em.createQuery(updateCriteria).executeUpdate();
fail("Expected failure dues to attempt to join");
} catch (IllegalArgumentException expected) {
}
}
{
try {
CriteriaUpdate<Customer> updateCriteria = builder.createCriteriaUpdate(Customer.class);
Root<Customer> root = updateCriteria.from(Customer.class);
root.fetch(Customer_.spouse);
em.createQuery(updateCriteria).executeUpdate();
fail("Expected failure dues to attempt to fetch");
} catch (IllegalArgumentException expected) {
}
}
em.getTransaction().commit();
em.close();
}
Aggregations