Hibernate: Creating an index for an ElementCollection table

3.3k views Asked by At
@Entity
public class FruitStore {

@Id
private Long storeId;

@ElementCollection
private Set<Fruit> fruits;

}

Of course, the Fruit class is marked @Embeddable.

In the database (postgresql to be exact, although it shouldn't matter), there is a table created called fruitstore_fruits. It grows huge, and queries on it become very very slow. I have manually modified the database, such that the fruitstore_fruits table indexes on the FruitStore id column. Happily, this dramatically improves performance. I want this to be done automatically.

The question is, how can I annotate my code to get Hibernate to automatically index the fruitstore_fruits on the FruitStore id column?

EDIT: This Hibernate bug has removed lots of hope. I think what I want is simply not supported right now. Which is kinda sad, because the feature isn't that exotic (indexing a element collection with a foreign column). However, I'd love to be proven wrong here.

1

There are 1 answers

0
Сергей Грачев On

Take a look for example of how it could be done:

@CollectionTable(indexes = {@Index(columnList = "solution_version_training_session_id")})