public void testLoadingStrategies() { Session s = openSession(); s.beginTransaction(); Customer cust = new Customer("Acme, Inc."); Order order = new Order(new Order.Id(cust, 1)); cust.getOrders().add(order); s.save(cust); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); cust = (Customer) s.get(Customer.class, cust.getId()); assertEquals(1, cust.getOrders().size()); s.clear(); cust = (Customer) s.createQuery("from Customer").uniqueResult(); assertEquals(1, cust.getOrders().size()); s.clear(); cust = (Customer) s.createQuery("from Customer c join fetch c.orders").uniqueResult(); assertEquals(1, cust.getOrders().size()); s.clear(); s.delete(cust); s.getTransaction().commit(); s.close(); }
public static void createTestData(EntityManager em) { em.getTransaction().begin(); // Create customer 1 Customer customer1 = new Customer(); customer1.setName("Customer1"); customer1.setId(1); // Create customer 2 Customer customer2 = new Customer(); customer2.setName("Customer2"); customer2.setId(2); // Create customer 3 Customer customer3 = new Customer(); customer3.setName("Customer3"); customer3.setId(3); // Create 3 orders Order order1 = new Order(); order1.setAddress("Address of order #1, Brazil"); order1.setId(1); Order order2 = new Order(); order2.setAddress("Address of order #2, USA"); order2.setId(2); Order order3 = new Order(); order3.setAddress("Address of order #3, Korea"); order3.setId(3); // Since we are not using Cascade, we need to manaully persist orders em.persist(order1); em.persist(order2); em.persist(order3); // The first 2 orders belong to customer 1 customer1.getOrders().add(order1); order1.setCustomer(customer1); customer1.getOrders().add(order2); order2.setCustomer(customer1); // The 3rd order belongs to customer 2 customer2.getOrders().add(order3); order3.setCustomer(customer2); em.persist(customer1); em.persist(customer2); em.persist(customer3); em.getTransaction().commit(); }
@Override public int compareTo(Order o) { if (o == null) { throw new IllegalArgumentException("The object to compare is null"); } if (flavor < o.flavor) { return -1; } else if (flavor == o.flavor) { if (unmalt < o.unmalt) { return -1; } else if (unmalt > o.unmalt) { return 1; } else { int a = customer.getOrders().size(); int b = o.customer.getOrders().size(); if (a < b) { return -1; } else if (a > b) { return 1; } } return 0; } else { return 1; } }
public static void addOrderToDB(Order o) { Customer customer = CustomerServiceImpl.getCustomer(o.getCustomerID()); customer.setOrders(OrderServiceImpl.retrieveUnpaidOrders(customer)); customer.addOrder(o); if (customer.getOrders().contains(o)) { orderDAO.addOrder(o); List<OrderItem> items = o.getItems(); Inventory inventory = new Inventory(InventoryServiceImpl.getAllAvailableProductsInDB()); for (OrderItem i : items) { Product product = ProductServiceImpl.getProduct(i.getItemSKUNumber()); InventoryItem inventoryitem = InventoryServiceImpl.getInventoryItem(inventory, product); InventoryServiceImpl.deductFromInventory(inventory, inventoryitem, i.getQuantity()); } } }
public void testSaveCascadedToKeyManyToOne() { // test cascading a save to an association with a key-many-to-one which refers to a // just saved entity Session s = openSession(); s.beginTransaction(); Customer cust = new Customer("Acme, Inc."); Order order = new Order(new Order.Id(cust, 1)); cust.getOrders().add(order); s.save(cust); s.flush(); assertEquals(2, sfi().getStatistics().getEntityInsertCount()); s.delete(cust); s.getTransaction().commit(); s.close(); }
/* * Some data to play with. */ static void saveData(Database db) throws DatabaseException { Customer customer = new Customer( "jjohnson", "mypasswd5", "John", "Johnson", "ABCProducts", "*****@*****.**"); customer.getOrders().add(new Order("NFL Broncos Coffee Cup", 100, 1.00, "unverified")); db.saveObject(customer); customer = new Customer( "deisenhower", "mypasswd5", "Dwight", "Eisenhower", "United States", "*****@*****.**"); customer .getSupport() .add( new Support( "Request", "New", "no phone", "*****@*****.**", "Can I have my bust on a dollar, please.")); customer .getSupport() .add( new Support( "Response", "Pending", "no phone", "*****@*****.**", "Yes, but you may have to share it.")); customer .getSupport() .add( new Support( "Request", "New", "no phone", "*****@*****.**", "Share it with who?")); customer .getOrders() .add( new Order( "Dwight D. Eisenhower Dollar", new Integer(100), new Double(1.00), "unverified")); customer .getOrders() .add(new Order("Susan B. Anthony Dollar", new Integer(10), new Double(1.52), "unverified")); db.saveObject(customer); customer = new Customer( "ssmith", "mypasswd", "Scott", "Smith", "ABCProducts", "*****@*****.**"); customer .getOrders() .add(new Order("NFL Seattle Seahawks Team T-Shirts", 150, 12.00, "unverified")); db.saveObject(customer); customer = new Customer( "ssmith2", "mypasswd", "Scott", "Smith2", "ABCProducts", "*****@*****.**"); customer .getOrders() .add(new Order("NFL Denver Broncos Team T-Shirts", 150, 12.00, "unverified")); db.saveObject(customer); }