Search in sources :

Example 1 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class HbmMappingMappedSuperclassWithVersionTest method testMetamodelContainsHbmVersion.

@Test
public void testMetamodelContainsHbmVersion() {
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        final TestEntity entity = new TestEntity();
        entity.setName("Chris");
        entityManager.persist(entity);
    });
    TransactionUtil.doInJPA(this::entityManagerFactory, entityManager -> {
        final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<TestEntity> query = builder.createQuery(TestEntity.class);
        final Root<TestEntity> root = query.from(TestEntity.class);
        assertThat(root.get("version"), is(notNullValue()));
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Example 2 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project javaee7-samples by javaee-samples.

the class MovieBean method listMovies.

public List<Movie> listMovies() {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Movie> listCriteria = builder.createQuery(Movie.class);
    Root<Movie> listRoot = listCriteria.from(Movie.class);
    listCriteria.select(listRoot);
    TypedQuery<Movie> query = em.createQuery(listCriteria);
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder)

Example 3 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project javaee7-samples by javaee-samples.

the class MovieBean method updateMovie.

public void updateMovie() {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaUpdate<Movie> updateCriteria = builder.createCriteriaUpdate(Movie.class);
    Root<Movie> updateRoot = updateCriteria.from(Movie.class);
    updateCriteria.where(builder.equal(updateRoot.get(Movie_.name), "Inception"));
    updateCriteria.set(updateRoot.get(Movie_.name), "INCEPTION");
    Query q = em.createQuery(updateCriteria);
    q.executeUpdate();
    em.flush();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Query(javax.persistence.Query) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) TypedQuery(javax.persistence.TypedQuery)

Example 4 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class FetchingTest method test.

@Test
public void test() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Department department = new Department();
        department.id = 1L;
        entityManager.persist(department);
        Employee employee1 = new Employee();
        employee1.id = 1L;
        employee1.username = "user1";
        employee1.password = "3fabb4de8f1ee2e97d7793bab2db1116";
        employee1.accessLevel = 0;
        employee1.department = department;
        entityManager.persist(employee1);
        Employee employee2 = new Employee();
        employee2.id = 2L;
        employee2.username = "user2";
        employee2.password = "3fabb4de8f1ee2e97d7793bab2db1116";
        employee2.accessLevel = 1;
        employee2.department = department;
        entityManager.persist(employee2);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        String username = "user1";
        String password = "3fabb4de8f1ee2e97d7793bab2db1116";
        Employee employee = entityManager.createQuery("select e " + "from Employee e " + "where " + "	e.username = :username and " + "	e.password = :password", Employee.class).setParameter("username", username).setParameter("password", password).getSingleResult();
        assertNotNull(employee);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        String username = "user1";
        String password = "3fabb4de8f1ee2e97d7793bab2db1116";
        Integer accessLevel = entityManager.createQuery("select e.accessLevel " + "from Employee e " + "where " + "	e.username = :username and " + "	e.password = :password", Integer.class).setParameter("username", username).setParameter("password", password).getSingleResult();
        assertEquals(Integer.valueOf(0), accessLevel);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        String username = "user1";
        String password = "3fabb4de8f1ee2e97d7793bab2db1116";
        Employee employee = entityManager.createQuery("select e " + "from Employee e " + "left join fetch e.projects " + "where " + "	e.username = :username and " + "	e.password = :password", Employee.class).setParameter("username", username).setParameter("password", password).getSingleResult();
        assertNotNull(employee);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        String username = "user1";
        String password = "3fabb4de8f1ee2e97d7793bab2db1116";
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
        Root<Employee> root = query.from(Employee.class);
        root.fetch("projects", JoinType.LEFT);
        query.select(root).where(builder.and(builder.equal(root.get("username"), username), builder.equal(root.get("password"), password)));
        Employee employee = entityManager.createQuery(query).getSingleResult();
        assertNotNull(employee);
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Test(org.junit.Test)

Example 5 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project hibernate-orm by hibernate.

the class CriteriaTest method test_criteria_from_multiple_root_example.

@Test
public void test_criteria_from_multiple_root_example() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        String address = "Earth";
        String prefix = "J%";
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
        Root<Person> personRoot = criteria.from(Person.class);
        Root<Partner> partnerRoot = criteria.from(Partner.class);
        criteria.multiselect(personRoot, partnerRoot);
        Predicate personRestriction = builder.and(builder.equal(personRoot.get(Person_.address), address), builder.isNotEmpty(personRoot.get(Person_.phones)));
        Predicate partnerRestriction = builder.and(builder.like(partnerRoot.get(Partner_.name), prefix), builder.equal(partnerRoot.get(Partner_.version), 0));
        criteria.where(builder.and(personRestriction, partnerRestriction));
        List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
        assertEquals(2, tuples.size());
    });
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Person(org.hibernate.userguide.model.Person) Partner(org.hibernate.userguide.model.Partner) Tuple(javax.persistence.Tuple) Predicate(javax.persistence.criteria.Predicate) Test(org.junit.Test)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)168 Test (org.junit.Test)121 EntityManager (javax.persistence.EntityManager)79 TestForIssue (org.hibernate.testing.TestForIssue)38 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)30 Customer (org.hibernate.jpa.test.metamodel.Customer)21 Predicate (javax.persistence.criteria.Predicate)15 Tuple (javax.persistence.Tuple)13 Person (org.hibernate.userguide.model.Person)12 List (java.util.List)11 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)11 ArrayList (java.util.ArrayList)10 Order (org.hibernate.jpa.test.metamodel.Order)10 ExecutionException (java.util.concurrent.ExecutionException)9 Message (org.apache.aries.tx.control.itests.entity.Message)6 TypedQuery (javax.persistence.TypedQuery)5 Root (javax.persistence.criteria.Root)4 Query (javax.persistence.Query)3 SkipForDialect (org.hibernate.testing.SkipForDialect)3 HashSet (java.util.HashSet)2