Hibernate allows adding indexes on @ManyToOne
mappings by the use of @org.hibernate.annotations.Index
.
Is there a way to specify index for the join table in a @ManyToMany
relation?
If Entity A
and Entity B
have a @ManyToMany
with A
being the owning side, the join table will have a composite index (a1, b1).
I wonder if this is enough, or do I need to create another index (b1, a1)?
Advertisement
Answer
I too am searching for this – the holy grail, it seems.
In this post:
How can I ask Hibernate to create an index on a foreign key (JoinColumn)? an answerer seems to believe that adding @Index to the collection creates an index on the join table. It doesn’t.
This post:
How do I create an index on join tables using Hibernate annotations? reveals what I am slowly coming to believe is the horrible truth. It’s not possible, unless you revert to using an hbm.xml file to define your entities (ie: doesn’t appear to be possible using annotations).
So whilst it does speed up queries tremendously to have a composite index on join tables, encompassing both FK columns, but there is currently no way to create said index automatically using annotations.
I am now looking into creating a task to be used after Hibernate’s ExportSchema execution to identify join tables and create indexes. It’s not going very well! Please do share any viable solutions you happen to come across. I’m hoping someone will share their approach for comparison.