use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project in project eclipselink by eclipse-ee4j.
the class SQLResultSetMappingTestSuite method testSimpleInheritance.
public void testSimpleInheritance() throws Exception {
SQLResultSetMapping resultSetMapping = new SQLResultSetMapping("SimpleInheritance");
EntityResult entityResult = new EntityResult(Project.class);
entityResult.setDiscriminatorColumn("SMALL_DESCRIM");
resultSetMapping.addResult(entityResult);
// Use ANSI outer join sytax so that the query works on most of the databases.
// SQLCall call = new SQLCall("SELECT t0.PROJ_ID, t0.PROJ_TYPE AS SMALL_DESCRIM, t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION, t1.BUDGET FROM CMP3_FA_PROJECT t0, CMP3_FA_LPROJECT t1 WHERE t1.PROJ_ID (+)= t0.PROJ_ID");
SQLCall call = new SQLCall("SELECT t0.PROJ_ID, t0.PROJ_TYPE AS SMALL_DESCRIM, t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION, t1.BUDGET FROM CMP3_FA_PROJECT t0 left outer join CMP3_FA_LPROJECT t1 on t1.PROJ_ID = t0.PROJ_ID");
ResultSetMappingQuery query = new ResultSetMappingQuery(call);
query.setSQLResultSetMapping(resultSetMapping);
List results = (List) getServerSession("fieldaccess").executeQuery(query);
assertNotNull("No result returned", results);
assertTrue("Empty list returned", (results.size() != 0));
for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
Object project = iterator.next();
assertTrue("Failed to return a project", (project instanceof Project));
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project in project eclipselink by eclipse-ee4j.
the class SQLResultSetMappingTestSuite method testRefresh.
public void testRefresh() throws Exception {
SQLResultSetMapping resultSetMapping = new SQLResultSetMapping("ComplicatedInheritance");
EntityResult entityResult = new EntityResult(Project.class);
resultSetMapping.addResult(entityResult);
entityResult.setDiscriminatorColumn("SMALL_DESCRIM");
SQLCall call = new SQLCall("SELECT t0.PROJ_ID, t0.PROJ_TYPE AS SMALL_DESCRIM, t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION, t1.BUDGET FROM CMP3_FA_PROJECT t0, CMP3_FA_PROJECT t2, CMP3_FA_LPROJECT t1 WHERE t1.PROJ_ID = t0.PROJ_ID");
ResultSetMappingQuery query = new ResultSetMappingQuery(call);
query.setSQLResultSetMapping(resultSetMapping);
query.setShouldRefreshIdentityMapResult(true);
EntityManager em = createEntityManager();
beginTransaction(em);
try {
List results = (List) getServerSession("fieldaccess").executeQuery(query);
assertNotNull("No result returned", results);
Project project = (Project) results.get(0);
project.setDescription("To A new changed description");
results = (List) getServerSession("fieldaccess").executeQuery(query);
assertNotNull("No result returned", results);
assertTrue("Empty list returned", (results.size() != 0));
assertFalse("Object was not refreshed", project.getDescription().equals("To A new changed description"));
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project in project eclipselink by eclipse-ee4j.
the class NestedFetchGroupTests method allNestedFetchGroupWithJoinFetch.
@Test
public void allNestedFetchGroupWithJoinFetch() {
EntityManager em = createEntityManager("fieldaccess");
try {
beginTransaction(em);
// select employees who are neither managers nor team leaders
Query query = em.createQuery("SELECT e FROM Employee e WHERE NOT EXISTS(SELECT p.id FROM Project p WHERE p.teamLeader = e) AND NOT EXISTS(SELECT e2.id FROM Employee e2 WHERE e2.manager = e)");
FetchGroup employeeFG = new FetchGroup("employee");
employeeFG.addAttribute("lastName");
employeeFG.addAttribute("address.country");
employeeFG.addAttribute("address.city");
query.setHint(QueryHints.LEFT_FETCH, "e.address");
employeeFG.addAttribute("phoneNumbers");
query.setHint(QueryHints.LEFT_FETCH, "e.phoneNumbers");
employeeFG.addAttribute("projects.name");
employeeFG.addAttribute("projects.teamLeader.firstName");
// employeeFG.addAttribute("projects.teamLeader.address.street");
// employeeFG.addAttribute("projects.teamLeader.address.postalCode");
employeeFG.addAttribute("projects.teamLeader.phoneNumbers.owner");
employeeFG.addAttribute("projects.teamLeader.phoneNumbers.type");
employeeFG.addAttribute("projects.teamLeader.phoneNumbers.areaCode");
query.setHint(QueryHints.LEFT_FETCH, "e.projects.teamLeader.phoneNumbers");
employeeFG.addAttribute("manager.firstName");
// employeeFG.addAttribute("manager.address.street");
// employeeFG.addAttribute("manager.address.postalCode");
employeeFG.addAttribute("manager.phoneNumbers.owner");
employeeFG.addAttribute("manager.phoneNumbers.type");
employeeFG.addAttribute("manager.phoneNumbers.areaCode");
query.setHint(QueryHints.LEFT_FETCH, "e.manager.phoneNumbers");
// department attribute defined with JoinFetchType.OUTER
employeeFG.addAttribute("department.name");
query.setHint(QueryHints.FETCH_GROUP, employeeFG);
List<Employee> employees = query.getResultList();
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
for (Employee emp : employees) {
assertFetched(emp, employeeFG);
Address address = emp.getAddress();
if (address != null) {
assertFetched(address, employeeFG.getGroup("address"));
}
for (PhoneNumber phone : emp.getPhoneNumbers()) {
assertFetched(phone, defaultPhoneFG);
}
for (Project project : emp.getProjects()) {
assertFetched(project, employeeFG.getGroup("projects"));
Employee teamLeader = project.getTeamLeader();
if (teamLeader != null) {
assertFetched(teamLeader, employeeFG.getGroup("projects.teamLeader"));
for (PhoneNumber phone : teamLeader.getPhoneNumbers()) {
assertFetched(phone, employeeFG.getGroup("projects.teamLeader.phoneNumbers"));
}
}
}
Employee manager = emp.getManager();
if (manager != null) {
assertFetched(manager, employeeFG.getGroup("manager"));
for (PhoneNumber phone : manager.getPhoneNumbers()) {
assertFetched(phone, employeeFG.getGroup("manager.phoneNumbers"));
}
}
Department department = emp.getDepartment();
if (department != null) {
assertFetched(department, employeeFG.getGroup("department"));
}
}
assertEquals(1, getQuerySQLTracker(em).getTotalSQLSELECTCalls());
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testDeleteEmployee.
// Test that deleting an employee works correctly.
public void testDeleteEmployee() {
// seems to be a Spring bug, removing from Spring run for now.
if ((getServerPlatform() != null) && getServerPlatform().isSpring()) {
return;
}
if (getDatabaseSession().getPlatform().isSymfoware()) {
getDatabaseSession().logMessage("Test testDeleteEmployee skipped for this platform, " + "The CascadeOnDelete doesn't work on a relation where CascadeType.Remove or CascadeType.All is specified on Symfoware Platform.");
return;
}
Employee employee = new Employee();
employee.addPhoneNumber(new PhoneNumber("home", "123", "4567"));
employee.addPhoneNumber(new PhoneNumber("fax", "456", "4567"));
employee.addResponsibility("work hard");
employee.addResponsibility("write code");
employee.addProject(new Project());
employee.setWorkWeek(new HashSet<Employee.Weekdays>());
employee.getWorkWeek().add(Employee.Weekdays.MONDAY);
employee.getWorkWeek().add(Employee.Weekdays.TUESDAY);
EntityManager em = createEntityManager();
QuerySQLTracker counter = null;
try {
beginTransaction(em);
em.persist(employee);
commitTransaction(em);
closeEntityManager(em);
clearCache();
counter = new QuerySQLTracker(getDatabaseSession());
em = createEntityManager();
beginTransaction(em);
employee = em.find(Employee.class, employee.getId());
counter.getSqlStatements().clear();
em.remove(employee);
if (isWeavingEnabled("fieldaccess") && counter.getSqlStatements().size() > 3) {
fail("Only 2 delete and 1 select should have occured.");
}
commitTransaction(em);
beginTransaction(em);
verifyDelete(employee, "fieldaccess");
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
if (counter != null) {
counter.remove();
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testDatabaseSyncNewObject.
public void testDatabaseSyncNewObject() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
Project project = new LargeProject();
em.persist(project);
project.setName("Blah");
project.setTeamLeader(new Employee());
project.getTeamLeader().addProject(project);
em.flush();
} catch (RuntimeException ex) {
rollbackTransaction(em);
if (ex instanceof IllegalStateException)
return;
}
fail("Failed to throw illegal argument when finding unregistered new object cascading on database sync");
}
Aggregations