Пример #1
0
 @Test
 public void testInsertManyToMany() {
   Person person = randomPerson();
   data.insert(person);
   assertTrue(person.getGroups().toList().isEmpty());
   List<Group> added = new ArrayList<>();
   try (Transaction transaction = data.transaction().begin()) {
     for (int i = 0; i < 10; i++) {
       Group group = new Group();
       group.setName("Group" + i);
       group.setDescription("Some description");
       group.setType(GroupType.PRIVATE);
       data.insert(group);
       person.getGroups().add(group);
       added.add(group);
     }
     data.update(person);
     transaction.commit();
   }
   data.refresh(person, Person.GROUPS);
   assertTrue(added.containsAll(person.getGroups().toList()));
 }
Пример #2
0
  @Test
  public void testTransactionRollback() {
    ArrayList<Integer> ids = new ArrayList<>();
    try (Transaction transaction = data.transaction().begin()) {
      for (int i = 0; i < 10; i++) {
        Person person = randomPerson();
        data.insert(person);
        assertTrue(person.getId() > 0);
        ids.add(person.getId());
        if (i == 5) {
          throw new Exception("rollback...");
        }
      }
      transaction.commit();
    } catch (Exception ignored) {

    }
    for (Integer id : ids) {
      Person p = data.select(Person.class).where(Person.ID.equal(id)).get().firstOrNull();
      assertNull(p);
    }
  }