Search in sources :

Example 1 with QuerySQLTracker

use of org.eclipse.persistence.testing.framework.QuerySQLTracker in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testDeleteEmployee.

// Test that deleting an employee works correctly.
public void testDeleteEmployee() {
    Employee employee = new Employee();
    employee.addPhoneNumber(new PhoneNumber("home", "123", "4567"));
    employee.addPhoneNumber(new PhoneNumber("fax", "456", "4567"));
    employee.addResponsibility("work hard");
    employee.addResponsibility("write code");
    employee.addProject(new Project());
    employee.setWorkWeek(new HashSet<Employee.Weekdays>());
    employee.getWorkWeek().add(Employee.Weekdays.MONDAY);
    employee.getWorkWeek().add(Employee.Weekdays.TUESDAY);
    QuerySQLTracker counter = new QuerySQLTracker(getDatabaseSession());
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        em.persist(employee);
        commitTransaction(em);
        closeEntityManager(em);
        clearCache();
        em = createEntityManager();
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        counter.getSqlStatements().clear();
        em.remove(employee);
        commitTransaction(em);
        if (isWeavingEnabled() && counter.getSqlStatements().size() > 13) {
            fail("Only 13 sql statements should have occured:" + counter.getSqlStatements().size());
        }
        beginTransaction(em);
        verifyDelete(employee);
        commitTransaction(em);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        counter.remove();
        closeEntityManager(em);
    }
}
Also used : LargeProject(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.SuperLargeProject) Project(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.Project) SmallProject(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) PhoneNumber(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 2 with QuerySQLTracker

use of org.eclipse.persistence.testing.framework.QuerySQLTracker in project eclipselink by eclipse-ee4j.

the class AdvancedCriteriaQueryTestSuite method testQueryCacheFirstCacheHits.

/**
 * Test that a cache hit will occur on a query.
 */
public void testQueryCacheFirstCacheHits() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    QuerySQLTracker counter = null;
    try {
        // Load an employee into the cache.
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Query query = em.createQuery(cq);
        List result = query.getResultList();
        Employee employee = (Employee) result.get(result.size() - 1);
        // Count SQL.
        counter = new QuerySQLTracker(getServerSession());
        // Query by primary key.
        cq = qb.createQuery(Employee.class);
        Root<Employee> from = cq.from(Employee.class);
        cq.where(qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName")));
        query = em.createQuery(cq);
        query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheThenDatabase);
        query.setParameter("firstName", employee.getFirstName());
        Employee queryResult = (Employee) query.getSingleResult();
        if (!queryResult.getFirstName().equals(employee.getFirstName())) {
            fail("Employees are not equal: " + employee + ", " + queryResult);
        }
        if (counter.getSqlStatements().size() > 0) {
            fail("Cache hit do not occur: " + counter.getSqlStatements());
        }
    } finally {
        rollbackTransaction(em);
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Query(jakarta.persistence.Query) TypedQuery(jakarta.persistence.TypedQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) List(java.util.List) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 3 with QuerySQLTracker

use of org.eclipse.persistence.testing.framework.QuerySQLTracker in project eclipselink by eclipse-ee4j.

the class AdvancedCriteriaQueryTestSuite method testTupleQuery.

/**
 * Test that a cache hit will occur on a primary key query.
 */
public void testTupleQuery() {
    EntityManager em = createEntityManager();
    QuerySQLTracker counter = null;
    beginTransaction(em);
    try {
        // Load an employee into the cache.
        CriteriaBuilder qb = em.getCriteriaBuilder();
        Query query = em.createQuery(em.getCriteriaBuilder().createQuery(Employee.class));
        List result = query.getResultList();
        Employee employee = (Employee) result.get(0);
        // Count SQL.
        counter = new QuerySQLTracker(getServerSession());
        // Query by primary key.
        CriteriaQuery<Tuple> cq = qb.createQuery(Tuple.class);
        Root<Employee> from = cq.from(Employee.class);
        cq.multiselect(from.get("id"), from.get("firstName"));
        cq.where(qb.and(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")), qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName"))));
        TypedQuery<Tuple> typedQuery = em.createQuery(cq);
        typedQuery.setParameter("id", employee.getId());
        typedQuery.setParameter("firstName", employee.getFirstName());
        Tuple queryResult = typedQuery.getSingleResult();
        assertTrue("Query Results do not match selection", queryResult.get(0).equals(employee.getId()) && queryResult.get(1).equals(employee.getFirstName()));
    } finally {
        rollbackTransaction(em);
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : JpaCriteriaBuilder(org.eclipse.persistence.jpa.JpaCriteriaBuilder) CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Query(jakarta.persistence.Query) TypedQuery(jakarta.persistence.TypedQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) List(java.util.List) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker) Tuple(jakarta.persistence.Tuple)

Example 4 with QuerySQLTracker

use of org.eclipse.persistence.testing.framework.QuerySQLTracker in project eclipselink by eclipse-ee4j.

the class AdvancedQueryTestSuite method testTupleQuery.

/**
 * Test that a cache hit will occur on a primary key query.
 */
public void testTupleQuery() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    QuerySQLTracker counter = null;
    try {
        // Load an employee into the cache.
        CriteriaBuilder qb = em.getCriteriaBuilder();
        Query query = em.createQuery(em.getCriteriaBuilder().createQuery(Employee.class));
        List result = query.getResultList();
        Employee employee = (Employee) result.get(0);
        // Count SQL.
        counter = new QuerySQLTracker(getServerSession());
        // Query by primary key.
        CriteriaQuery<Tuple> cq = qb.createQuery(Tuple.class);
        Root<Employee> from = cq.from(Employee.class);
        cq.multiselect(from.get("id"), from.get("firstName"));
        cq.where(qb.and(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")), qb.equal(from.get("firstName"), qb.parameter(from.get("firstName").getModel().getBindableJavaType(), "firstName"))));
        TypedQuery<Tuple> typedQuery = em.createQuery(cq);
        typedQuery.setParameter("id", employee.getId());
        typedQuery.setParameter("firstName", employee.getFirstName());
        Tuple queryResult = typedQuery.getSingleResult();
        assertTrue("Query Results do not match selection", queryResult.get(0).equals(employee.getId()) && queryResult.get(1).equals(employee.getFirstName()));
    } finally {
        rollbackTransaction(em);
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) TypedQuery(jakarta.persistence.TypedQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) List(java.util.List) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker) Tuple(jakarta.persistence.Tuple)

Example 5 with QuerySQLTracker

use of org.eclipse.persistence.testing.framework.QuerySQLTracker in project eclipselink by eclipse-ee4j.

the class AdvancedQueryTestSuite method testQueryExactPrimaryKeyCacheHits.

/**
 * Test that a cache hit will occur on a primary key query.
 */
public void testQueryExactPrimaryKeyCacheHits() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    QuerySQLTracker counter = null;
    try {
        // Load an employee into the cache.
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
        Query query = em.createQuery(cq);
        List result = query.getResultList();
        Employee employee = (Employee) result.get(0);
        // Count SQL.
        counter = new QuerySQLTracker(getServerSession());
        // Query by primary key.
        cq = qb.createQuery(Employee.class);
        Root<Employee> from = cq.from(Employee.class);
        cq.where(qb.equal(from.get("id"), qb.parameter(from.get("id").getModel().getBindableJavaType(), "id")));
        query = em.createQuery(cq);
        query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheByExactPrimaryKey);
        query.setParameter("id", employee.getId());
        Employee queryResult = (Employee) query.getSingleResult();
        if (queryResult != employee) {
            fail("Employees are not equal: " + employee + ", " + queryResult);
        }
        if (counter.getSqlStatements().size() > 0) {
            fail("Cache hit do not occur: " + counter.getSqlStatements());
        }
    } finally {
        rollbackTransaction(em);
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) TypedQuery(jakarta.persistence.TypedQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) List(java.util.List) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Aggregations

QuerySQLTracker (org.eclipse.persistence.testing.framework.QuerySQLTracker)69 EntityManager (jakarta.persistence.EntityManager)68 Query (jakarta.persistence.Query)48 TypedQuery (jakarta.persistence.TypedQuery)44 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)43 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)34 ReadQuery (org.eclipse.persistence.queries.ReadQuery)31 List (java.util.List)21 ArrayList (java.util.ArrayList)14 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)12 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)11 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)11 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)10 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)5 Buyer (org.eclipse.persistence.testing.models.jpa.advanced.Buyer)4 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)4 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)4 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)4 SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)4 Customer (org.eclipse.persistence.testing.models.jpa.relationships.Customer)4