Search in sources :

Example 1 with Employee

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

the class JUnitCriteriaSimpleTestSuite method testOneEqualsOne.

/**
 * Tests 1=1 returns correct result.
 */
public void testOneEqualsOne() throws Exception {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        // "SELECT e FROM Employee e"
        Query query = em.createQuery(qb.createQuery(Employee.class));
        List emps = query.getResultList();
        assertNotNull(emps);
        int numRead = emps.size();
        // "SELECT e FROM Employee e WHERE 1=1");
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        cq.where(qb.equal(qb.literal(1), 1));
        emps = em.createQuery(cq).getResultList();
        assertNotNull(emps);
        assertEquals(numRead, emps.size());
        ExpressionBuilder builder = new ExpressionBuilder();
        query = ((JpaEntityManager) em.getDelegate()).createQuery(builder.value(1).equal(builder.value(1)), Employee.class);
        emps = query.getResultList();
        assertNotNull(emps);
        assertEquals(numRead, emps.size());
    } 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) TypedQuery(jakarta.persistence.TypedQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) List(java.util.List) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder)

Example 2 with Employee

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

the class JUnitCriteriaSimpleTestSuiteBase method testTupleStringTypeValidation.

/**
 * bug 366179 : test Tuple.get(string, Type) validation
 */
public void testTupleStringTypeValidation() {
    EntityManager em = createEntityManager();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = qb.createTupleQuery();
    Root<Employee> emp = wrapper.from(criteria, Employee.class);
    criteria.multiselect(wrapper.get(emp, Employee_lastName).alias("lastName"), wrapper.get(emp, Employee_firstName).alias("firstName"));
    // make sure firstName !=null for type checking
    criteria.where(qb.isNotNull(wrapper.get(emp, Employee_firstName)));
    TypedQuery<Tuple> query = em.createQuery(criteria);
    List<Tuple> list = query.getResultList();
    Tuple row = list.get(0);
    // verify it doesn't throw an exception in a valid case first:
    row.get("lastName", String.class);
    try {
        Object result = row.get("firstName", java.sql.Date.class);
        fail("IllegalArgumentException expected using an invalid value. Result returned:" + result);
    } catch (Exception iae) {
        assertEquals(iae.getClass(), IllegalArgumentException.class);
    }
    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) Tuple(jakarta.persistence.Tuple)

Example 3 with Employee

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

the class JUnitCriteriaSimpleTestSuiteBase method simpleReverseParameterTest.

public void simpleReverseParameterTest() {
    EntityManager em = createEntityManager();
    Employee emp = (Employee) getServerSession().readAllObjects(Employee.class).elementAt(0);
    clearCache();
    String parameterName = "firstName";
    ExpressionBuilder builder = new ExpressionBuilder();
    Expression whereClause = builder.get("firstName").equal(builder.getParameter(parameterName));
    ReadAllQuery raq = new ReadAllQuery();
    raq.setReferenceClass(Employee.class);
    raq.setSelectionCriteria(whereClause);
    raq.addArgument(parameterName);
    Vector<String> parameters = new Vector<>();
    parameters.add(emp.getFirstName());
    Vector expectedResult = (Vector) getServerSession().executeQuery(raq, parameters);
    clearCache();
    // "SELECT OBJECT(emp) FROM Employee emp WHERE ?1 = emp.firstName "
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    cq.where(qb.equal(qb.parameter(String.class, "1"), wrapper.get(wrapper.from(cq, Employee.class), Employee_firstName)));
    beginTransaction(em);
    try {
        List<Employee> result = em.createQuery(cq).setParameter("1", parameters.get(0)).getResultList();
        assertTrue("Simple Reverse Parameter 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 4 with Employee

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

the class JUnitCriteriaSimpleTestSuiteBase method testTupleIndexValidation.

/**
 * bug 366104 : Tuple.get(int) validation
 */
public void testTupleIndexValidation() {
    EntityManager em = createEntityManager();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = qb.createTupleQuery();
    Root<Employee> emp = wrapper.from(criteria, Employee.class);
    criteria.multiselect(wrapper.get(emp, Employee_lastName), wrapper.get(emp, Employee_firstName));
    TypedQuery<Tuple> query = em.createQuery(criteria);
    List<Tuple> list = query.getResultList();
    Tuple row = list.get(0);
    try {
        Object result = row.get(-1);
        fail("IllegalArgumentException not thrown when using index of -1 on a Tuple.  returned: " + result);
    } catch (Exception iae) {
        assertEquals(iae.getClass(), IllegalArgumentException.class);
    }
    try {
        row.get(2);
        fail("IllegalArgumentException expected. Array:" + Arrays.toString(row.toArray()));
    } catch (Exception iae) {
        assertEquals(iae.getClass(), IllegalArgumentException.class);
    }
}
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) Tuple(jakarta.persistence.Tuple)

Example 5 with Employee

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

the class JUnitCriteriaSimpleTestSuiteBase method smallProjectNOTMemberOfProjectsTest.

public void smallProjectNOTMemberOfProjectsTest() {
    EntityManager em = createEntityManager();
    // query for those employees with Project named "Enterprise" (which should be a SmallProject)
    ReadObjectQuery smallProjectQuery = new ReadObjectQuery();
    smallProjectQuery.setReferenceClass(SmallProject.class);
    smallProjectQuery.setSelectionCriteria(new ExpressionBuilder().get("name").equal("Enterprise"));
    SmallProject smallProject = (SmallProject) getServerSession().executeQuery(smallProjectQuery);
    ReadAllQuery query = new ReadAllQuery();
    query.addArgument("smallProject");
    Expression selectionCriteria = new ExpressionBuilder().noneOf("projects", new ExpressionBuilder().equal(new ExpressionBuilder().getParameter("smallProject")));
    query.setSelectionCriteria(selectionCriteria);
    query.setReferenceClass(Employee.class);
    Vector<SmallProject> arguments = new Vector<>();
    arguments.add(smallProject);
    Vector expectedResult = (Vector) getServerSession().executeQuery(query, arguments);
    // "SELECT OBJECT(employee) FROM Employee employee WHERE ?1 NOT MEMBER OF employee.projects"
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    cq.where(qb.isNotMember(qb.parameter(Project.class, "1"), wrapper.<Employee, Collection<Project>>get(wrapper.from(cq, Employee.class), Employee_projects)));
    beginTransaction(em);
    try {
        List<Employee> result = em.createQuery(cq).setParameter("1", smallProject).getResultList();
        assertTrue("Simple small Project NOT Member Of Projects test failed", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) 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) Collection(java.util.Collection) Vector(java.util.Vector)

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