Search in sources :

Example 96 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class OraclePkGenerator method longPkFromDatabase.

/**
 * Generates primary key by calling Oracle sequence corresponding to the
 * <code>dbEntity</code>. Executed SQL looks like this:
 *
 * <pre>
 *   SELECT pk_table_name.nextval FROM DUAL
 * </pre>
 *
 * @since 3.0
 */
@Override
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
    DbKeyGenerator pkGenerator = entity.getPrimaryKeyGenerator();
    String pkGeneratingSequenceName;
    if (pkGenerator != null && DbKeyGenerator.ORACLE_TYPE.equals(pkGenerator.getGeneratorType()) && pkGenerator.getGeneratorName() != null) {
        pkGeneratingSequenceName = pkGenerator.getGeneratorName();
    } else {
        pkGeneratingSequenceName = sequenceName(entity);
    }
    try (Connection con = node.getDataSource().getConnection()) {
        try (Statement st = con.createStatement()) {
            String sql = selectNextValQuery(pkGeneratingSequenceName);
            adapter.getJdbcEventLogger().log(sql);
            try (ResultSet rs = st.executeQuery(sql)) {
                if (!rs.next()) {
                    throw new CayenneRuntimeException("Error generating pk for DbEntity %s", entity.getName());
                }
                return rs.getLong(1);
            }
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbKeyGenerator(org.apache.cayenne.map.DbKeyGenerator)

Example 97 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class ObjEntity method translateToRelatedEntity.

/**
 * Transforms an Expression rooted in this entity to an analogous expression
 * rooted in related entity.
 *
 * @since 1.1
 */
@Override
public Expression translateToRelatedEntity(Expression expression, String relationshipPath) {
    if (expression == null) {
        return null;
    }
    if (relationshipPath == null) {
        return expression;
    }
    if (getDbEntity() == null) {
        throw new CayenneRuntimeException("Can't transform expression, no DbEntity for '%s'.", getName());
    }
    // converts all OBJ_PATH expressions to DB_PATH expressions
    // and pass control to the DB entity
    DBPathConverter transformer = new DBPathConverter();
    String dbPath = transformer.toDbPath(createPathIterator(relationshipPath));
    Expression dbClone = expression.transform(transformer);
    return getDbEntity().translateToRelatedEntity(dbClone, dbPath);
}
Also used : Expression(org.apache.cayenne.exp.Expression) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 98 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class ObjRelationship method getValidRelationshipPath.

/**
 * Returns dot-separated path over DbRelationships, only including
 * components that have valid DbRelationships.
 */
String getValidRelationshipPath() {
    String path = getDbRelationshipPath();
    if (path == null) {
        return null;
    }
    ObjEntity entity = (ObjEntity) getSourceEntity();
    if (entity == null) {
        throw new CayenneRuntimeException("Can't resolve DbRelationships, null source ObjEntity");
    }
    DbEntity dbEntity = entity.getDbEntity();
    if (dbEntity == null) {
        return null;
    }
    StringBuilder validPath = new StringBuilder();
    try {
        for (PathComponent<DbAttribute, DbRelationship> pathComponent : dbEntity.resolvePath(new ASTDbPath(path), Collections.emptyMap())) {
            if (validPath.length() > 0) {
                validPath.append(Entity.PATH_SEPARATOR);
            }
            validPath.append(pathComponent.getName());
        }
    } catch (ExpressionException ex) {
    }
    return validPath.toString();
}
Also used : ToStringBuilder(org.apache.cayenne.util.ToStringBuilder) ASTDbPath(org.apache.cayenne.exp.parser.ASTDbPath) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 99 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class ObjRelationship method refreshFromPath.

/**
 * Rebuild a list of relationships if String relationshipPath has changed.
 */
final void refreshFromPath(String dbRelationshipPath, boolean stripInvalid) {
    // remove existing relationships
    dbRelationships.clear();
    if (dbRelationshipPath != null) {
        ObjEntity entity = (ObjEntity) getSourceEntity();
        if (entity == null) {
            throw new CayenneRuntimeException("Can't resolve DbRelationships, null source ObjEntity");
        }
        try {
            // add new relationships from path
            Iterator<CayenneMapEntry> it = entity.resolvePathComponents(new ASTDbPath(dbRelationshipPath));
            while (it.hasNext()) {
                DbRelationship relationship = (DbRelationship) it.next();
                dbRelationships.add(relationship);
            }
        } catch (ExpressionException ex) {
            if (!stripInvalid) {
                throw ex;
            }
        }
    }
    recalculateToManyValue();
    recalculateReadOnlyValue();
}
Also used : CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) ASTDbPath(org.apache.cayenne.exp.parser.ASTDbPath) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ExpressionException(org.apache.cayenne.exp.ExpressionException)

Example 100 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class AbstractQuery method route.

/**
 * Implements default routing mechanism relying on the EntityResolver to find DataMap
 * based on the query root. This mechanism should be sufficient for most queries that
 * "know" their root.
 *
 * @since 1.2
 */
public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) {
    DataMap map = getMetaData(resolver).getDataMap();
    if (map == null) {
        throw new CayenneRuntimeException("No DataMap found, can't route query %s", this);
    }
    router.route(router.engineForDataMap(map), this, substitutedQuery);
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9