Пример #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);
  }