Skip to content

[OPENJPA-3002] Fix: @Index(columnNames) at field level is now correctly applied - #149

Merged
solomax merged 3 commits into
apache:masterfrom
cwegener-79:master
Aug 21, 2026
Merged

[OPENJPA-3002] Fix: @Index(columnNames) at field level is now correctly applied#149
solomax merged 3 commits into
apache:masterfrom
cwegener-79:master

Conversation

@cwegener-79

Copy link
Copy Markdown
Contributor

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

@cwegener-79
cwegener-79 marked this pull request as ready for review May 26, 2026 08:35
@cwegener-79

Copy link
Copy Markdown
Contributor Author

Just a quick nudge on this PR 🙂
I know @Index is not part of standard JPA, but I could make good use of this fix in a legacy project.
Please let me know if there’s anything I can do to help move this forward.

@solomax solomax left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove all whitespaces-only changes to AnnotationPersistenceMappingParser

Thanks in advance :)

@cwegener-79
cwegener-79 force-pushed the master branch 2 times, most recently from 6851bcf to aa5e4b3 Compare June 3, 2026 07:25
@solomax solomax changed the title Fix: @Index(columnNames) at field level is now correctly applied [OPENJPA-3002] Fix: @Index(columnNames) at field level is now correctly applied Aug 20, 2026
@solomax solomax self-assigned this Aug 20, 2026
@solomax

solomax commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

@cwegener-79 could you please rebase this one? we just have merged huge new functionality (this is why merging was delayed)

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
- Replace fully qualified org.apache.openjpa.jdbc.schema.Column with
  the already-imported short name Column
- Add missing braces around the for-loop body
@cwegener-79

Copy link
Copy Markdown
Contributor Author

@cwegener-79 could you please rebase this one? we just have merged huge new functionality (this is why merging was delayed)

No Problem, I've just rebased it.

@solomax
solomax merged commit 2d865d8 into apache:master Aug 21, 2026
4 checks passed
@solomax

solomax commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Thanks for the contribution!

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.

2 participants