We're updating the issue view to help you get more done. 

DDL generation fails for an indexed column when a corresponding attribute doesn't define physical mapping using @Column

Description

Failing entity definition:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Index; import javax.persistence.Table; @Entity @Table(indexes = @Index(columnList = "user_name")) public class Sample { @Id private long id; private String userName; }

Stacktrace:

1 2 3 4 5 6 7 Caused by: org.hibernate.AnnotationException: Unable to create index (user_name) on table sample: database column 'user_name' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.buildUniqueKeyFromColumnNames(InFlightMetadataCollectorImpl.java:2137) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processJPAIndexHolders(InFlightMetadataCollectorImpl.java:2149) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1671) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:287) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final] at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]

Workaround #1 (with a superfluous @Column definition):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Index; import javax.persistence.Table; @Entity @Table(indexes = @Index(columnList = "user_name")) public class Sample { @Id private long id; @Column(name = "user_name") private String userName; }

Workaround #2 (with logical column name usage):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Index; import javax.persistence.Table; @Entity @Table(indexes = @Index(columnList = "userName")) public class Sample { @Id private long id; private String userName; }

My assumption is that columnNames attribute of @Index annotation are physical values, whereas code in the method
org.hibernate.boot.internal.InFlightMetadataCollectorImpl#buildUniqueKeyFromColumnNames starting on the line 2013 works with it as if it were logical values.

Environment

None

Status

Assignee

Unassigned

Reporter

Roman Golovan

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.3.10

Priority

Minor