use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testCollectionAddNewObjectUpdate.
/**
* Test adding a new object to a collection.
*/
public void testCollectionAddNewObjectUpdate() {
int id = createEmployee("testCollectionAddNew").getId();
clearCache();
EntityManager em = createEntityManager();
beginTransaction(em);
Employee employee = em.find(Employee.class, id);
SmallProject project = new SmallProject();
employee.getProjects().add(project);
project.getTeamMembers().add(employee);
commitTransaction(em);
// Note: even in this case the entity should still be in the shared cache and database below
if (em.contains(employee)) {
verifyObject(project);
verifyObject(employee);
}
clearCache();
if (em.contains(employee)) {
verifyObject(project);
verifyObject(employee);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testAddAndDeleteSameObject.
// Bug 346022
public void testAddAndDeleteSameObject() {
EntityManager em = createEntityManager();
beginTransaction(em);
Employee emp = new Employee();
emp.setFirstName("A");
em.persist(emp);
Project project = new SmallProject();
project.setDescription("B");
em.persist(project);
emp.addProject(project);
project.addTeamMember(emp);
em.flush();
em.clear();
clearCache();
try {
emp = em.find(Employee.class, emp.getId());
project = em.find(SmallProject.class, project.getId());
emp.removeProject(project);
project.removeTeamMember(emp);
em.flush();
emp.addProject(project);
project.addTeamMember(emp);
em.flush();
em.clear();
clearCache();
emp = em.find(Employee.class, emp.getId());
project = em.find(SmallProject.class, project.getId());
assertTrue("Employee projects were not the correct size", emp.getProjects().size() == 1);
assertTrue("Project did not have the correct number of team members", project.getTeamMembers().size() == 1);
} finally {
rollbackTransaction(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject 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.SmallProject 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);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject 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);
}
}
Aggregations