mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#866 - Fix RawSql mapping such that it automatically maps foreign key columns
This commit is contained in:
@@ -363,8 +363,15 @@ class CQueryBuilder {
|
||||
RawSql.ColumnMapping.Column column = it.next();
|
||||
String propertyName = column.getPropertyName();
|
||||
if (!RawSqlBuilder.IGNORE_COLUMN.equals(propertyName)) {
|
||||
|
||||
ElPropertyValue el = descriptor.getElGetValue(propertyName);
|
||||
if (el == null && propertyName.endsWith("Id")) {
|
||||
// try default naming convention for foreign key columns
|
||||
String foreignIdPath = assocOneIdPath(propertyName);
|
||||
el = descriptor.getElGetValue(foreignIdPath);
|
||||
if (el != null) {
|
||||
propertyName = foreignIdPath;
|
||||
}
|
||||
}
|
||||
if (el == null) {
|
||||
throw new PersistenceException("Property [" + propertyName + "] not found on " + descriptor.getFullName());
|
||||
} else {
|
||||
@@ -404,6 +411,13 @@ class CQueryBuilder {
|
||||
return new SqlTreeBuilder(request, predicates, detail, rawNoId).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a path for a foreign key property using the default naming convention.
|
||||
*/
|
||||
private String assocOneIdPath(String propertyName) {
|
||||
return propertyName.substring(0, propertyName.length() - 2) + ".id";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the SQL response with row limiting (when not an update statement).
|
||||
*/
|
||||
|
||||
@@ -35,7 +35,13 @@ class CQueryPlanRawSql extends CQueryPlan {
|
||||
|
||||
// set the resultSet index positions for the property expressions
|
||||
for (int i = 0; i < chain.size(); i++) {
|
||||
indexPositions[i] = 1 + columnMapping.getIndexPosition(chain.get(i));
|
||||
String logicalPropertyPath = chain.get(i);
|
||||
int mappedPosition = columnMapping.getIndexPosition(logicalPropertyPath);
|
||||
if (mappedPosition == -1 && logicalPropertyPath.endsWith(".id")) {
|
||||
// try a automatically mapped foreign key
|
||||
mappedPosition = columnMapping.getIndexPosition(foreignKeyPath(logicalPropertyPath));
|
||||
}
|
||||
indexPositions[i] = 1 + mappedPosition;
|
||||
}
|
||||
|
||||
// check and handle the case where a discriminator column for
|
||||
@@ -51,4 +57,12 @@ class CQueryPlanRawSql extends CQueryPlan {
|
||||
|
||||
return indexPositions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the path for a foreign key column that was automatically mapped.
|
||||
*/
|
||||
private String foreignKeyPath(String logicalPropertyPath) {
|
||||
// trim the .id and replace with Id ... to reverse the auto fk mapping earlier
|
||||
return logicalPropertyPath.substring(0, logicalPropertyPath.length() - 3) + "Id";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user