use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testNestedBatchQueryHints.
public void testNestedBatchQueryHints() {
String errorMsg = "";
clearCache();
ServerSession session = getServerSession();
QuerySQLTracker counter = new QuerySQLTracker(session);
EntityManager em = createEntityManager();
beginTransaction(em);
try {
// setup
int nEmployees = 2;
int nProjects = 2;
int nTeamMembers = 2;
int nDealers = 2;
int nPhones = 2;
boolean useLargeProject = false;
List<Employee> employees = new ArrayList(nEmployees);
List<Employee> teamMembers = new ArrayList(nEmployees * nProjects * nTeamMembers);
for (int i = 0; i < nEmployees; i++) {
String iStr = Integer.toString(i);
Employee emp = new Employee("Employee_NestedBatchQueryHint", iStr);
employees.add(emp);
Employee manager = new Employee("Manager_NestedBatchQueryHint", iStr);
manager.addManagedEmployee(emp);
for (int j = 0; j < nProjects; j++) {
String jStr = Integer.toString(j);
Project project;
if (useLargeProject) {
project = new LargeProject();
} else {
project = new SmallProject();
}
useLargeProject = !useLargeProject;
project.setName(iStr + jStr);
manager.addProject(project);
project.addTeamMember(manager);
for (int k = 0; k < nTeamMembers; k++) {
String kStr = Integer.toString(k);
String teamMemberLastName = iStr + jStr + kStr;
Employee teamMember = new Employee("TeamMember_NestedBatchQueryHint", teamMemberLastName);
teamMembers.add(teamMember);
teamMember.addProject(project);
project.addTeamMember(teamMember);
for (int l = 0; l < nDealers; l++) {
String lStr = Integer.toString(l);
Dealer dealer = new Dealer(lStr, teamMemberLastName);
teamMember.addDealer(dealer);
}
for (int l = 0; l < nPhones; l++) {
String lStr = Integer.toString(l);
PhoneNumber phone = new PhoneNumber(lStr, teamMemberLastName, lStr + lStr + lStr + lStr + lStr + lStr + lStr);
teamMember.addPhoneNumber(phone);
}
}
}
}
// Persist cacscaded manager, then projects
for (Employee emp : employees) {
em.persist(emp);
}
// Persist cacscaded dealers and phones
for (Employee emp : teamMembers) {
em.persist(emp);
}
em.flush();
// test
em.clear();
Query query1 = em.createQuery("SELECT e FROM Employee e WHERE e.firstName = 'Employee_NestedBatchQueryHint' ORDER BY e.lastName");
query1.setHint(QueryHints.BATCH, "e.manager");
query1.setHint(QueryHints.BATCH, "e.manager.projects");
query1.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers");
query1.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.dealers");
query1.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.phoneNumbers");
errorMsg += verifyBatchReading("query1", query1, counter, employees, session);
em.clear();
Query query2 = em.createQuery("SELECT e FROM Employee e WHERE e.firstName = 'Employee_NestedBatchQueryHint' ORDER BY e.lastName");
query2.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers");
query2.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.dealers");
query2.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.phoneNumbers");
errorMsg += verifyBatchReading("query2", query2, counter, employees, session);
em.clear();
Query query3 = em.createQuery("SELECT e FROM Employee e WHERE e.firstName = 'Employee_NestedBatchQueryHint' ORDER BY e.lastName");
query3.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.dealers");
query3.setHint(QueryHints.BATCH, "e.manager.projects.teamMembers.phoneNumbers");
errorMsg += verifyBatchReading("query3", query3, counter, employees, session);
} finally {
// clean-up
rollbackTransaction(em);
closeEntityManager(em);
counter.remove();
}
if (errorMsg.length() > 0) {
fail(errorMsg);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method internalTestUpdateAllProjects.
protected void internalTestUpdateAllProjects(Class<?> cls) {
if ((JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) {
getServerSession().logMessage("Test testUpdateAll*Projects skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
return;
}
String className = Helper.getShortClassName(cls);
String name = "testUpdateAllProjects";
String newName = "testUpdateAllProjectsNEW";
HashMap map = null;
boolean ok = false;
try {
// setup
// save the original names of projects: will set them back in cleanup
// to restore the original state.
EntityManager em = createEntityManager();
List projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
map = new HashMap(projects.size());
for (int i = 0; i < projects.size(); i++) {
Project p = (Project) projects.get(i);
map.put(p.getId(), p.getName());
}
// populate Projects - necessary only if no SmallProject and/or LargeProject objects already exist.
beginTransaction(em);
try {
createProjectsWithName(name, null, em);
commitTransaction(em);
} finally {
closeEntityManagerAndTransaction(em);
}
// test
em = createEntityManager();
beginTransaction(em);
try {
em.createQuery("UPDATE " + className + " p set p.name = '" + newName + "'").executeUpdate();
commitTransaction(em);
} finally {
closeEntityManagerAndTransaction(em);
}
// verify
em = createEntityManager();
String errorMsg = "";
try {
projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
for (int i = 0; i < projects.size(); i++) {
Project p = (Project) projects.get(i);
String readName = p.getName();
if (cls.isInstance(p)) {
if (!newName.equals(readName)) {
errorMsg = errorMsg + "haven't updated name: " + p + "; ";
}
} else {
if (newName.equals(readName)) {
errorMsg = errorMsg + "have updated name: " + p + "; ";
}
}
}
} catch (RuntimeException ex) {
if (usesSOP() && !isSOPRecoverable()) {
if (ex instanceof PersistenceException) {
if (ex.getCause() instanceof QueryException && ((QueryException) ex.getCause()).getErrorCode() == QueryException.SOP_OBJECT_IS_NOT_FOUND) {
// getResultList is expected to fail because SOP field is set to null after bulk update
} else {
fail("Wrong cause of PersistenceException: " + ex.getCause());
}
} else {
fail("PersistenceException was expected");
}
} else {
throw ex;
}
} finally {
closeEntityManager(em);
}
if (errorMsg.length() > 0) {
fail(errorMsg);
} else {
ok = true;
}
} finally {
// clean-up
try {
if (map != null) {
EntityManager em = createEntityManager();
beginTransaction(em);
Query query = em.createQuery("SELECT OBJECT(p) FROM Project p");
if (usesSOP() && !isSOPRecoverable()) {
// otherwise will fail because SOP field is set to null after bulk update
query.setHint(QueryHints.SERIALIZED_OBJECT, "false");
}
List projects = query.getResultList();
try {
for (int i = 0; i < projects.size(); i++) {
Project p = (Project) projects.get(i);
String oldName = (String) map.get(((Project) projects.get(i)).getId());
p.setName(oldName);
}
commitTransaction(em);
} finally {
closeEntityManagerAndTransaction(em);
}
}
// delete projects that createProjectsWithName has created in setup.
// these will be left over under the newName
deleteProjectsWithName(newName);
} catch (RuntimeException ex) {
// eat clean-up exception in case the test failed
if (ok) {
throw ex;
}
}
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.
the class CriteriaQueryCastTestSuite method testDowncastInSelect.
public void testDowncastInSelect() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
LargeProject proj = new LargeProject();
proj.setBudget(1000);
proj.setName("test1");
em.persist(proj);
SmallProject sp = new SmallProject();
sp.setName("sp1");
em.persist(sp);
em.flush();
clearCache();
em.clear();
// this would work in the past if TYPE was added to the where clause
// Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Project> cq = qb.createQuery(Project.class);
Root<Project> root = cq.from(Project.class);
Root largeProjectRoot = qb.treat(root, LargeProject.class);
cq.select(largeProjectRoot.get("budget"));
List resultList = em.createQuery(cq).getResultList();
assertTrue("Incorrect results returned", resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.
the class CriteriaQueryCastTestSuite method testDowncastManyToManyQueryKey.
public void testDowncastManyToManyQueryKey() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
LargeProject proj = new LargeProject();
proj.setBudget(1000);
proj.setName("test1");
em.persist(proj);
SmallProject sp = new SmallProject();
sp.setName("sp1");
em.persist(sp);
Employee emp = new Employee();
emp.setFirstName("Reggie");
emp.setLastName("Josephson");
emp.addProject(proj);
proj.addTeamMember(emp);
emp.addProject(sp);
sp.addTeamMember(emp);
em.persist(emp);
emp = new Employee();
emp.setFirstName("Ron");
emp.setLastName("Josephson");
emp.addProject(sp);
sp.addTeamMember(emp);
em.persist(emp);
em.flush();
clearCache();
em.clear();
// Query query = em.createQuery("Select e from Employee e join treat(e.projects as LargeProject) p where p.budget > 100");
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
Root<Employee> root = cq.from(Employee.class);
Join projectsJoin = root.join("projects");
Join largeProjectJoin = qb.treat(projectsJoin, LargeProject.class);
cq.where(qb.gt(largeProjectJoin.get("budget"), 100));
List resultList = em.createQuery(cq).getResultList();
assertTrue("Incorrect results returned", resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.
the class CriteriaQueryCastTestSuite method testDowncastManyToManyExpressionBuilder.
public void testDowncastManyToManyExpressionBuilder() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
LargeProject proj = new LargeProject();
proj.setBudget(1000);
proj.setName("test1");
em.persist(proj);
SmallProject sp = new SmallProject();
sp.setName("sp1");
em.persist(sp);
Employee emp = new Employee();
emp.setFirstName("Reggie");
emp.setLastName("Josephson");
emp.addProject(proj);
proj.addTeamMember(emp);
emp.addProject(sp);
sp.addTeamMember(emp);
em.persist(emp);
emp = new Employee();
emp.setFirstName("Ron");
emp.setLastName("Josephson");
emp.addProject(sp);
sp.addTeamMember(emp);
em.persist(emp);
em.flush();
clearCache();
em.clear();
// JpaQuery query = (JpaQuery)em.createQuery("Select p from Project p where treat (p as LargeProject).budget > 100");
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Project> cq = qb.createQuery(Project.class);
Root<Project> root = cq.from(Project.class);
Root largeProjectRoot = qb.treat(root, LargeProject.class);
cq.where(qb.gt(largeProjectRoot.get("budget"), 100));
List resultList = em.createQuery(cq).getResultList();
assertTrue("Incorrect results returned", resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations