use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class QueryCastTestSuite method testTreatInSelect.
public void testTreatInSelect() {
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();
Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
List resultList = query.getResultList();
assertTrue("Incorrect results returned, expected 1, received: " + resultList.size(), resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class QueryCastTestSuite method testSelectCast.
public void testSelectCast() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
LargeProject proj = new LargeProject();
proj.setBudget(1000);
proj.setName("test1");
em.persist(proj);
LargeProject lp = new LargeProject();
lp.setBudget(100);
lp.setName("sp1");
em.persist(lp);
Employee emp = new Employee();
emp.setFirstName("Reggie");
emp.setLastName("Josephson");
emp.addProject(proj);
proj.addTeamMember(emp);
emp.addProject(lp);
lp.addTeamMember(emp);
em.persist(emp);
emp = new Employee();
emp.setFirstName("Ron");
emp.setLastName("Josephson");
emp.addProject(lp);
lp.addTeamMember(emp);
em.persist(emp);
em.flush();
clearCache();
em.clear();
Query query = em.createQuery("Select max(l.budget) from Employee e join treat(e.projects as LargeProject) l");
List resultList = query.getResultList();
assertTrue("Incorrect result size returned", resultList.size() == 1);
assertTrue("Incorrect results returned", (Double) resultList.get(0) == 1000);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class QueryCastTestSuite method testCastInSubselect.
public void testCastInSubselect() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
LargeProject proj = new LargeProject();
proj.setBudget(1000);
proj.setName("test1");
em.persist(proj);
LargeProject lp = new LargeProject();
lp.setBudget(100);
lp.setName("sp1");
em.persist(lp);
Employee emp = new Employee();
emp.setFirstName("Reggie");
emp.setLastName("Josephson");
emp.addProject(proj);
proj.addTeamMember(emp);
emp.addProject(lp);
lp.addTeamMember(emp);
emp.setSalary(10000);
em.persist(emp);
emp = new Employee();
emp.setFirstName("Ron");
emp.setLastName("Josephson");
emp.addProject(lp);
lp.addTeamMember(emp);
em.persist(emp);
emp.setSalary(100);
em.flush();
clearCache();
em.clear();
Query query = em.createQuery("select e from Employee e where e.salary > (Select max(l.budget) from Employee emp join treat(emp.projects as LargeProject) l)");
List resultList = query.getResultList();
assertTrue("Incorrect result size returned", resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class QueryCastTestSuite 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();
// create a shell query using JPA and alter the expression criteria
// this allows us to access the base of the query
JpaQuery query = (JpaQuery) em.createQuery("Select p from Project p");
ReadAllQuery raq = new ReadAllQuery(Project.class);
query.setDatabaseQuery(raq);
Expression exp = raq.getExpressionBuilder();
Expression criteria = exp.treat(LargeProject.class).get("budget").greaterThan(100);
raq.setSelectionCriteria(criteria);
List resultList = query.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.LargeProject in project eclipselink by eclipse-ee4j.
the class SQLResultSetMappingTestSuite method testComplicateResultWithInheritance.
public void testComplicateResultWithInheritance() throws Exception {
SQLResultSetMapping resultSetMapping = new SQLResultSetMapping("ComplicatedInheritance");
resultSetMapping.addResult(new ColumnResult("BUDGET_SUM"));
EntityResult entityResult = new EntityResult(Project.class);
resultSetMapping.addResult(entityResult);
entityResult = new EntityResult(SmallProject.class);
entityResult.addFieldResult(new FieldResult("id", "SMALL_ID"));
entityResult.addFieldResult(new FieldResult("name", "SMALL_NAME"));
entityResult.addFieldResult(new FieldResult("description", "SMALL_DESCRIPTION"));
entityResult.addFieldResult(new FieldResult("teamLeader", "SMALL_TEAMLEAD"));
entityResult.addFieldResult(new FieldResult("version", "SMALL_VERSION"));
entityResult.setDiscriminatorColumn("SMALL_DESCRIM");
resultSetMapping.addResult(entityResult);
SQLCall call = new SQLCall("SELECT (t1.BUDGET/t0.PROJ_ID) AS BUDGET_SUM, t0.PROJ_ID, t0.PROJ_TYPE, t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION, t1.BUDGET, t2.PROJ_ID AS SMALL_ID, t2.PROJ_TYPE AS SMALL_DESCRIM, t2.PROJ_NAME AS SMALL_NAME, t2.DESCRIP AS SMALL_DESCRIPTION, t2.LEADER_ID AS SMALL_TEAMLEAD, t2.VERSION AS SMALL_VERSION FROM CMP3_PROJECT t0, CMP3_PROJECT t2, CMP3_LPROJECT t1 WHERE t1.PROJ_ID = t0.PROJ_ID AND t2.PROJ_TYPE='S'");
ResultSetMappingQuery query = new ResultSetMappingQuery(call);
query.setSQLResultSetMapping(resultSetMapping);
List results = (List) getServerSession().executeQuery(query);
assertNotNull("No result returned", results);
assertTrue("Empty list returned", (results.size() != 0));
for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
Object[] resultElement = (Object[]) iterator.next();
assertTrue("Failed to Return 3 items", (resultElement.length == 3));
// Using Number as Different db/drivers can return different types
// e.g. Oracle with ijdbc14.jar returns BigDecimal where as
// Derby with derbyclient.jar returns Double
assertTrue("Failed to return column", (resultElement[0] instanceof Number));
assertTrue("Failed to return LargeProject", (resultElement[1] instanceof LargeProject));
assertTrue("Failed To Return SmallProject", (resultElement[2] instanceof SmallProject));
assertFalse("Returned same data in both result elements", ((SmallProject) resultElement[2]).getName().equals(((LargeProject) resultElement[1]).getName()));
}
}
Aggregations