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()));
});
}
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();
}
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();
}
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);
});
}
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());
});
}
Aggregations