Skip to content

Provide a way to alter composite key backreference naming #2288

Open

Description

@treetip

Mapping an existing schema like

create table product(
  tenant bigint,
  code text,
  ...
  primary key (tenant, code)
);

create table product_i18n(
  tenant bigint,
  product_code text,
  language text,
  name text,
  description text,
  primary key (tenant, product_code, language),
  foreign key (tenant, product_code) references product(tenant, code)
);
record ProductPK(Long tenant, String code) {}

@Table("product")
record Product(
  @Id ProductPK pk,
  @MappedCollection(keyColumn = "language") Map<String, ProductI18n> localization,
  ...
) {}

@Table("product_i18n")
record ProductI18n(String name, String description) {}

Loading any data expects that the product_i18n.tenant column would be named product_tenant.

Documentation says



All references in an aggregate result in a foreign key relationship in the opposite direction in the database. By default, the name of the foreign key column is the table name of the referencing entity.

If the referenced id is an @Embedded id, the back reference consists of multiple columns, each named by a concatenation of <table-name> + _ + <column-name>. E.g. the back reference to a Person entity, with a composite id with the properties firstName and lastName will consist of the two columns PERSON_FIRST_NAME and PERSON_LAST_NAME.

Alternatively you may choose to have them named by the entity name of the referencing entity ignoring @Table annotations. You activate this behaviour by calling setForeignKeyNaming(ForeignKeyNaming.IGNORE_RENAMING) on the RelationalMappingContext.

I guess the 3rd paragraph is referring to the first and not the second as AggregatePath.java#410 is:

if (idProperty != null && idProperty.isEntity()) {
    RelationalPersistentEntity<?> idEntity = basePath.getRequiredLeafEntity();
    idEntity.doWithProperties((PropertyHandler<RelationalPersistentProperty>) p -> {
        AggregatePath idElementPath = basePath.append(p);
	SqlIdentifier name = idElementPath.getColumnInfo().name();
	name = name.transform(n -> idDefiningParentPath.getTableInfo().qualifiedTableName.getReference() + "_" + n);
	ciBuilder.add(idElementPath, name, name);
    });
} else {

so please provide a way to give the name the columns for composite pks back reference.

Have not tested saving, assuming something similar is an issue there.

Activity

  1. Andreas-wl commented on Apr 29, 2026

    @Andreas-wl

    We ran into the same issue while preparing our multi-tenant application for table partitioning. All our tables have composite PKs (tenant_id, id) and the child tables use (tenant_id, <parent>_id) as foreign keys. The auto-generated back-reference names (<table>_<column>) don't match our existing schema.

    Local fix

    We prototyped a fix in AggregatePath.computeBackReferenceColumnInfos that adds explicit column mapping via a new annotation:

    @Retention(RetentionPolicy.RUNTIME)                                                                                                                                                                                                                                                                                   
    @Target({})
    public @interface IdColumnMapping {
        String idProperty();
        String column();
    }                                                                                                                                                                                                                                                                                                                       
     
    Added as a new attribute on @MappedCollection:                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                          
    IdColumnMapping[] compositeIdColumns() default {};                                                                                                                                                                                                                                                                      
     
    Usage                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                          
    @MappedCollection(
        compositeIdColumns = {                                                                                                                                                                                                                                                                                              
            @IdColumnMapping(idProperty = "tenantId", column = "tenant_id"),
            @IdColumnMapping(idProperty = "id", column = "schedule_id")                                                                                                                                                                                                                                                     
        },                                                                                                                                                                                                                                                                                                                
        keyColumn = "id"
    )

    How it works

    In computeBackReferenceColumnInfos, when iterating over composite ID properties, it checks if an @IdColumnMapping exists for the current property name. If found, it uses the explicit column name. Otherwise, it falls back to the existing <table>_<column> convention.

    • ~15 lines changed in AggregatePath
    • Fully backwards compatible (empty array = existing behavior)
    • No changes needed to existing code or schemas

    Hopefully this may work as inspiration

  2. christophstrobl commented on May 11, 2026

    @christophstrobl
    Member

    Thank you for bringing this up and providing food for thought on this matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions