How can I set the index name for a primary key using @Index JPA annotation?

My tools -> Java 8, JPA 2.1 and Hibernate 4. I only use JPA2.1 annotations.

Code in dock ->

@Entity
@Table(indexes = { @Index(name = INDEX_PK, columnList = ID) })
public class Invoice {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO, generator = DEF_GEN_NAME)
 @SequenceGenerator(sequenceName = DEF_SEQUENCE_NAME, name = DEF_GEN_NAME, allocationSize =   
 ALLOCATION_SIZE)
 @Column(name = ID)
 private Long id = 0L;

}

      

When the schema is created by Hibernate (hbm2ddl = "create-drop") I get the following Oracle error:

Hibernate: create index INVOICE_ID_PK on Invoice (ID)
sep 14, 2014 7:00:53 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: create index INVOICE_ID_PK on Invoice (ID)
sep 14, 2014 7:00:53 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: ORA-01408: such column list already indexed

      


While looking for this issue y found that the default primary key index is generated by Oracle itself, Hibernate is unaware of this behavior, the fact is that hibernate first creates the Invoice table, so Oracle automatically creates a sysXXX index (example: SYS_C0011010) for the id on table of accounts. After all tables are created, Hibernate starts creating indexes and then Oracle throws an error for
duplicate index on the same column ID

Is there a way to change this behavior to create the index and table in the same SQL statement using Hibernate? .. any walkaround?

Thank!

+3


source to share





All Articles