@Test
  public void testTopLevelManyToOneChangedToNewEntity() {
    Item item1 = new Item();
    item1.setName("item1 name");
    Category category = new Category();
    category.setName("category");

    item1.setCategory(category);
    category.setExampleItem(item1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    // change many-to-one in top level to be a new (transient)
    Category categoryNewer = new Category();
    categoryNewer.setName("newer category");
    item1.setCategory(categoryNewer);

    // put the other representation in categoryNewer
    categoryNewer.setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // the many-to-one from the top level item will win.
    assertEquals(categoryNewer.getName(), item1Merged.getCategory().getName());
    assertSame(item1Merged, item1Merged.getCategory().getExampleItem());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(categoryNewer.getName(), item1.getCategory().getName());
    assertSame(item1, item1.getCategory().getExampleItem());
    // make sure original category is still there
    Category categoryQueried =
        (Category) s.createQuery("from Category c where c.name='category'").uniqueResult();
    assertNotNull(categoryQueried);
    // make sure original category has the same item.
    assertSame(item1, categoryQueried.getExampleItem());
    // set exampleItem to null to avoid constraint violation on cleanup.
    categoryQueried.setExampleItem(null);
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  public void testNestedManyToOneChangedToNewEntity() {
    Item item1 = new Item();
    item1.setName("item1 name");
    Category category = new Category();
    category.setName("category");

    item1.setCategory(category);
    category.setExampleItem(item1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    // change many-to-one in nested entity to a new (transient) value
    Category categoryNew = new Category();
    categoryNew.setName("new category");
    item1_1.setCategory(categoryNew);
    item1.getCategory().setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // the many-to-one from the top level item will win.
    assertEquals(category.getName(), item1Merged.getCategory().getName());
    assertSame(item1Merged, item1Merged.getCategory().getExampleItem());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(category.getName(), item1.getCategory().getName());
    assertSame(item1, item1.getCategory().getExampleItem());
    // make sure new category got persisted
    Category categoryQueried =
        (Category) s.createQuery("from Category c where c.name='new category'").uniqueResult();
    assertNotNull(categoryQueried);
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  public void testTopLevelEntityNewerThanNested() {
    Item item = new Item();
    item.setName("item");

    Category category = new Category();
    category.setName("category");

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item);
    s.persist(category);
    tx.commit();
    s.close();

    // Get the Category from a different session.
    s = openSession();
    tx = s.beginTransaction();
    Category category1_2 = (Category) s.get(Category.class, category.getId());
    tx.commit();
    s.close();

    // Get and update the same Category.
    s = openSession();
    tx = s.beginTransaction();
    Category category1_1 = (Category) s.get(Category.class, category.getId());
    category1_1.setName("new name");
    tx.commit();
    s.close();

    assertTrue(category1_2.getVersion() < category1_1.getVersion());

    category1_1.setExampleItem(item);
    item.setCategory(category1_2);

    s = openSession();
    tx = s.beginTransaction();
    try {
      // representation merged at top level is newer than nested representation.
      category1_1 = (Category) s.merge(category1_1);
      fail("should have failed because one representation is an older version.");
    } catch (StaleObjectStateException ex) {
      // expected
    } finally {
      tx.rollback();
      s.close();
    }

    cleanup();
  }
  @Before
  public void setUp() throws Exception {
    Category category_one = new Category();
    Category category_two = new Category();
    category_one.setName("Breakfast");
    category_one.setName("Dinner");

    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction transaction = session.beginTransaction();
    session.save(category_one);
    session.save(category_two);
    transaction.commit();
    session.close();
  }
Example #5
0
 public static Category createCategory(EOEditingContext editingContext, Integer id, String name) {
   Category eo =
       (Category) EOUtilities.createAndInsertInstance(editingContext, _Category.ENTITY_NAME);
   eo.setId(id);
   eo.setName(name);
   return eo;
 }
 @Test
 public void itAddsNewCategories() {
   Category category = new Category();
   category.setName("Salads");
   categoryDAO.create(category);
   assertEquals("Salads", categoryDAO.find((long) 3).getName());
 }
 @Test
 public void itUpdatesCategory() {
   Category category = categoryDAO.find((long) 1);
   category.setName("Salads");
   categoryDAO.update(category);
   assertEquals("Salads", categoryDAO.find((long) 1).getName());
 }
  /**
   * Gets the category by event.
   *
   * @param event_id the event_id
   * @return the category by event
   */
  public Category getCategoryByEvent(long event_id) {
    Category ct = new Category();
    String selectQuery =
        "SELECT  * FROM "
            + TABLE_CATEGORY
            + " tc, "
            + TABLE_EVENT
            + " te, "
            + TABLE_CATEGORY_EVENT
            + " ce WHERE te."
            + KEY_ID
            + " = "
            + event_id
            + " AND tc."
            + KEY_ID
            + " = "
            + "ce."
            + KEY_CATEGORY_ID
            + " AND te."
            + KEY_ID
            + " = "
            + "ce."
            + KEY_EVENT_ID;

    Log.e("In getCategoryByEvent", selectQuery);
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    if (c.moveToFirst()) {
      ct.setId(c.getLong((c.getColumnIndex(KEY_CATEGORY_ID))));
      ct.setName(c.getString(c.getColumnIndex(KEY_CATEGORY_NAME)));
      ct.setColor(c.getString(c.getColumnIndex(KEY_CATEGORY_COLOR)));
    }
    return ct;
  }
Example #9
0
  private Category createCategory(Organization organization) {
    Category category = new Category();
    category.setName("Test Category");
    category.setOrganization(organization);

    return category;
  }
  @Before
  public void setUp() {
    category1 = new Category();
    category1.setId(1L);
    category1.setColor("#ffffff");
    category1.setCount(10);
    category1.setDescription("first cat description");
    category1.setName("first cat name");

    category2 = new Category();
    category2.setId(2L);
    category2.setColor("#000000");
    category2.setCount(5);
    category2.setDescription("second cat description");
    category2.setName("second cat name");
  }
  @Test
  public void testMergeMultipleEntityCopiesAllowedAndDisallowed() {
    Item item1 = new Item();
    item1.setName("item1 name");
    Category category = new Category();
    category.setName("category");
    item1.setCategory(category);
    category.setExampleItem(item1);

    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(item1);
    em.getTransaction().commit();
    em.close();

    // get another representation of item1
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Item item1_1 = em.find(Item.class, item1.getId());
    // make sure item1_1.category is initialized
    Hibernate.initialize(item1_1.getCategory());
    em.getTransaction().commit();
    em.close();

    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Item item1Merged = em.merge(item1);

    // make sure item1Merged.category is also managed
    Hibernate.initialize(item1Merged.getCategory());

    item1Merged.setCategory(category);
    category.setExampleItem(item1_1);

    // now item1Merged is managed and it has a nested detached item
    // and there is  multiple managed/detached Category objects
    try {
      // the following should fail because multiple copies of Category objects is not allowed by
      // CustomEntityCopyObserver
      em.merge(item1Merged);
      fail(
          "should have failed because CustomEntityCopyObserver does not allow multiple copies of a Category. ");
    } catch (IllegalStateException ex) {
      // expected
    } finally {
      em.getTransaction().rollback();
    }
    em.close();

    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    item1 = em.find(Item.class, item1.getId());
    assertEquals(category.getName(), item1.getCategory().getName());
    assertSame(item1, item1.getCategory().getExampleItem());
    em.getTransaction().commit();
    em.close();

    cleanup();
  }
Example #12
0
 public Category mapRow(ResultSet rs, int rowNum) throws SQLException {
   Category category = new Category();
   category.setId(rs.getLong("id"));
   category.setName(rs.getString("name"));
   category.setShowOnSalesScreen(rs.getBoolean("showOnSalesScreen"));
   category.setThumbnailId(rs.getLong("thumbnailId"));
   return category;
 }
Example #13
0
  private void createCategories(final Context context) {
    persistence.runInTransaction(
        em -> {
          Category category;

          category = metadata.create(Category.class);
          category.setName("Housekeeping");
          category.setCatType(CategoryType.EXPENSE);
          em.persist(category);
          context.expenseCategories.add(category);

          category = metadata.create(Category.class);
          category.setName("Hobby");
          category.setCatType(CategoryType.EXPENSE);
          em.persist(category);
          context.expenseCategories.add(category);

          category = metadata.create(Category.class);
          category.setName("Travel");
          category.setCatType(CategoryType.EXPENSE);
          em.persist(category);
          context.expenseCategories.add(category);

          category = metadata.create(Category.class);
          category.setName("Food");
          category.setCatType(CategoryType.EXPENSE);
          em.persist(category);
          context.expenseCategories.add(category);

          category = metadata.create(Category.class);
          category.setName("Clothes");
          category.setCatType(CategoryType.EXPENSE);
          em.persist(category);
          context.expenseCategories.add(category);

          category = metadata.create(Category.class);
          category.setName("Car");
          category.setCatType(CategoryType.EXPENSE);
          em.persist(category);
          context.expenseCategories.add(category);

          category = metadata.create(Category.class);
          category.setName("Salary");
          category.setCatType(CategoryType.INCOME);
          em.persist(category);
          context.salaryCategory = category;

          category = metadata.create(Category.class);
          category.setName("Other");
          category.setCatType(CategoryType.INCOME);
          em.persist(category);
          context.otherIncomeCategory = category;
        });
  }
  @Test
  @FailureExpected(jiraKey = "HHH-9239")
  public void testNestedUnidirOneToManyBackrefWithRemovedElement() {
    Item item1 = new Item();
    item1.setName("item1 name");
    SubItem subItem1 = new SubItem();
    subItem1.setName("subItem1 name");
    item1.getSubItemsBackref().add(subItem1);
    SubItem subItem2 = new SubItem();
    subItem2.setName("subItem2 name");
    item1.getSubItemsBackref().add(subItem2);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    Hibernate.initialize(item1_1.getSubItemsBackref());
    tx.commit();
    s.close();

    // remove subItem1 from the nested Item
    item1_1.getSubItemsBackref().remove(subItem1);

    Category category = new Category();
    category.setName("category");
    item1.setCategory(category);
    category.setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // the element should have been removed
    assertEquals(1, item1Merged.getSubItemsBackref().size());
    assertTrue(item1Merged.getSubItemsBackref().contains(subItem2));
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(1, item1.getSubItemsBackref().size());
    assertTrue(item1.getSubItemsBackref().contains(subItem2));
    // because cascade includes "delete-orphan" the removed SubItem should have been deleted.
    subItem1 = (SubItem) s.get(SubItem.class, subItem1.getId());
    assertNull(subItem1);
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  @FailureExpected(jiraKey = "HHH-9239")
  public void testNestedUnidirOneToManyBackrefWithNewElement() {
    Item item1 = new Item();
    item1.setName("item1 name");
    SubItem subItem1 = new SubItem();
    subItem1.setName("subItem1 name");
    item1.getSubItemsBackref().add(subItem1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    Hibernate.initialize(item1_1.getSubItemsBackref());
    tx.commit();
    s.close();

    Category category = new Category();
    category.setName("category");
    item1.setCategory(category);

    // Add a new SubItem to the Item representation that will be in a nested association.
    SubItem subItem2 = new SubItem();
    subItem2.setName("subItem2 name");
    item1_1.getSubItemsBackref().add(subItem2);

    category.setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // The resulting collection should contain the added element
    assertEquals(2, item1Merged.getSubItemsBackref().size());
    assertEquals("subItem1 name", item1Merged.getSubItemsBackref().get(0).getName());
    assertEquals("subItem2 name", item1Merged.getSubItemsBackref().get(1).getName());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(2, item1.getSubItemsBackref().size());
    assertEquals("subItem1 name", item1.getSubItemsBackref().get(0).getName());
    assertEquals("subItem2 name", item1.getSubItemsBackref().get(1).getName());
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  // @FailureExpected( jiraKey = "HHH-9106" )
  public void testTopLevelUnidirOneToManyBackrefWithRemovedElement() {
    Item item1 = new Item();
    item1.setName("item1 name");
    SubItem subItem1 = new SubItem();
    subItem1.setName("subItem1 name");
    item1.getSubItemsBackref().add(subItem1);
    SubItem subItem2 = new SubItem();
    subItem2.setName("subItem2 name");
    item1.getSubItemsBackref().add(subItem2);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    assertFalse(Hibernate.isInitialized(item1_1.getSubItemsBackref()));

    Category category = new Category();
    category.setName("category");

    item1.setCategory(category);
    category.setExampleItem(item1_1);

    // remove subItem1 from top-level Item
    item1.getSubItemsBackref().remove(subItem1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // entity should have been removed
    assertEquals(1, item1Merged.getSubItemsBackref().size());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(1, item1.getSubItemsBackref().size());
    subItem1 = (SubItem) s.get(SubItem.class, subItem1.getId());
    // cascade does not include delete-orphan, so subItem1 should still be persistent.
    assertNotNull(subItem1);
    tx.commit();

    cleanup();
  }
  @Test
  // @FailureExpected( jiraKey = "HHH-9106" )
  public void testTopLevelUnidirOneToManyNoBackrefWithRemovedElement() {
    Category category1 = new Category();
    category1.setName("category1 name");
    SubCategory subCategory1 = new SubCategory();
    subCategory1.setName("subCategory1 name");
    category1.getSubCategories().add(subCategory1);
    SubCategory subCategory2 = new SubCategory();
    subCategory2.setName("subCategory2 name");
    category1.getSubCategories().add(subCategory2);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(category1);
    tx.commit();
    s.close();

    // get another representation of category1
    s = openSession();
    tx = s.beginTransaction();
    Category category1_1 = (Category) s.get(Category.class, category1.getId());
    tx.commit();
    s.close();

    assertFalse(Hibernate.isInitialized(category1_1.getSubCategories()));

    Item item = new Item();
    item.setName("item");
    category1.setExampleItem(item);
    item.setCategory(category1_1);

    category1.getSubCategories().remove(subCategory1);

    s = openSession();
    tx = s.beginTransaction();
    Category category1Merged = (Category) s.merge(category1);
    assertEquals(1, category1Merged.getSubCategories().size());
    assertTrue(category1Merged.getSubCategories().contains(subCategory2));
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    category1 = (Category) s.get(Category.class, category1.getId());
    assertEquals(1, category1.getSubCategories().size());
    assertTrue(category1.getSubCategories().contains(subCategory2));
    // cascade does not include delete-orphan, so subCategory1 should still be persistent.
    subCategory1 = (SubCategory) s.get(SubCategory.class, subCategory1.getId());
    assertNotNull(subCategory1);
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  @FailureExpected(jiraKey = "HHH-9239")
  public void testNestedUnidirOneToManyNoBackrefWithRemovedElement() {
    Category category1 = new Category();
    category1.setName("category1 name");
    SubCategory subCategory1 = new SubCategory();
    subCategory1.setName("subCategory1 name");
    category1.getSubCategories().add(subCategory1);
    SubCategory subCategory2 = new SubCategory();
    subCategory2.setName("subCategory2 name");
    category1.getSubCategories().add(subCategory2);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(category1);
    tx.commit();
    s.close();

    // get another representation of category1
    s = openSession();
    tx = s.beginTransaction();
    Category category1_1 = (Category) s.get(Category.class, category1.getId());
    Hibernate.initialize(category1_1.getSubCategories());
    tx.commit();
    s.close();

    category1_1.getSubCategories().remove(subCategory2);

    Item item = new Item();
    item.setName("item");
    category1.setExampleItem(item);
    item.setCategory(category1_1);

    s = openSession();
    tx = s.beginTransaction();
    Category category1Merged = (Category) s.merge(category1);
    assertEquals(1, category1Merged.getSubCategories().size());
    assertTrue(category1Merged.getSubCategories().contains(subCategory2));
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    category1 = (Category) s.get(Category.class, category1.getId());
    assertEquals(1, category1.getSubCategories().size());
    assertTrue(category1.getSubCategories().contains(subCategory2));
    subCategory1 = (SubCategory) s.get(SubCategory.class, subCategory1.getId());
    assertNull(subCategory1);
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  @FailureExpected(jiraKey = "HHH-9240")
  public void testTopLevelUnidirOneToManyBackrefWithNewElement() {
    Item item1 = new Item();
    item1.setName("item1 name");
    SubItem subItem1 = new SubItem();
    subItem1.setName("subItem1 name");
    item1.getSubItemsBackref().add(subItem1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    assertFalse(Hibernate.isInitialized(item1_1.getSubItemsBackref()));

    Category category = new Category();
    category.setName("category");

    SubItem subItem2 = new SubItem();
    subItem2.setName("subItem2 name");
    item1.getSubItemsBackref().add(subItem2);

    item1.setCategory(category);
    category.setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    // The following will fail due to PropertyValueException because item1  will
    // be removed from the inverted merge map when the operation cascades to item1_1.
    Item item1Merged = (Item) s.merge(item1);
    // top-level collection should win
    assertEquals(2, item1.getSubItemsBackref().size());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(2, item1.getSubItemsBackref().size());
    tx.commit();
    s.close();

    cleanup();
  }
Example #20
0
  public static void main(String[] args) {
    Category c1 = new Category();
    c1.setChildCategories(new HashSet<Category>());
    c1.setName("zhangsan");
    Category c2 = null;

    Session session = sessionFactory.openSession();
    Transaction tx = null;
    Long id = null;
    try {
      tx = session.beginTransaction();
      id = (Long) session.save(c1);
      System.out.println(c1.getId());

      c1.setName("lisi"); // 可以看出在commit之前,都没有写到数据库中,都是对缓存的操作,也就是都是对transient对象的操作
      c1 = null;
      c2 = (Category) session.get(Category.class, id);
      tx.commit();

    } catch (Exception e) {
      if (null != tx) tx.rollback();
      e.printStackTrace();
    } finally {
      session.close();
    }

    try {
      session = sessionFactory.openSession();
      tx = session.beginTransaction();
      session.update(c2);
      c2.setName("wangwu");
      tx.commit();

    } catch (Exception e) {
      if (null != tx) tx.rollback();
      e.printStackTrace();
    } finally {
      session.close();
    }
  }
  private static Category createCategory(long id, String name) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();

    Category value = new Category();
    value.setId(id);
    value.setName(name);

    em.persist(value);
    em.getTransaction().commit();
    em.close();
    return value;
  }
  @Test
  public void listContainsNote() {
    List<Category> categories = new ArrayList<>();
    categories.add(category1);
    categories.add(category2);
    assertTrue(categories.contains(category2));
    assertTrue(categories.contains(category1));

    Category newCategory = new Category();
    newCategory.setName("newCat");
    newCategory.setDescription("newCat desc");
    newCategory.setColor("#cccccc");
    assertFalse(categories.contains(newCategory));
  }
  @Test
  // @FailureExpected( jiraKey = "HHH-9106" )
  public void testTopLevelUnidirOneToManyNoBackrefWithNewElement() {
    Category category1 = new Category();
    category1.setName("category1 name");
    SubCategory subCategory1 = new SubCategory();
    subCategory1.setName("subCategory1 name");
    category1.getSubCategories().add(subCategory1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(category1);
    tx.commit();
    s.close();

    // get another representation of category1
    s = openSession();
    tx = s.beginTransaction();
    Category category1_1 = (Category) s.get(Category.class, category1.getId());
    tx.commit();
    s.close();

    assertFalse(Hibernate.isInitialized(category1_1.getSubCategories()));

    SubCategory subCategory2 = new SubCategory();
    subCategory2.setName("subCategory2 name");
    category1.getSubCategories().add(subCategory2);

    Item item = new Item();
    item.setName("item");
    category1.setExampleItem(item);
    item.setCategory(category1_1);

    s = openSession();
    tx = s.beginTransaction();
    Category category1Merged = (Category) s.merge(category1);
    assertEquals(2, category1Merged.getSubCategories().size());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    category1 = (Category) s.get(Category.class, category1.getId());
    assertEquals(2, category1.getSubCategories().size());
    tx.commit();
    s.close();

    cleanup();
  }
  private Pet createRandomPet() {
    Pet pet = new Pet();
    pet.setId(TestUtils.nextId());
    pet.setName("gorilla");

    Category category = new Category();
    category.setName("really-happy");

    pet.setCategory(category);
    pet.setStatus(Pet.StatusEnum.AVAILABLE);
    List<String> photos =
        Arrays.asList(new String[] {"http://foo.bar.com/1", "http://foo.bar.com/2"});
    pet.setPhotoUrls(photos);

    return pet;
  }
  @Test
  public void testTopLevelManyToOneManagedNestedIsDetached() {
    Item item1 = new Item();
    item1.setName("item1 name");
    Category category = new Category();
    category.setName("category");
    item1.setCategory(category);
    category.setExampleItem(item1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);

    item1Merged.setCategory(category);
    category.setExampleItem(item1_1);

    // now item1Merged is managed and it has a nested detached item
    s.merge(item1Merged);
    assertEquals(category.getName(), item1Merged.getCategory().getName());
    assertSame(item1Merged, item1Merged.getCategory().getExampleItem());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(category.getName(), item1.getCategory().getName());
    assertSame(item1, item1.getCategory().getExampleItem());
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  public void testNestedDiffBasicProperty() {
    Item item1 = new Item();
    item1.setName("item1 name");
    Category category = new Category();
    category.setName("category");

    item1.setCategory(category);
    category.setExampleItem(item1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    // change basic property of nested entity
    item1_1.setName("item1_1 name");

    // change the nested Item to be the copy with the new name
    item1.getCategory().setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // the name from the top level item will win.
    assertEquals(item1.getName(), item1Merged.getName());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    Item item1Get = (Item) s.get(Item.class, item1.getId());
    assertEquals(item1.getName(), item1Get.getName());
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  public void testCategoryAndMovieIntegration() {
    Movie movie = new Movie();
    movie.setTitle("The Iron Man");
    movie.setBirthplace("EUA");
    movie.setReleaseYear(2014);
    movie.setSinopse("Very cool!!");
    movie.setStockQuantity(20);
    movie.setCoverPicture("");

    Category category = new Category();
    category.setName("Category test");

    category.addMovie(movie);

    Assert.assertNotNull(category.getMovieByTitle(movie.getTitle()));
  }
  @Test
  public void testNestedManyToOneChangedToNull() {
    Item item1 = new Item();
    item1.setName("item1 name");
    Category category = new Category();
    category.setName("category");

    item1.setCategory(category);
    category.setExampleItem(item1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    // change many-to-one in nested entity to null.
    item1_1.setCategory(null);
    item1.getCategory().setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // the many-to-one from the top level item will win.
    assertEquals(category.getName(), item1Merged.getCategory().getName());
    assertSame(item1Merged, item1Merged.getCategory().getExampleItem());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(category.getName(), item1.getCategory().getName());
    assertSame(item1, item1.getCategory().getExampleItem());
    tx.commit();
    s.close();

    cleanup();
  }
  /**
   * Gets the category id.
   *
   * @param categoryName the category name
   * @return the category id
   */
  public Category getCategoryID(String categoryName) {
    Category ct = new Category();
    String selectQuery =
        "SELECT  * FROM "
            + TABLE_CATEGORY
            + " tc WHERE tc."
            + KEY_CATEGORY_NAME
            + " = '"
            + categoryName
            + "'";
    Log.e(LOG, selectQuery);
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    if (c.moveToFirst()) {

      ct.setId(c.getInt((c.getColumnIndex(KEY_ID))));
      ct.setName(c.getString(c.getColumnIndex(KEY_CATEGORY_NAME)));
      ct.setColor(c.getString(c.getColumnIndex(KEY_CATEGORY_COLOR)));
    }
    return ct;
  }
  /**
   * Gets the all categories.
   *
   * @return the all categories
   */
  public List<Category> getAllCategories() {
    List<Category> categories = new ArrayList<Category>();
    String selectQuery = "SELECT  * FROM " + TABLE_CATEGORY;

    Log.e(LOG, selectQuery);
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
      do {
        Category ct = new Category();
        ct.setId(c.getInt((c.getColumnIndex(KEY_ID))));
        ct.setName(c.getString(c.getColumnIndex(KEY_CATEGORY_NAME)));
        ct.setColor(c.getString(c.getColumnIndex(KEY_CATEGORY_COLOR)));

        // adding to tags list
        categories.add(ct);
      } while (c.moveToNext());
    }
    return categories;
  }