コード例 #1
0
  public void testSelectCustomer() throws Exception {
    if (logger.isDebugEnabled()) {
      logger.debug("testSelectCustomer() - start");
    }

    Session s = HibernateUtil.currentSession();

    Criteria criteria = s.createCriteria(Customer.class);
    List returnlist = criteria.list();

    for (int i = 0; i < returnlist.size(); i++) {
      Customer customer = (Customer) returnlist.get(i);
      assertNotNull("Customer " + i + " Objekt ist null", customer);
      assertNotNull("Customer " + i + " Person ist null", customer.getPerson());
      logger.debug("Customer " + i + ": " + customer + " Person: " + customer.getPerson());
      s.delete(customer);
    }
    s.flush();
    returnlist = criteria.list();
    logger.debug("Anzahl Customer nach Löschen: " + returnlist.size());

    criteria = s.createCriteria(Person.class);
    returnlist = criteria.list();
    logger.debug("Anzahl Personen nach Löschen: " + returnlist.size());

    if (logger.isDebugEnabled()) {
      logger.debug("testSelectCustomer() - end");
    }
  }
コード例 #2
0
  public void testCreateCustomer() throws Exception {
    if (logger.isDebugEnabled()) {
      logger.debug("testCreateCustomer() - start");
    }

    // get new Session and begin Transaction
    Session s = HibernateUtil.currentSession();
    Transaction tx = null;
    try {
      tx = s.beginTransaction();

      try {
        // is DB open and connected
        assertTrue("Connected to Db? ", s.isConnected());
        assertTrue("Db Open? ", s.isOpen());

        List data = new ArrayList();

        List customer = new ArrayList();

        // create new objects

        Customer c = new Customer();
        c.setUserName("kunde1");
        c.setUserPassword("kunde1");
        c.setHiddenQuestion(" ");
        c.setHiddenAnswer(" ");

        Person person = new Person();
        person.setFirstName("Philipp");
        person.setLastName("Schneider");
        person.setStreet("Kastanienring");
        person.setStreetNumber("16");
        person.setCity("Leipzig");
        person.setZipCode("04316");
        person.setCountry("Deutschland");
        person.setSalutation("Herr");
        person.setEmailAddress("*****@*****.**");

        Calendar birthday = Calendar.getInstance();
        birthday.set(Calendar.DAY_OF_MONTH, 10);
        birthday.set(Calendar.MONTH, 8);
        birthday.set(Calendar.YEAR, 1988);

        person.setDayOfBirth(birthday);

        c.setPerson(person);
        c.setDayOfRegistration(Calendar.getInstance());
        customer.add(c);

        // second Customer
        c = new Customer();
        c.setUserName("kunde2");
        c.setUserPassword("kunde2");
        c.setHiddenQuestion(" ");
        c.setHiddenAnswer(" ");

        person = new Person();
        person.setFirstName("Max");
        person.setLastName("Meier");
        person.setStreet("Kohlgartenstr.");
        person.setStreetNumber("67");
        person.setCity("Wernigerode");
        person.setZipCode("38855");
        person.setCountry("Deutschland");
        person.setSalutation("Herr");
        person.setEmailAddress("*****@*****.**");

        birthday = Calendar.getInstance();
        birthday.set(Calendar.DAY_OF_MONTH, 2);
        birthday.set(Calendar.MONTH, 2);
        birthday.set(Calendar.YEAR, 1954);
        person.setDayOfBirth(birthday);
        c.setPerson(person);
        c.setDayOfRegistration(Calendar.getInstance());
        customer.add(c);

        // third Customer
        c = new Customer();
        c.setUserName("kunde3");
        c.setUserPassword("kunde3");
        c.setHiddenQuestion(" ");
        c.setHiddenAnswer(" ");

        person = new Person();
        person.setFirstName("Thomas");
        person.setLastName("Pech");
        person.setStreet("Riebeck.");
        person.setStreetNumber("7a");
        person.setCity("Leipzig");
        person.setZipCode("04317");
        person.setCountry("Deutschland");
        person.setSalutation("Herr");
        person.setEmailAddress("*****@*****.**");

        birthday = Calendar.getInstance();
        birthday.set(Calendar.DAY_OF_MONTH, 10);
        birthday.set(Calendar.MONTH, 8);
        birthday.set(Calendar.YEAR, 1988);
        person.setDayOfBirth(birthday);
        c.setDayOfRegistration(Calendar.getInstance());
        c.setPerson(person);
        customer.add(c);

        List ids = new ArrayList();

        // save objects
        for (Iterator iter = customer.iterator(); iter.hasNext(); ) {
          Customer custe = (Customer) iter.next();
          ids.add((Integer) s.save(custe));
        }
        s.flush();

        for (int i = 0; i < ids.size(); i++) {

          int id = ((Integer) ids.get(i)).intValue();

          // get Condition from Hibernate
          Customer dbc = (Customer) s.get(Customer.class, new Integer(id));
          assertNotNull("Can't get Customer" + id + " from DB", dbc);
          if (dbc == null) {
            if (logger.isDebugEnabled()) {
              logger.debug("testCreateCustomer() Kein object mit id " + id + "gefunden.");
            }
            return;
          }
          logger.debug("Customer: " + dbc + " Person:" + dbc.getPerson());

          Customer myc = (Customer) customer.get(i);
          // are both equal?
          assertEquals(
              "Select: Customer aus DB nicht gleich meiner. DB: " + dbc + " My:" + myc, myc, dbc);
        }

        tx.commit();

      } catch (Exception e) {
        if (tx != null) {
          logger.error(
              "testCreateCustomer() - Something went wrong here; discard all partial changes", e);

          // Something went wrong; discard all partial changes
          tx.rollback();
        }
      }

    } catch (Exception e) {
      logger.error("testCreateCustomer() - Error while trying to beginTransaction", e);
      throw e;
    } finally {
      // No matter what, close the session
      HibernateUtil.closeSession();
    }

    if (logger.isDebugEnabled()) {
      logger.debug("testCreateCustomer() - end");
    }
  }