Example usage for javax.persistence.criteria Root join

List of usage examples for javax.persistence.criteria Root join

Introduction

In this page you can find the example usage for javax.persistence.criteria Root join.

Prototype

<Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create an inner join to the specified single-valued attribute.

Usage

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private Product getProduct_Mercifully() {
    return transactionTemplate.execute(new TransactionCallback<Product>() {
        @Override//from  ww  w . jav a 2s  . c  om
        public Product doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Product> productRoot = query.from(Product.class);
            Join<Product, WarehouseProductInfo> warehouseProductInfoJoin = productRoot
                    .join(Product_.warehouseProductInfo);

            query.select(productRoot).where(cb.and(cb.equal(productRoot.get(Product_.code), "tvCode"),
                    cb.gt(warehouseProductInfoJoin.get(WarehouseProductInfo_.quantity), 50)));
            return entityManager.createQuery(query).getSingleResult();
        }
    });
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * subquery(equivalent to dowhere1) : exists
 * SELECT e FROM jpa_query_employee e WHERE EXISTS (SELECT p FROM e.projects p WHERE p.name = :projectname)
 *//*from w  ww  .j a  va 2s .  c om*/
@Transactional
public void doWhere2() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);

    // subquery
    Subquery<Project> sq = c.subquery(Project.class);
    // Root<Project> p = sq.from(Project.class);
    Root<Employee> se = sq.correlate(e);
    Join<Employee, Project> p = se.join("projects");
    sq.select(p).where(cb.equal(p.get("name"), cb.parameter(String.class, "projectname")));

    //
    c.select(e).where(cb.exists(sq));
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * group by and having :// ww w. jav  a 2  s  .  c  om
 * SELECT e, COUNT(p) FROM jpa_query_employee e JOIN e.projects p GROUP BY e HAVING COUNT(p) >= 2
 */
@Transactional
public void doGroupby() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);
    Root<Employee> e = c.from(Employee.class);
    Join<Employee, Project> p = e.join("projects");
    c.multiselect(e, cb.count(p)).groupBy(e).having(cb.ge(cb.count(p), 2));

    showResult(c);
}

From source file:org.oncoblocks.centromere.jpa.test.EntrezGeneRepositoryImpl.java

public List<EntrezGene> guessGene(String keyword) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EntrezGene> query = builder.createQuery(EntrezGene.class);
    Root<EntrezGene> root = query.from(EntrezGene.class);
    query.where(builder.equal(root.get("primaryGeneSymbol"), keyword));
    List<EntrezGene> genes = entityManager.createQuery(query).getResultList();
    if (genes != null && genes.size() > 0)
        return genes;

    query = builder.createQuery(EntrezGene.class);
    root = query.from(EntrezGene.class);
    Path join = root.join("aliases");
    query.where(builder.equal(join.get("name"), keyword));
    return entityManager.createQuery(query).getResultList();
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * subquery: in//from w  w  w  .  j  ava  2s.com
 * SELECT e FROM jpa_query_employee e WHERE e IN (SELECT emp FROM jpa_query_project p JOIN p.employees pe WHERE p.name = :projectname)
 */
@Transactional
public void doWhere1() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> e = c.from(Employee.class);

    // subquery
    Subquery<Employee> sq = c.subquery(Employee.class);
    Root<Project> p = sq.from(Project.class);
    Join<Project, Employee> pe = p.join("employees");
    sq.select(pe).where(cb.equal(p.get("name"), cb.parameter(String.class, "projectname")));

    //
    c.select(e).where(cb.in(e).value(sq));
}

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * inner join/*from  ww w  . j  a  va 2s  . co m*/
 */
@Transactional
public void doFrom1() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);
    Root<Employee> e = c.from(Employee.class);
    Join<Object, Object> address = e.join("address");
    Join<Object, Object> department = e.join("department");
    c.multiselect(e, address, department);
    //         .where(cb.and(
    //               cb.equal(address.get("state"), "NY")),
    //               cb.greaterThan(e.<Long>get("salary"), Long.valueOf(1500)), 
    //               cb.like(department.<String>get("name"), "department%"
    //            ));
    showResult(c);
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

public List<T> findByAssignedEntity(ListAttribute<T, ?> listAttribute, Long entityId) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);
    Join<T, ?> memberJoin = rootQuery.join(listAttribute);

    Path<?> nameField = memberJoin.get("id");
    Predicate nameEquals = queryBuilder.equal(nameField, entityId);
    criteriaQuery.where(nameEquals);/*from  w w  w  .j a  v  a2 s . c o  m*/

    TypedQuery<T> typedQuery = getEntityManager().createQuery(select);

    return typedQuery.getResultList();
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

public List<T> findByAssignedEntity(SetAttribute<T, ?> setAttribute, Long entityId) {
    CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);
    Join<T, ?> memberJoin = rootQuery.join(setAttribute);

    Path<?> nameField = memberJoin.get("id");
    Predicate nameEquals = queryBuilder.equal(nameField, entityId);
    criteriaQuery.where(nameEquals);//from   w  w w.j a  v  a2  s .  co  m

    TypedQuery<T> typedQuery = getEntityManager().createQuery(select);

    return typedQuery.getResultList();
}

From source file:org.openregistry.core.repository.jpa.JpaPersonRepository.java

@Override
public List<Person> findByUnknownIdentifier(final String identifierValue) throws RepositoryAccessException {
    final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();

    final CriteriaQuery<JpaPersonImpl> c = criteriaBuilder.createQuery(JpaPersonImpl.class);
    final Root<JpaPersonImpl> person = c.from(JpaPersonImpl.class);
    final Join<JpaPersonImpl, JpaIdentifierImpl> identifier = person.join(JpaPersonImpl_.identifiers);

    c.select(person).distinct(true)/*from  w  ww .jav a  2s  .c om*/
            .where(criteriaBuilder.like(identifier.get(JpaIdentifierImpl_.value), identifierValue + "%"));

    final List<JpaPersonImpl> persons = this.entityManager.createQuery(c).getResultList();

    return new ArrayList<Person>(persons);
}

From source file:me.ineson.demo.service.utils.RestUtilsTest.java

@SuppressWarnings("unchecked")
@Test//  w  w  w  .  j av a2 s  .c  o  m
public void testParseWhereClauseCmr() {
    CriteriaBuilder criteriaBuilderMock = mock(CriteriaBuilder.class);
    CriteriaQuery<SolarBody> criteriaQueryMock = mock(CriteriaQuery.class);
    Root<SolarBody> rootMock = mock(Root.class);
    log.info("mock root {}, builder {}", rootMock, criteriaBuilderMock);

    Join<Object, Object> cmrRecordPath = mock(Join.class, "cmr field");
    when(rootMock.join("cmr")).thenReturn(cmrRecordPath);

    Join<Object, Object> idFieldPath = mock(Join.class, "id field");
    when(cmrRecordPath.get("id")).thenReturn(idFieldPath);

    Predicate firstPredicate = mock(Predicate.class);
    when(criteriaBuilderMock.equal(idFieldPath, "22")).thenReturn(firstPredicate);

    Predicate predicate = RestUtils.parseWhereClause("cmr.id=22", rootMock, criteriaQueryMock,
            criteriaBuilderMock, null);

    verify(rootMock, times(1)).join("cmr");
    verifyNoMoreInteractions(rootMock);

    verify(cmrRecordPath, times(1)).get("id");
    verifyNoMoreInteractions(cmrRecordPath);

    verify(criteriaBuilderMock, times(1)).equal(idFieldPath, "22");
    verifyNoMoreInteractions(criteriaBuilderMock);

    Assert.assertEquals(firstPredicate, predicate);
}