The back reference generation is now configurable.

The default version is the behavior that existed so far:
The back reference is the table name as generated by the `NamingStrategy` without taking `@Table` annotations into account.

The new alternative is to take `@Table` into account.

The behavior can be configured by setting the `foreignKeyNaming` property on the `RelationalMappingContext`.

Closes #1161
Closes #1147
Original pull request: #1324.
This commit is contained in:
Jens Schauder
2022-09-01 15:02:55 +02:00
committed by Mark Paluch
parent 15796b88fe
commit 40446f9ca9
18 changed files with 313 additions and 39 deletions

View File

@@ -298,10 +298,10 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
Assert.notNull(propertyPath, "propertyPath must not be null");
PersistentPropertyPathExtension path = new PersistentPropertyPathExtension(context, propertyPath);
Class<?> actualType = path.getActualType();
String findAllByProperty = sql(actualType) //
.getFindAllByProperty(identifier, path.getQualifierColumn(), path.isOrdered());
.getFindAllByProperty(identifier, propertyPath);
RowMapper<?> rowMapper = path.isMap() ? this.getMapEntityRowMapper(path, identifier)
: this.getEntityRowMapper(path, identifier);

View File

@@ -37,7 +37,7 @@ class SqlContext {
SqlContext(RelationalPersistentEntity<?> entity) {
this.entity = entity;
this.table = Table.create(entity.getTableName());
this.table = Table.create(entity.getFullTableName());
}
Column getIdColumn() {

View File

@@ -200,6 +200,26 @@ class SqlGenerator {
return render(selectBuilder(Collections.emptyList(), pageable.getSort(), pageable).build());
}
/**
* Returns a query for selecting all simple properties of an entity, including those for one-to-one relationships.
* Results are limited to those rows referencing some parent entity. This is used to select values for a complex
* property ({@link Set}, {@link Map} ...) based on a referencing entity.
*
* @param parentIdentifier name of the column of the FK back to the referencing entity.
* @param propertyPath used to determine if the property is ordered and if there is a key column.
* @return a SQL String.
*/
String getFindAllByProperty(Identifier parentIdentifier,
PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath) {
Assert.notNull(parentIdentifier, "identifier must not be null");
Assert.notNull(propertyPath, "propertyPath must not be null");
PersistentPropertyPathExtension path = new PersistentPropertyPathExtension(mappingContext, propertyPath);
return getFindAllByProperty(parentIdentifier, path.getQualifierColumn(), path.isOrdered());
}
/**
* Returns a query for selecting all simple properties of an entity, including those for one-to-one relationships.
* Results are limited to those rows referencing some other entity using the column specified by
@@ -915,7 +935,7 @@ class SqlGenerator {
private SelectBuilder.SelectOrdered applyQueryOnSelect(Query query, MapSqlParameterSource parameterSource,
SelectBuilder.SelectWhere selectBuilder) {
Table table = Table.create(this.entity.getTableName());
Table table = Table.create(this.entity.getFullTableName());
SelectBuilder.SelectOrdered selectOrdered = query //
.getCriteria() //

View File

@@ -38,7 +38,7 @@ class SqlContext {
SqlContext(RelationalPersistentEntity<?> entity) {
this.entity = entity;
this.table = Table.create(entity.getTableName());
this.table = Table.create(entity.getFullTableName());
}
Column getIdColumn() {