Search in sources :

Example 6 with TestEntity

use of org.javaee7.jpa.aggregate_function_in_select.entity.TestEntity in project javaee7-samples by javaee-samples.

the class QueryRepository method buildGetAll.

/**
     * Builds a criteria query equal to the JPQL
     * 
     * <code>SELECT _testEntity FROM TestEntity _testEntity</code>
     * 
     *
     */
private TypedQuery<TestEntity> buildGetAll() {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<TestEntity> criteriaQuery = criteriaBuilder.createQuery(TestEntity.class);
    Root<TestEntity> root = criteriaQuery.from(TestEntity.class);
    criteriaQuery.select(root);
    return entityManager.createQuery(criteriaQuery);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) TestEntity(org.javaee7.jpa.dynamicnamedquery.entity.TestEntity)

Example 7 with TestEntity

use of org.javaee7.jpa.aggregate_function_in_select.entity.TestEntity in project javaee7-samples by javaee-samples.

the class DynamicNamedQueryTest method testDynamicNamedCriteriaQueries.

@Test
public void testDynamicNamedCriteriaQueries() throws IOException, SAXException {
    // Nothing inserted yet, data base should not contain any entities 
    // (this tests that a simple query without parameters works as named query created by Criteria)
    assertTrue(testService.getAll().size() == 0);
    // Insert one entity
    TestEntity testEntity = new TestEntity();
    testEntity.setValue("myValue");
    testService.save(testEntity);
    // The total amount of entities should be 1
    assertTrue(testService.getAll().size() == 1);
    // The entity with "myValue" should be found
    // (this tests that a query with a parameter works as named query created by Criteria)
    assertTrue(testService.getByValue("myValue").size() == 1);
}
Also used : TestEntity(org.javaee7.jpa.dynamicnamedquery.entity.TestEntity) Test(org.junit.Test)

Aggregations

TestEntity (org.javaee7.jpa.dynamicnamedquery.entity.TestEntity)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 AggregatedTestEntity (org.javaee7.jpa.aggregate_function_in_select.entity.AggregatedTestEntity)1 TestEntity (org.javaee7.jpa.aggregate_function_in_select.entity.TestEntity)1 TestEntity (org.javaee7.jpa.datasourcedefinition_annotation_pu.entity.TestEntity)1 TestEntity (org.javaee7.jpa.datasourcedefinition_applicationxml_pu.entity.TestEntity)1 TestEntity (org.javaee7.jpa.datasourcedefinition_webxml_pu.entity.TestEntity)1 Test (org.junit.Test)1