use of org.hibernate.orm.test.jpa.metamodel.Customer 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();
}
use of org.hibernate.orm.test.jpa.metamodel.Customer in project hibernate-orm by hibernate.
the class QueryBuilderTest method testConstructor.
@Test
public void testConstructor() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Customer> cquery = cb.createQuery(Customer.class);
Root<Customer> customer = cquery.from(Customer.class);
EntityType<Customer> Customer_ = customer.getModel();
cquery.select(cb.construct(Customer.class, customer.get(Customer_.getSingularAttribute("id", String.class)), customer.get(Customer_.getSingularAttribute("name", String.class))));
TypedQuery<Customer> tq = em.createQuery(cquery);
tq.getResultList();
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Customer in project hibernate-orm by hibernate.
the class QueryBuilderTest method testMultiselectWithPredicates.
@Test
@TestForIssue(jiraKey = "HHH-8699")
public void testMultiselectWithPredicates() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.createQuery("select (c.id, c.name), c.age from Customer c").getResultList();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Customer> r = cq.from(Customer.class);
cq.multiselect(r.get(Customer_.id), r.get(Customer_.name), cb.concat("Hello ", r.get(Customer_.name)), cb.isNotNull(r.get(Customer_.age)));
TypedQuery<Tuple> tq = em.createQuery(cq);
tq.getResultList();
em.getTransaction().commit();
em.close();
}
use of org.hibernate.orm.test.jpa.metamodel.Customer in project hibernate-orm by hibernate.
the class CriteriaCompilingTest method setUp.
@Before
public void setUp() {
TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
Customer customer = new Customer();
customer.setId("id");
customer.setName(" David R. Vincent ");
entityManager.persist(customer);
customer = new Customer();
customer.setId("id2");
customer.setName("R Vincent");
entityManager.persist(customer);
});
}
use of org.hibernate.orm.test.jpa.metamodel.Customer in project hibernate-orm by hibernate.
the class CriteriaCompilingTest method testTrim.
@Test
public void testTrim() {
final String expectedResult = "David R. Vincent";
TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<String> cquery = cb.createQuery(String.class);
Root<Customer> cust = cquery.from(Customer.class);
// Get Metamodel from Root
EntityType<Customer> Customer_ = cust.getModel();
cquery.where(cb.equal(cust.get(Customer_.getSingularAttribute("name", String.class)), cb.literal(" David R. Vincent ")));
cquery.select(cb.trim(CriteriaBuilder.Trimspec.BOTH, cust.get(Customer_.getSingularAttribute("name", String.class))));
TypedQuery<String> tq = entityManager.createQuery(cquery);
String result = tq.getSingleResult();
Assert.assertEquals("Mismatch in received results", expectedResult, result);
});
}
Aggregations