コード例 #1
0
  /**
   * With delete-orphan cascade. The cascade=”delete-orphan” is declared in ‘billingDetails’ to
   * enable the delete orphan cascade effect. When you save or update the User, it will remove those
   * ‘billingDetails’ which already mark as removed.
   *
   * <p>In short, delete-orphan allow parent table to delete few records (delete orphan) in its
   * child table.
   */
  public void /*test*/ BillingDetailsWithDeleteOrphanCascade() throws Exception {
    LogHelper.disableLogging();
    User u1 = new User("Christian", "Bauer", "turin", "abc123", "*****@*****.**");
    u1.setAddress(new Address("Foo", "12345", "Bar"));
    u1.setAdmin(true);

    HibernateUtil.getSession().save(u1);

    BillingDetails ccOne =
        new CreditCard(
            "Christian  Bauer", u1, "1234567890", CreditCardType.MASTERCARD, "10", "2005");
    u1.addBillingDetails(ccOne);

    HibernateUtil.getSession().save(ccOne);

    HibernateUtil.getSession().flush();

    UserDAO userDAO = new UserDAO();
    User user = userDAO.loadUserById(1l, false);

    Set details = user.getBillingDetails();

    for (Object detail : details) {
      LogHelper.setLogging("org.hibernate.SQL", Level.DEBUG);

      user.getBillingDetails().remove(detail);
      user.setDefaultBillingDetails(null);

      LogHelper.disableLogging();
    }

    LogHelper.setLogging("org.hibernate.SQL", Level.DEBUG);
    HibernateUtil.getSession().saveOrUpdate(u1);
    HibernateUtil.getSession().flush();
  }
コード例 #2
0
  /**
   * With save-update cascade. The cascade=”save-update” is declared in ‘BillingDetails’ to enable
   * the save-update cascade effect..
   */
  public void /*test*/ BillingDetailsCascadeSaveUpdate() {
    User u1 = new User("Christian", "Bauer", "turin", "abc123", "*****@*****.**");
    u1.setAddress(new Address("Foo", "12345", "Bar"));
    u1.setAdmin(true);

    BillingDetails ccOne =
        new CreditCard(
            "Christian  Bauer", u1, "1234567890", CreditCardType.MASTERCARD, "10", "2005");
    u1.addBillingDetails(ccOne);

    HibernateUtil.getSession().save(u1);
  }
コード例 #3
0
  public void /*test*/ UserWithItems() throws Exception {
    System.out.println("******************** testUserWithItems ********************");

    UserDAO userDAO = new UserDAO();
    assertTrue(userDAO.findAll().size() > 0);

    User user = new User();
    user.setFirstname("Christian");
    user.setAdmin(true);
    user.clearCreatedDate();

    Collection users = userDAO.findByExample(user);

    assertNotNull(users);
    Object[] userArr = users.toArray();
    assertTrue(userArr.length > 0);
    assertTrue(userArr[0] instanceof User);

    User christian = (User) userArr[0];
    assertNotNull(christian.getId());

    Set items = christian.getItems();
    Item item = (Item) items.toArray()[0];
    assertNotNull(item.getId());

    Calendar inThreeDays = GregorianCalendar.getInstance();
    inThreeDays.roll(Calendar.DAY_OF_YEAR, 3);
    Item newItem =
        new Item(
            "Item One",
            "An item in the carsLuxury category.",
            christian,
            new MonetaryAmount(new BigDecimal("1.99"), Currency.getInstance(Locale.US)),
            new MonetaryAmount(new BigDecimal("50.33"), Currency.getInstance(Locale.US)),
            new Date(),
            inThreeDays.getTime());

    christian.addItem(newItem);

    ItemDAO itemDAO = new ItemDAO();
    itemDAO.makePersistent(newItem);

    HibernateUtil.commitTransaction();
  }