Topics

JPA schema creation : Unique index on foreign-keys


passignat@...
 

Hi,

The schema tool is creating unique indexes on every foreign-key. I'm trying to reproduce the case on a shareable application, but I can't reproduce.
The index name pattern is <Target table>_U<number>

any suggestion ?
--
Stephane


passignat@...
 
Edited

Debugging the schema creation, the unique constraint is added by TableImpl:createCandidateKeys.
When a OneToOne doesn't have mapped-by parameter, it is considered as unique. The JPAAnnotationReader makes foreign-key unique by default in such situation (unidirectional OneToOne)
Why ? Semantically, the OneToOne relationship also identify both side of the relationship, so having a UNIQUE index is totally correct.

So to map a simple unidirectional association, the solution with JPA is ManyToOne without collection on the other side. May need some clarification on JPA spec (and a tck...).

Stephane