Search in sources :

Example 11 with Employee

use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.

the class SQLResultSetMappingTestSuite method testComplicateResults.

/**
 * tests that embeddable and dot notation for fieldresults work
 */
public void testComplicateResults() throws Exception {
    SQLResultSetMapping resultSetMapping = new SQLResultSetMapping("ComplicatedInheritance");
    EntityResult entityResult;
    entityResult = new EntityResult(Employee.class);
    entityResult.addFieldResult(new FieldResult("period.startDate", "STARTDATE"));
    entityResult.addFieldResult(new FieldResult("address.id", "EMP_ADDR"));
    resultSetMapping.addResult(entityResult);
    SQLCall call = new SQLCall("SELECT t0.EMP_ID, t1.EMP_ID, t0.F_NAME, t0.L_NAME, t0.VERSION, t1.SALARY, t0.START_DATE AS STARTDATE, t0.END_DATE, t0.ADDR_ID AS EMP_ADDR, t0.MANAGER_EMP_ID FROM CMP3_EMPLOYEE t0, CMP3_SALARY t1 WHERE ((t1.EMP_ID = t0.EMP_ID) AND ( t0.L_NAME = 'Smith' ))");
    ResultSetMappingQuery query = new ResultSetMappingQuery(call);
    query.setSQLResultSetMapping(resultSetMapping);
    List results = (List) getServerSession().executeQuery(query);
    assertNotNull("No result returned", results);
    assertTrue("Incorrect number of results returned, expected 2 got " + results.size(), (results.size() == 2));
    for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
        Object resultElement = iterator.next();
        assertTrue("Failed to return Employee", (resultElement instanceof Employee));
        Employee emp = (Employee) resultElement;
        assertNotNull("Failed to get an address for Employee " + emp.getFirstName(), emp.getAddress());
    }
}
Also used : Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Iterator(java.util.Iterator) List(java.util.List)

Example 12 with Employee

use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaSimpleTestSuiteBase method baseTestCase.

// Test case for selecting ALL employees from the database
public void baseTestCase() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        List expectedResult = getServerSession().readAllObjects(Employee.class);
        clearCache();
        // "SELECT OBJECT(emp) FROM Employee emp"
        List<Employee> result = em.createQuery(em.getCriteriaBuilder().createQuery(Employee.class)).getResultList();
        assertTrue("Base Test Case Failed", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with Employee

use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaSimpleTestSuiteBase method distinctTest.

public void distinctTest() {
    EntityManager em = createEntityManager();
    ReadAllQuery raq = new ReadAllQuery();
    ExpressionBuilder employee = new ExpressionBuilder();
    Expression whereClause = employee.get("lastName").equal("Smith");
    raq.setReferenceClass(Employee.class);
    raq.setSelectionCriteria(whereClause);
    raq.useDistinct();
    Vector expectedResult = (Vector) getServerSession().executeQuery(raq);
    clearCache();
    // "SELECT DISTINCT OBJECT(emp) FROM Employee emp WHERE emp.lastName = \'Smith\'"
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    cq.distinct(true);
    cq.where(qb.equal(wrapper.get(wrapper.from(cq, Employee.class), Employee_lastName), "Smith"));
    List result = null;
    beginTransaction(em);
    try {
        result = em.createQuery(cq).getResultList();
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
    assertTrue("Distinct test failed", comparer.compareObjects(result, expectedResult));
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Expression(org.eclipse.persistence.expressions.Expression) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) List(java.util.List) ArrayList(java.util.ArrayList) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector)

Example 14 with Employee

use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaSimpleTestSuiteBase method simpleNotBetweenTest.

public void simpleNotBetweenTest() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp1 = (Employee) getServerSession().readAllObjects(Employee.class).firstElement();
        Employee emp2 = (Employee) getServerSession().readAllObjects(Employee.class).lastElement();
        ReadAllQuery raq = new ReadAllQuery();
        raq.setReferenceClass(Employee.class);
        ExpressionBuilder eb = new ExpressionBuilder();
        Expression whereClause = eb.get("id").between(emp1.getId(), emp2.getId()).not();
        raq.setSelectionCriteria(whereClause);
        Vector expectedResult = (Vector) getServerSession().executeQuery(raq);
        clearCache();
        // "SELECT OBJECT(emp) FROM Employee emp WHERE emp.id NOT BETWEEN " + emp1.getId() + " AND "+ emp2.getId()
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Root<Employee> root = wrapper.from(cq, Employee.class);
        cq.where(qb.not(qb.between(wrapper.get(root, Employee_id), emp1.getId(), emp2.getId())));
        List<Employee> result = em.createQuery(cq).getResultList();
        assertTrue("Simple Not Between Test failed", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Expression(org.eclipse.persistence.expressions.Expression) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector)

Example 15 with Employee

use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.

the class JUnitCriteriaSimpleTestSuiteBase method simpleThreeArgConcatTest.

public void simpleThreeArgConcatTest() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee expectedResult = (Employee) (getServerSession().readAllObjects(Employee.class).firstElement());
        clearCache();
        String partOne, partTwo, partThree;
        partOne = expectedResult.getFirstName().substring(0, 1);
        partTwo = expectedResult.getFirstName().substring(1, 2);
        partThree = expectedResult.getFirstName().substring(2);
        // "SELECT OBJECT(emp) FROM Employee emp WHERE emp.firstName = CONCAT(\"" + partOne + "\", CONCAT(\"" + partTwo
        // + "\", \"" + partThree + "\") )"
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Root<Employee> root = wrapper.from(cq, Employee.class);
        cq.where(qb.equal(wrapper.get(root, Employee_firstName), qb.concat(qb.literal(partOne), qb.concat(qb.literal(partTwo), qb.literal(partThree)))));
        List<Employee> result = em.createQuery(cq).getResultList();
        assertTrue("Concat test failed", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee)

Aggregations

Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)629 EntityManager (jakarta.persistence.EntityManager)602 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)376 Query (jakarta.persistence.Query)209 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)173 List (java.util.List)173 ArrayList (java.util.ArrayList)146 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)129 TypedQuery (jakarta.persistence.TypedQuery)107 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)98 Vector (java.util.Vector)90 ReportQuery (org.eclipse.persistence.queries.ReportQuery)89 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)83 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)80 Expression (org.eclipse.persistence.expressions.Expression)77 Test (org.junit.Test)76 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)70 FetchGroup (org.eclipse.persistence.queries.FetchGroup)65 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)60 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)47