Search in sources :

Example 1 with Employee

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

the class CriteriaQueryMetamodelTestSuite method simpleMetamodelCriteriaUpdateTest.

// ///UPDATE Criteria tests:
public void simpleMetamodelCriteriaUpdateTest() {
    if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) {
        getPersistenceUnitServerSession().logMessage("Test simpleUpdate skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    EntityManager em = createEntityManager();
    int nrOfEmps = ((Number) em.createQuery("SELECT COUNT(e) FROM Employee e ").getSingleResult()).intValue();
    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
    // test query "UPDATE Employee e SET e.firstName = 'CHANGED'";
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaUpdate<Employee> cq = qb.createCriteriaUpdate(Employee.class);
    Root<Employee> root = cq.from(entityEmp_);
    cq.set(root.get(entityEmp_.getSingularAttribute("firstName", String.class)), "CHANGED");
    beginTransaction(em);
    try {
        Query q = em.createQuery(cq);
        int updated = q.executeUpdate();
        assertEquals("simpleCriteriaUpdateTest: wrong number of updated instances", nrOfEmps, updated);
        // check database changes
        int nr = ((Number) em.createQuery("SELECT COUNT(e) FROM Employee e WHERE e.firstName = 'CHANGED'").getSingleResult()).intValue();
        assertEquals("simpleCriteriaUpdateTest: unexpected number of changed values in the database", nrOfEmps, nr);
    } finally {
        if (isTransactionActive(em)) {
            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.jpa21.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa21.advanced.PhoneNumber) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 2 with Employee

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

the class CriteriaQueryMetamodelTestSuite method testMetamodelOnClauseWithLeftJoin.

public void testMetamodelOnClauseWithLeftJoin() {
    EntityManager em = createEntityManager();
    Query query = em.createQuery("Select e from Employee e left join e.address a on a.city = 'Ottawa' " + "where a.postalCode is not null");
    List baseResult = query.getResultList();
    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
    EntityType<Address> entityAddr_ = metamodel.entity(Address.class);
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    Root<Employee> root = cq.from(entityEmp_);
    Join address = root.join(entityEmp_.getSingularAttribute("address"), JoinType.LEFT);
    address.on(qb.equal(address.get(entityAddr_.getSingularAttribute("city")), "Ottawa"));
    cq.where(qb.isNotNull(address.get(entityAddr_.getSingularAttribute("postalCode"))));
    List testResult = em.createQuery(cq).getResultList();
    clearCache();
    closeEntityManager(em);
    if (baseResult.size() != testResult.size()) {
        fail("Criteria query using ON clause with a left join did not match JPQL results; " + baseResult.size() + " were expected, while criteria query returned " + testResult.size());
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa21.advanced.Employee) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Address(org.eclipse.persistence.testing.models.jpa21.advanced.Address) Join(jakarta.persistence.criteria.Join) List(java.util.List) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 3 with Employee

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

the class CriteriaQueryMetamodelTestSuite method testMetamodelCriteriaUpdate.

public void testMetamodelCriteriaUpdate() {
    if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) {
        getPersistenceUnitServerSession().logMessage("Test simpleUpdate skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    EntityManager em = createEntityManager();
    int nrOfEmps = ((Number) em.createQuery("SELECT COUNT(e) FROM Employee e where e.firstName is not null").getSingleResult()).intValue();
    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
    // test query "UPDATE Employee e SET e.firstName = 'CHANGED' where e.firstName is not null";
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaUpdate<Employee> cq = qb.createCriteriaUpdate(Employee.class);
    Root<Employee> root = cq.from(entityEmp_);
    cq.set(root.get(entityEmp_.getSingularAttribute("firstName", String.class)), "CHANGED");
    cq.where(qb.isNotNull(root.get(entityEmp_.getSingularAttribute("firstName"))));
    beginTransaction(em);
    try {
        Query q = em.createQuery(cq);
        int updated = q.executeUpdate();
        assertEquals("simpleCriteriaUpdateTest: wrong number of updated instances", nrOfEmps, updated);
        // check database changes
        int nr = ((Number) em.createQuery("SELECT COUNT(e) FROM Employee e WHERE e.firstName = 'CHANGED'").getSingleResult()).intValue();
        assertEquals("simpleCriteriaUpdateTest: unexpected number of changed values in the database", nrOfEmps, nr);
    } finally {
        if (isTransactionActive(em)) {
            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.jpa21.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa21.advanced.PhoneNumber) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 4 with Employee

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

the class CriteriaQueryMetamodelTestSuite method testMetamodelCriteriaUpdateEmbeddedField.

public void testMetamodelCriteriaUpdateEmbeddedField() {
    if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) {
        getPersistenceUnitServerSession().logMessage("Test updateEmbeddedFieldTest skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    EntityManager em = createEntityManager();
    int nrOfEmps = ((Number) em.createQuery("SELECT COUNT(e) FROM Employee e where e.firstName is not null").getSingleResult()).intValue();
    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
    jakarta.persistence.metamodel.EmbeddableType<EmploymentPeriod> embedEmpPeriod_ = metamodel.embeddable(EmploymentPeriod.class);
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.set(1905, 11, 31, 0, 0, 0);
    java.sql.Date startDate = new java.sql.Date(startCalendar.getTime().getTime());
    // em.createQuery("UPDATE Employee e SET e.period.startDate= :startDate").setParameter("startDate", startDate).executeUpdate();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaUpdate<Employee> cq = qb.createCriteriaUpdate(Employee.class);
    Root<Employee> root = cq.from(entityEmp_);
    cq.set(root.get(entityEmp_.getSingularAttribute("period", EmploymentPeriod.class)).get(embedEmpPeriod_.getSingularAttribute("startDate", java.sql.Date.class)), startDate);
    beginTransaction(em);
    try {
        clearCache();
        int updated = em.createQuery(cq).executeUpdate();
        assertEquals("testCriteriaUpdateEmbeddedField: wrong number of updated instances", nrOfEmps, updated);
        // check database changes
        int nr = ((Number) em.createQuery("SELECT COUNT(e) FROM Employee e WHERE e.period.startDate = :startDate").setParameter("startDate", startDate).getSingleResult()).intValue();
        assertEquals("testCriteriaUpdateEmbeddedField: unexpected number of changed values in the database", nrOfEmps, nr);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EmploymentPeriod(org.eclipse.persistence.testing.models.jpa21.advanced.EmploymentPeriod) Calendar(java.util.Calendar) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa21.advanced.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa21.advanced.PhoneNumber) Metamodel(jakarta.persistence.metamodel.Metamodel)

Example 5 with Employee

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

the class CriteriaQueryMetamodelTestSuite method testMetamodelOnClause.

// Bug 367452
// Test join on clause
public void testMetamodelOnClause() {
    EntityManager em = createEntityManager();
    Query query = em.createQuery("Select e from Employee e join e.address a on a.city = 'Ottawa'");
    List baseResult = query.getResultList();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
    EntityType<Address> entityAddr_ = metamodel.entity(Address.class);
    Root<Employee> root = cq.from(entityEmp_);
    Join address = root.join(entityEmp_.getSingularAttribute("address"));
    address.on(qb.equal(address.get(entityAddr_.getSingularAttribute("city")), "Ottawa"));
    List testResult = em.createQuery(cq).getResultList();
    clearCache();
    closeEntityManager(em);
    if (baseResult.size() != testResult.size()) {
        fail("Criteria query using ON clause did not match JPQL results; " + baseResult.size() + " were expected, while criteria query returned " + testResult.size());
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa21.advanced.Employee) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) Address(org.eclipse.persistence.testing.models.jpa21.advanced.Address) Join(jakarta.persistence.criteria.Join) List(java.util.List) Metamodel(jakarta.persistence.metamodel.Metamodel)

Aggregations

EntityManager (jakarta.persistence.EntityManager)90 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)50 Employee (org.eclipse.persistence.testing.models.jpa21.advanced.Employee)43 Employee (org.eclipse.persistence.testing.models.jpa22.advanced.Employee)43 List (java.util.List)36 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)36 Query (jakarta.persistence.Query)31 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)27 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)25 Metamodel (jakarta.persistence.metamodel.Metamodel)16 PhoneNumber (org.eclipse.persistence.testing.models.jpa22.advanced.PhoneNumber)11 PersistenceUnitUtil (jakarta.persistence.PersistenceUnitUtil)9 PhoneNumber (org.eclipse.persistence.testing.models.jpa21.advanced.PhoneNumber)9 Expression (jakarta.persistence.criteria.Expression)8 Address (org.eclipse.persistence.testing.models.jpa21.advanced.Address)7 Address (org.eclipse.persistence.testing.models.jpa22.advanced.Address)7 Join (jakarta.persistence.criteria.Join)6 Predicate (jakarta.persistence.criteria.Predicate)6 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)5 Case (jakarta.persistence.criteria.CriteriaBuilder.Case)4