use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method simpleTypeTest.
public void simpleTypeTest() {
EntityManager em = createEntityManager();
List expectedResult = getServerSession().readAllObjects(LargeProject.class);
clearCache();
// "SELECT OBJECT(proj) FROM Project proj WHERE TYPE(proj) = LargeProject"
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Project> cq = qb.createQuery(Project.class);
cq.where(qb.equal(wrapper.from(cq, Project.class).type(), LargeProject.class));
beginTransaction(em);
try {
List<Project> result = em.createQuery(cq).getResultList();
assertTrue("SimpleTypeTest", comparer.compareObjects(result, expectedResult));
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method largeProjectCastTest.
public void largeProjectCastTest() {
EntityManager em = createEntityManager();
ReadAllQuery query = new ReadAllQuery();
Expression selectionCriteria = new ExpressionBuilder().anyOf("projects").treat(LargeProject.class).get("budget").equal(5000);
query.setSelectionCriteria(selectionCriteria);
query.setReferenceClass(Employee.class);
// gf 1395 changed jpql to not use distinct on joins
query.dontUseDistinct();
Vector expectedResult = (Vector) getServerSession().executeQuery(query);
clearCache();
// "SELECT e from Employee e join cast(e.project, LargeProject) p where p.budget = 1000
CriteriaBuilder qb1 = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq1 = qb1.createQuery(Employee.class);
Root<Employee> empRoot = wrapper.from(cq1, Employee.class);
Join<Employee, Project> join = wrapper.join(empRoot, Employee_projects);
jakarta.persistence.criteria.Expression exp = wrapper.get((Path) join.as(LargeProject.class), LargeProject_budget);
cq1.where(qb1.equal(exp, 5000));
beginTransaction(em);
try {
List<Employee> result = em.createQuery(cq1).getResultList();
assertTrue("LargeProject cast failed.", comparer.compareObjects(result, expectedResult));
} finally {
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<Object, LargeProject> l = qb.treat(root.join("projects"), LargeProject.class);
cq.select(qb.max(l.get("budget")));
List<Number> 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 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<LargeProject> largeProjectRoot = qb.treat(root, LargeProject.class);
cq.select(largeProjectRoot.get("budget"));
List<Project> 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 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<LargeProject> largeProjectRoot = qb.treat(root, LargeProject.class);
cq.where(qb.gt(largeProjectRoot.get("budget"), 100));
List<Project> resultList = em.createQuery(cq).getResultList();
assertTrue("Incorrect results returned", resultList.size() == 1);
} finally {
if (this.isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations