@Test public void departmentTest() throws Exception { Department dpNew = new Department(); em.getTransaction().begin(); dpNew.setName("Bob's Department"); dpNew.setLocation("Baltimore"); em.persist(dpNew); em.getTransaction().commit(); int newId = dpNew.getId(); // test earlier persist Department dpLoad1 = em.find(Department.class, newId); assertEquals(dpLoad1.getId(), newId); assertEquals("Insert didn't work", dpLoad1.getName(), "Bob's Department"); assertEquals("Insert didn't work", dpLoad1.getLocation(), "Baltimore"); // test update em.getTransaction().begin(); dpLoad1.setName("Phil's Department"); dpLoad1.setLocation("Philadelphia"); em.getTransaction().commit(); Department dpLoad2 = em.find(Department.class, newId); assertEquals(dpLoad2.getId(), newId); assertEquals("Update didn't work", dpLoad2.getName(), "Phil's Department"); assertEquals("Update didn't work", dpLoad2.getLocation(), "Philadelphia"); em.getTransaction().begin(); em.remove(dpLoad1); em.getTransaction().commit(); Department shouldBeNull = em.find(Department.class, newId); assertNull("Delete didn't work", shouldBeNull); }
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("com/tyy/spring4/tag/util/beans.xml"); Employee e1 = ac.getBean("employee1", Employee.class); Employee e2 = ac.getBean("employee2", Employee.class); Department depart = (Department) ac.getBean("department"); System.out.println(e1); System.out.println(e2); System.out.println("Department: " + depart.getId() + " " + depart.getName()); System.out.println("\nArray:"); for (Employee e : depart.getEmployeeArray()) { System.out.println(e); } System.out.println("\nList:"); System.out.println(depart.getEmployeeList().getClass()); for (Employee e : depart.getEmployeeList()) { System.out.println(e); } // Set元素具有唯一性,多次添加不会报错,但无效(本例中应该重写Employee的equals方法和hashCode方法) System.out.println("\nSet:"); System.out.println(depart.getEmployeeSet().getClass()); for (Employee e : depart.getEmployeeSet()) { System.out.println(e); } System.out.println("\nMap:"); System.out.println(depart.getEmployeeMap().getClass()); for (Entry<String, Employee> entry : depart.getEmployeeMap().entrySet()) { System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue()); } System.out.println("\nProperties:"); System.out.println(depart.getEmployeeProperties().getClass()); Properties prop = depart.getEmployeeProperties(); Enumeration<String> propNames = (Enumeration<String>) prop.propertyNames(); while (propNames.hasMoreElements()) { String propName = propNames.nextElement(); System.out.println("Property -> key: " + propName + ", value: " + prop.getProperty(propName)); } }
private void setDepartment(Department department) { geographicalDepartment.setValue(department != null ? department.getParent().getId() : null); this.department.setValue(department != null ? department.getId() : null); }
@Override public void build( JsonObjectBuilder pBuilder, Examiner pReadOnly, UriInfo pUriInfo, LocalCache pLocalCache) { if (pReadOnly.getId() != null) { pBuilder.add("id", pReadOnly.getId()); } Course course = (Course) pLocalCache.cache(() -> pReadOnly.getCourse(), pReadOnly.getCourseId(), Course.class); pBuilder.add("courseId", course.getId()); pBuilder.add("courseNo", course.getNo()); pBuilder.add("courseTitle", course.getTitle()); pBuilder.add("courseCrHr", course.getCrHr()); pBuilder.add("year", course.getYear()); pBuilder.add("semester", course.getSemester()); pBuilder.add("syllabusId", course.getSyllabusId()); if (!StringUtils.isEmpty(pReadOnly.getPreparerId())) { Teacher teacher = (Teacher) pLocalCache.cache( () -> pReadOnly.getPreparer(), pReadOnly.getPreparerId(), Teacher.class); pBuilder.add("preparerId", teacher.getId()); pBuilder.add("preparerName", teacher.getName()); } if (!StringUtils.isEmpty(pReadOnly.getScrutinizerId())) { Teacher teacher = (Teacher) pLocalCache.cache( () -> pReadOnly.getScrutinizer(), pReadOnly.getScrutinizerId(), Teacher.class); pBuilder.add("scrutinizerId", teacher.getId()); pBuilder.add("scrutinizerName", teacher.getName()); } Department department = (Department) pLocalCache.cache( () -> course.getOfferedBy(), course.getOfferedDepartmentId(), Department.class); pBuilder.add("courseOfferedByDepartmentId", department.getId()); pBuilder.add("courseOfferedByDepartmentName", department.getShortName()); Syllabus syllabus = (Syllabus) pLocalCache.cache(() -> course.getSyllabus(), course.getSyllabusId(), Syllabus.class); Program program = (Program) pLocalCache.cache(() -> syllabus.getProgram(), syllabus.getProgramId(), Program.class); Department offeredToDepartment = (Department) pLocalCache.cache( () -> program.getDepartment(), program.getDepartmentId(), Department.class); pBuilder.add("courseOfferedToDepartmentId", offeredToDepartment.getId()); pBuilder.add("courseOfferedToDepartmentName", offeredToDepartment.getShortName()); }