use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject 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<Object, Object> projectsJoin = root.join("projects");
Join<Object, LargeProject> largeProjectJoin = qb.treat(projectsJoin, LargeProject.class);
cq.where(qb.gt(largeProjectJoin.get("budget"), 100));
List<Employee> 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.LargeProject in project eclipselink by eclipse-ee4j.
the class CriteriaQueryCastTestSuite 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");
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Number> cq = qb.createQuery(Number.class);
Root<Employee> root = cq.from(Employee.class);
Join l = qb.treat(root.join("projects"), LargeProject.class);
cq.select(qb.max(l.get("budget")));
List resultList = em.createQuery(cq).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 EntityManagerJUnitTestSuite method testInheritanceFetchJoinSecondCall.
// Bug 370474 - in a joined inheritance hierarchy, base class OneToMany relationship, query using join fetch works once then fails
public void testInheritanceFetchJoinSecondCall() {
EntityManager em = createEntityManager();
LargeProject project = new LargeProject();
project.setName("InheritanceFetchJoinSecondCall");
beginTransaction(em);
try {
// should have at least one project
em.persist(project);
em.flush();
TypedQuery<LargeProject> queryFetchOne = em.createQuery("SELECT p FROM LargeProject p JOIN FETCH p.teamLeader", LargeProject.class);
TypedQuery<LargeProject> queryFetchMany = em.createQuery("SELECT p FROM LargeProject p JOIN FETCH p.teamMembers", LargeProject.class);
em.clear();
// first time works
queryFetchOne.getResultList();
em.clear();
// second time used to fail
queryFetchOne.getResultList();
em.clear();
// first time works
queryFetchMany.getResultList();
em.clear();
// second time used to fail
queryFetchMany.getResultList();
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method createProjectsWithName.
/*this creates project types both with and without a teamLeader if one is passed in, and
* returns the number of project types created (2x the number of project instances will have been created if a team lead is specified)
*/
protected int createProjectsWithName(String name, Employee teamLeader, EntityManager em) {
Project p = new Project();
p.setName(name);
em.persist(p);
SmallProject sp = new SmallProject();
sp.setName(name);
em.persist(sp);
LargeProject lp = new LargeProject();
lp.setName(name);
em.persist(lp);
HugeProject hp = new HugeProject();
hp.setName(name);
em.persist(hp);
SpecialHugeProject shp = new SpecialHugeProject();
shp.setName(name);
em.persist(shp);
if (teamLeader != null) {
Project pWTL = new Project();
pWTL.setName(name);
pWTL.setTeamLeader(teamLeader);
em.persist(pWTL);
SmallProject spWTL = new SmallProject();
spWTL.setName(name);
spWTL.setTeamLeader(teamLeader);
em.persist(spWTL);
LargeProject lpWTL = new LargeProject();
lpWTL.setName(name);
lpWTL.setTeamLeader(teamLeader);
em.persist(lpWTL);
HugeProject hpWTL = new HugeProject();
hpWTL.setName(name);
hpWTL.setTeamLeader(teamLeader);
em.persist(hpWTL);
SpecialHugeProject shpWTL = new SpecialHugeProject();
shpWTL.setName(name);
shpWTL.setTeamLeader(teamLeader);
em.persist(shpWTL);
}
// change this if this method creates a new project subclass type.
return 5;
}
use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class QueryCastTestSuite 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();
ExpressionBuilder builder = new ExpressionBuilder(Project.class);
ReportQuery rq = new ReportQuery(Project.class, builder);
rq.addAttribute("project", builder.treat(LargeProject.class).get("budget"));
rq.setSelectionCriteria(builder.type().equal(LargeProject.class));
List resultList = (List) ((JpaEntityManager) em.getDelegate()).getActiveSession().executeQuery(rq);
assertTrue("Incorrect results returned", resultList.size() == 1);
// Test equivalent JPQL as well
Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
List JPQLresultList = query.getResultList();
assertTrue("Incorrect results returned", JPQLresultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations