Search in sources :

Example 1 with DbOomManager

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);
        }
    }
}
Also used : DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 2 with DbOomManager

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;
}
Also used : DbOomException(jodd.db.oom.DbOomException) DbQuery(jodd.db.DbQuery) DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 3 with DbOomManager

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);
}
Also used : DbOomManager(jodd.db.oom.DbOomManager) DbEntityDescriptor(jodd.db.oom.DbEntityDescriptor)

Example 4 with DbOomManager

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);
}
Also used : DbOomManager(jodd.db.oom.DbOomManager) Before(org.junit.Before)

Example 5 with DbOomManager

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);
}
Also used : DbOomManager(jodd.db.oom.DbOomManager) Before(org.junit.Before)

Aggregations

DbOomManager (jodd.db.oom.DbOomManager)7 DbEntityDescriptor (jodd.db.oom.DbEntityDescriptor)4 Before (org.junit.Before)2 DbManager (jodd.db.DbManager)1 DbQuery (jodd.db.DbQuery)1 DbSessionProvider (jodd.db.DbSessionProvider)1 DbJtxSessionProvider (jodd.db.jtx.DbJtxSessionProvider)1 DbOomException (jodd.db.oom.DbOomException)1 DbOomQuery (jodd.db.oom.DbOomQuery)1 AnnotationTxAdviceManager (jodd.jtx.proxy.AnnotationTxAdviceManager)1 MutableLong (jodd.mutable.MutableLong)1