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);
}
}
}
}
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);
}
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();
}
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();
}
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);
}
Aggregations