Exemplo n.º 1
0
  public void /*test*/ UserItemsInitialization() throws Exception {
    System.out.println("******************** testUserItemsInitialization ********************");
    UserDAO userDAO = new UserDAO();
    User user = userDAO.getUserById(1l, false);

    assertFalse(Hibernate.isInitialized(user.getItems()));
    Set<Item> items = user.getItems();
    assertFalse(Hibernate.isInitialized(items));

    Item item = (Item) items.toArray()[0];
    assertTrue(Hibernate.isInitialized(items));
    assertNotNull(item.getName());

    User user2 = userDAO.getUserById(1l, false);
    assertTrue(user == user2);

    userDAO.evict(user);

    User user3 = userDAO.getUserById(1l, false);
    assertFalse(user == user3);

    items = user3.getItems();
    assertFalse(Hibernate.isInitialized(items));
    Hibernate.initialize(items);
    assertTrue(Hibernate.isInitialized(items));
  }
Exemplo n.º 2
0
  /**
   * fetch=”join” or @Fetch(FetchMode.JOIN). The “join” fetching strategy will disabled the lazy
   * loading of all it’s related collections. Let see the example.
   */
  public void /*test*/ UserFetchingStrategies() {
    System.out.println("******************** testUserFetchingStrategies *******************");
    UserDAO userDAO = new UserDAO();
    User user = userDAO.loadUserById(1l, false);

    for (Iterator iter = user.getItems().iterator(); iter.hasNext(); ) {
      Item item = (Item) iter.next();
      assertNotNull(item.getId());
      assertNotNull(item.getName());
    }
  }
Exemplo n.º 3
0
  /**
   * fetch=”subselect” or @Fetch(FetchMode.SUBSELECT).
   *
   * <p>This fetching strategy is enable all its related collection in a sub select statement. Let
   * see the same query again.
   *
   * <p>With “subselect” enabled, it will create two select statements.
   *
   * <p>1. Select statement to retrieve all the Stock records.
   *
   * <p>2. Select all its related collections in a sub select query.
   */
  @org.junit.Test
  public void /*test*/ UserBatchItems() {
    System.out.println("******************* testUserBatchItems ********************");
    Session session = HibernateUtil.getSession();
    List<User> list = session.createQuery("from User").list();

    for (User user : list) {
      Set items = user.getItems();

      for (Iterator iter = items.iterator(); iter.hasNext(); ) {
        Item item = (Item) iter.next();
        assertNotNull(item.getId());
        assertNotNull(item.getName());
      }
    }
  }
Exemplo n.º 4
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();
  }