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);
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
Aggregations