use of jodd.db.oom.DbOomManager in project jodd by oblac.
the class GenericDao method deleteById.
/**
* Delete single object by its id. Resets ID value.
*/
public void deleteById(Object entity) {
if (entity != null) {
int result = query(DbEntitySql.deleteById(entity)).autoClose().executeUpdate();
if (result != 0) {
// now reset the ID value
DbOomManager dboom = DbOomManager.getInstance();
Class type = entity.getClass();
DbEntityDescriptor ded = dboom.lookupType(type);
setEntityId(ded, entity, 0);
}
}
}
use of jodd.db.oom.DbOomManager in project jodd by oblac.
the class GenericDao method store.
/**
* Saves or updates entity. If ID is not <code>null</code>, entity will be updated.
* Otherwise, entity will be inserted into the database.
*/
public <E> E store(E entity) {
DbOomManager dboom = DbOomManager.getInstance();
Class type = entity.getClass();
DbEntityDescriptor ded = dboom.lookupType(type);
if (ded == null) {
throw new DbOomException("Not an entity: " + type);
}
if (!isPersistent(ded, entity)) {
DbQuery q;
if (keysGeneratedByDatabase) {
q = query(insert(entity));
q.setGeneratedKey();
q.executeUpdate();
long nextId = q.getGeneratedKey();
setEntityId(ded, entity, nextId);
} else {
long nextId = generateNextId(ded);
setEntityId(ded, entity, nextId);
q = query(insert(entity));
q.executeUpdate();
}
q.close();
} else {
query(DbEntitySql.updateAll(entity)).autoClose().executeUpdate();
}
return entity;
}
use of jodd.db.oom.DbOomManager in project jodd by oblac.
the class DbEntitySql method findForeign.
/**
* Creates SELECT criteria for the entity matched by foreign key.
* Foreign key is created by concatenating foreign table name and column name.
*/
public static DbSqlBuilder findForeign(Class entity, Object value) {
String tableRef = createTableRefName(entity);
DbOomManager dbOomManager = DbOomManager.getInstance();
DbEntityDescriptor dedFk = dbOomManager.lookupType(value.getClass());
String tableName = dbOomManager.getTableNames().convertTableNameToEntityName(dedFk.getTableName());
String columnName = dbOomManager.getColumnNames().convertColumnNameToPropertyName(dedFk.getIdColumnName());
String fkColumn = uncapitalize(tableName) + capitalize(columnName);
Object idValue = BeanUtil.pojo.getProperty(value, dedFk.getIdPropertyName());
return sql().$(SELECT).column(tableRef).$(FROM).table(entity, tableRef).$(WHERE).ref(tableRef, fkColumn).$(EQUALS).columnValue(idValue);
}
use of jodd.db.oom.DbOomManager in project jodd by oblac.
the class GenericDaoTest method setUp.
@Before
public void setUp() throws Exception {
super.setUp();
DbOomManager.resetAll();
DbOomManager dbOom = DbOomManager.getInstance();
dbOom.registerEntity(Girl.class);
dbOom.registerEntity(Boy.class);
}
use of jodd.db.oom.DbOomManager in project jodd by oblac.
the class DbEntitySqlTest method setUp.
@Before
public void setUp() throws Exception {
DbOomManager.resetAll();
DbOomManager dbOom = DbOomManager.getInstance();
dbOom.registerType(Boy.class);
dbOom.registerType(BadBoy.class);
dbOom.registerType(BadGirl.class);
dbOom.registerType(Girl.class);
}
Aggregations