/** * Finds list of entities matching given criteria. */ @SuppressWarnings({"unchecked"}) public <E> List<E> find(final Object criteria) { return query(dbOom.entities().find(criteria)).autoClose().list(criteria.getClass()); }
/** * Finds one entity for given criteria. */ @SuppressWarnings({"unchecked"}) public <E> E findOne(final Object criteria) { return (E) query(dbOom.entities().find(criteria)).autoClose().find(criteria.getClass()); }
/** * Counts number of all entities. */ public long count(final Class entityType) { return query(dbOom.entities().count(entityType)).autoClose().executeCount(); }
/** * Finds related entity. */ public <E> List<E> findRelated(final Class<E> target, final Object source) { return query(dbOom.entities().findForeign(target, source)).autoClose().list(target); }
/** * List all entities. */ public <E> List<E> listAll(final Class<E> target) { return query(dbOom.entities().from(target)).autoClose().list(target); }
/** * Finds single entity by matching property. */ public <E> E findOneByProperty(final Class<E> entityType, final String name, final Object value) { return query(dbOom.entities().findByColumn(entityType, name, value)).autoClose().find(entityType); }
/** * Decreases a property. */ public <ID> void decreaseProperty(final Class entityType, final ID id, final String name, final Number delta) { query(dbOom.entities().increaseColumn(entityType, id, name, delta, false)).autoClose().executeUpdate(); }
/** * Finds list of entities matching given criteria. */ public <E> List<E> find(final Class<E> entityType, final Object criteria) { return query(dbOom.entities().find(entityType, criteria)).autoClose().list(entityType); }
/** * Deleted single entity by its id. */ public <ID> void deleteById(final Class entityType, final ID id) { query(dbOom.entities().deleteById(entityType, id)).autoClose().executeUpdate(); }
/** * Simply inserts object into the database. */ public void save(final Object entity) { final DbQuery q = query(dbOom.entities().insert(entity)); q.autoClose().executeUpdate(); }
/** * Updates single entity. */ public void update(final Object entity) { query(dbOom.entities().updateAll(entity)).autoClose().executeUpdate(); }
/** * Finds single entity by its id. */ public <E, ID> E findById(final Class<E> entityType, final ID id) { return query(dbOom.entities().findById(entityType, id)).autoClose().find(entityType); }
/** * Increases a property. */ public <ID> void increaseProperty(final Class entityType, final ID id, final String name, final Number delta) { query(dbOom.entities().increaseColumn(entityType, id, name, delta, true)).autoClose().executeUpdate(); }
/** * Updates single property in database and in the bean. */ public <E> E updateProperty(final E entity, final String name, final Object newValue) { query(dbOom.entities().updateColumn(entity, name, newValue)).autoClose().executeUpdate(); BeanUtil.declared.setProperty(entity, name, newValue); return entity; }
/** * Updates property in the database by storing the current property value. */ public <E> E updateProperty(final E entity, final String name) { Object value = BeanUtil.declared.getProperty(entity, name); query(dbOom.entities().updateColumn(entity, name, value)).autoClose().executeUpdate(); return entity; }
@Test void testFrom() { Girl g = new Girl(1, "sanja", "c++"); assertEquals("select Girl_.ID, Girl_.NAME, Girl_.SPECIALITY from GIRL Girl_ ", dbOom.entities().from(g).generateQuery()); assertEquals("select BadGirl_.ID, BadGirl_.NAME, BadGirl_.SPECIALITY from GIRL BadGirl_ ", dbOom.entities().from(BadGirl.class).generateQuery()); assertEquals("select ggg.ID, ggg.NAME, ggg.SPECIALITY from GIRL ggg ", dbOom.entities().from(BadGirl.class, "ggg").generateQuery()); }
@Test void testUpdateColumn() { BadGirl bg = new BadGirl(Integer.valueOf(1), "sanja", "c++"); DbSqlBuilder b = dbOom.entities().updateColumn(bg, "fooname", "Anja"); assertEquals( "update GIRL BadGirl_ set NAME=:p0 where (BadGirl_.ID=:badGirl.fooid)", b.generateQuery()); Map<String, ParameterValue> params = b.getQueryParameters(); assertEquals(2, params.entrySet().size()); assertEquals(Integer.valueOf(1), params.get("badGirl.fooid").getValue()); assertEquals("Anja", params.get("p0").getValue()); }
@Test void testIncreaseDecrease() { DbSqlBuilder b = dbOom.entities().increaseColumn(BadBoy.class, 1, "nejm", 5, true); assertEquals("update BOY set NAME=NAME+:p0 where BOY.ID=:p1", b.generateQuery()); b = dbOom.entities().increaseColumn(BadBoy.class, 1, "nejm", 5, false); assertEquals("update BOY set NAME=NAME-:p0 where BOY.ID=:p1", b.generateQuery()); }
@Test void testInsert() { Girl g = new Girl(1, "sanja", "c++"); DbSqlBuilder b = dbOom.entities().insert(g); assertEquals("insert into GIRL (ID, NAME, SPECIALITY) values (:girl.id, :girl.name, :girl.speciality)", b.generateQuery()); checkGirl(b); }