timer.start();
DbManager manager = new DbManager(pool, schema);
// create persons
Person p = new Person();
p.setName("Klaus Mustermann");
manager.createObject(p);
UUID p1 = p.getId();
p.setId(null);
p.setName("Alex Admin");
manager.createObject(p);
UUID p2 = p.getId();
// create books
Book b = new Book();
b.setName("Yust Another Boring Book");
manager.createObject(b);
UUID b1 = b.getId();
b.setId(null);
b.setName("Mystic Almanach");
manager.createObject(b);
UUID b2 = b.getId();
// get a book and modify
b = (Book) manager.getObject(Book.class, b1);
b.setLendToId(p1);
manager.saveObject(b);
b.setLendToId(null);
manager.saveObject(b);
b.setLendToId(p2);
b.setAuthorId(new UUID[] {p1});
manager.saveObject(b);
Book copy = (Book) manager.getObject(Book.class, b.getId());
assertEquals(copy.getAuthorId()[0],p1) ;
// test relations
assertNotNull(b.getLendTo());
Person rel = b.getLendTo().getRelation();
assertNotNull(rel);
assertEquals(rel.getId(), p2);
assertNotNull(rel.getLendTo());
List<Book> retRel = rel.getLendTo().getRelations();
assertNotNull(retRel);
assertEquals(1,retRel.size());
assertEquals(b.getId(), retRel.get(0).getId());
retRel.add(manager.getObject(Book.class, b2));
manager.saveObject(rel);
rel.getLendTo().reset();
retRel = rel.getLendTo().getRelations();
assertNotNull(retRel);
assertEquals(2,retRel.size());
b = manager.getObject(Book.class, b1);
assertNotNull(b.getLendToId());
b.getLendTo().setRelation(null);
manager.saveObject(b);
assertNull(b.getLendToId());
b.getLendTo().setRelation(p);
manager.saveObject(b);
assertNotNull(b.getLendToId());
// remove book
manager.removeObject(b);
b = (Book) manager.getObject(Book.class, b1);
assertNull(b);
// test selection
System.out.println("----------------------");
DbCollection<Person> col = manager.executeQuery(new Person(), "select * from $db.Person$", null);
int count = 0;
for (Person pp : col) {
System.out.println("--- " + pp.getId() + " " + pp.getName());
count++;
}
assertEquals(count, 3);
System.out.println("----------------------");
col = manager.getByQualification(new Person(), null, null);
count = 0;
for (Person pp : col) {
System.out.println("--- " + pp.getId() + " " + pp.getName());
count++;
}
assertEquals(count, 3);
System.out.println("----------------------");
col = manager.getByQualification(new Person(), "$db.Person.Name$ like 'Klaus%'", null);
count = 0;
for (Person pp : col) {
System.out.println("--- " + pp.getId() + " " + pp.getName());
count++;
}
assertEquals(count, 1);
System.out.println("----------------------");
// test a native sql execute - remove all persons
DbConnection con = manager.getPool().getConnection();
con.createStatement("DELETE FROM $db.Person$", null ).execute(manager.getNameMapping());
con.commit();
System.out.println("----------------------");
col = manager.executeQuery(new Person(), "select * from $db.Person$", null);
count = 0;
for (Person pp : col) {
System.out.println("--- " + pp.getId() + " " + pp.getName());
count++;
}