Skip to content

Fix: @Index(columnNames) at field level is now correctly applied#149

Draft
cwegener-79 wants to merge 1 commit into
apache:masterfrom
cwegener-79:master
Draft

Fix: @Index(columnNames) at field level is now correctly applied#149
cwegener-79 wants to merge 1 commit into
apache:masterfrom
cwegener-79:master

Conversation

@cwegener-79
Copy link
Copy Markdown

Fix: @Index(columnNames) at field level is silently ignored

Problem

The OpenJPA-specific @Index annotation (org.apache.openjpa.persistence.jdbc.Index) supports a columnNames attribute that allows explicitly defining which columns should be included in a database index when the annotation is placed on a field or method. However, these column names were silently ignored during annotation parsing.

Root cause: AnnotationPersistenceMappingParser.parseIndex(MappingInfo, Index) called an internal overload passing only name, enabled, and uniqueidx.columnNames() was never forwarded:

// Before — columnNames dropped on the floor:
private void parseIndex(MappingInfo info, Index idx) {
    parseIndex(info, idx.name(), idx.enabled(), idx.unique());
}

As a result, any entity using @Index(columnNames = {"COL_A", "COL_B"}) at the field level would get a schema index with no explicitly defined columns.

Fix

  • parseIndex(MappingInfo, Index) now passes idx.columnNames() to a new overload.
  • New method parseIndex(MappingInfo, String, boolean, boolean, String[]) creates Column objects from the provided names and adds them to the Index schema object.
  • The existing protected 4-parameter overload delegates to the new method with null column names, preserving backwards compatibility for subclasses.

Testing

Added a regression test that verifies the fix end-to-end:

  • EntityWithIndexColumnNames — test entity with @Index(name="idx_col_a_b", columnNames={"COL_A","COL_B"}) on a field.
  • TestIndexColumnNames#testFieldIndexColumnNamesAreApplied — asserts that after full mapping resolution the schema index contains both explicitly named columns. The test fails without the fix (getColumns().length == 0) and passes with it.

Changed Files

File Change
AnnotationPersistenceMappingParser.java Bug fix + whitespace/indentation cleanup
EntityWithIndexColumnNames.java New test entity
TestIndexColumnNames.java New regression test

The columnNames attribute of the OpenJPA-specific @Index annotation
was not passed to the internal parseIndex overload and therefore
silently ignored.

A new overload parseIndex(..., String[] columnNames) adds the specified
columns to the schema Index object. The existing 4-parameter method
delegates to it in a backwards-compatible way.

Regression test: TestIndexColumnNames with EntityWithIndexColumnNames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant