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