@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[]) { Employee hrManager = new Employee( "1", "SpongeBob", "SquarePants", "HR Manager", new Date(System.currentTimeMillis())); Department hr = new Department(); hr.setManager(hrManager); hr.setName("Humane Resources"); try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("hr.ser"))) { out.writeObject(hr); System.out.println("Wrote \"" + hr.getName() + "\" to file"); } catch (IOException e) { System.err.println("Error writing object: " + e.getMessage()); } }
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)); } }
/** * Method to call when we want to apply (grant or deny) an agreement visa * * @param employee * @param visaStatus * @param comment * @return */ public Request applyAgreementVisa( Employee employee, RequestAgreementVisaStatus visaStatus, String comment) { if (!waitsForAnAgreementVisa()) { throw REQUEST_DOES_NOT_EXPECT_ANY_AGREEMENT_VISA.exception(); } if (userHasAlreadyAppliedAVisa(employee.getId())) { throw EMPLOYEE_HAS_ALREADY_APPLIED_A_VISA.exception(employee.getFullName()); } AgreementRuleVisaAud nextExpectedVisa = getNextExpectedAgreementVisa() .orElseThrow(() -> COULD_NOT_FIND_NEXT_EXPECTED_AGREEMENT_VISA.exception()); Department department = employee.getDepartment(); Seniority seniority = employee.getSeniority(); if (!nextExpectedVisa.canBeAppliedBy(department, seniority)) { throw VISA_TO_APPLY_DOESNT_MATCH_NEXT_EXPECTED_ONE.exception( department.getName(), seniority.getValue(), nextExpectedVisa.getDepartment().getName(), nextExpectedVisa.getSeniority().getValue()); } LocalDateTime now = LocalDateTime.now(); RequestAgreementVisa appliedVisa = RequestAgreementVisa.build( this, null, employee, visaStatus, nextAgreementVisaRank, comment, department, seniority, now); addAgreementVisa(appliedVisa); updateUser = employee.getUid(); updateDate = now; return this; }
@Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", department=" + department.getName() + "]"; }