コード例 #1
0
  @Test
  public void testDeleteWithSubquery() {
    // setup the test data...
    Session s = openSession();
    s.beginTransaction();
    SimpleEntityWithAssociation owner = new SimpleEntityWithAssociation("myEntity-1");
    owner.addAssociation("assoc-1");
    owner.addAssociation("assoc-2");
    owner.addAssociation("assoc-3");
    s.save(owner);
    SimpleEntityWithAssociation owner2 = new SimpleEntityWithAssociation("myEntity-2");
    owner2.addAssociation("assoc-1");
    owner2.addAssociation("assoc-2");
    owner2.addAssociation("assoc-3");
    owner2.addAssociation("assoc-4");
    s.save(owner2);
    SimpleEntityWithAssociation owner3 = new SimpleEntityWithAssociation("myEntity-3");
    s.save(owner3);
    s.getTransaction().commit();
    s.close();

    // now try the bulk delete
    s = openSession();
    s.beginTransaction();
    int count =
        s.createQuery(
                "delete SimpleEntityWithAssociation e where size( e.associatedEntities ) = 0 and e.name like '%'")
            .executeUpdate();
    assertEquals("incorrect delete count", 1, count);
    s.getTransaction().commit();
    s.close();

    // finally, clean up
    s = openSession();
    s.beginTransaction();
    s.createQuery("delete SimpleAssociatedEntity").executeUpdate();
    s.createQuery("delete SimpleEntityWithAssociation").executeUpdate();
    s.getTransaction().commit();
    s.close();
  }
コード例 #2
0
  @SuppressWarnings({"unchecked"})
  @Test
  public void testUpdateWithWhereExistsSubquery() {
    // multi-table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Human joe = new Human();
    joe.setName(new Name("Joe", 'Q', "Public"));
    s.save(joe);
    Human doll = new Human();
    doll.setName(new Name("Kyu", 'P', "Doll"));
    doll.setFriends(new ArrayList());
    doll.getFriends().add(joe);
    s.save(doll);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    String updateQryString =
        "update Human h "
            + "set h.description = 'updated' "
            + "where exists ("
            + "      select f.id "
            + "      from h.friends f "
            + "      where f.name.last = 'Public' "
            + ")";
    int count = s.createQuery(updateQryString).executeUpdate();
    assertEquals(1, count);
    s.delete(doll);
    s.delete(joe);
    t.commit();
    s.close();

    // single-table (one-to-many & many-to-many) ~~~~~~~~~~~~~~~~~~~~~~~~~~
    s = openSession();
    t = s.beginTransaction();
    SimpleEntityWithAssociation entity = new SimpleEntityWithAssociation();
    SimpleEntityWithAssociation other = new SimpleEntityWithAssociation();
    entity.setName("main");
    other.setName("many-to-many-association");
    entity.getManyToManyAssociatedEntities().add(other);
    entity.addAssociation("one-to-many-association");
    s.save(entity);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    // one-to-many test
    updateQryString =
        "update SimpleEntityWithAssociation e "
            + "set e.name = 'updated' "
            + "where exists ("
            + "      select a.id "
            + "      from e.associatedEntities a "
            + "      where a.name = 'one-to-many-association' "
            + ")";
    count = s.createQuery(updateQryString).executeUpdate();
    assertEquals(1, count);
    // many-to-many test
    if (getDialect().supportsSubqueryOnMutatingTable()) {
      updateQryString =
          "update SimpleEntityWithAssociation e "
              + "set e.name = 'updated' "
              + "where exists ("
              + "      select a.id "
              + "      from e.manyToManyAssociatedEntities a "
              + "      where a.name = 'many-to-many-association' "
              + ")";
      count = s.createQuery(updateQryString).executeUpdate();
      assertEquals(1, count);
    }
    s.delete(entity.getManyToManyAssociatedEntities().iterator().next());
    s.delete(entity);
    t.commit();
    s.close();
  }