How do I create an index on join tables using Hibernate annotations?
I have a relationship as follows using Hibernate annotations, this is what I tried:
public class Job{
...
@OneToMany(cascade = Cas开发者_运维百科cadeType.ALL)
@JoinTable(name = "jobs_resource_locations")
@ForeignKey(name = "job_inputs_fk")
@Index(name="job_inputs_fk")
private List<FileSystemLocation> inputs;
This sort of thing works nicely on ManyToOne like so:
@ManyToOne
@JoinColumn(name = "service_call_id", referencedColumnName = "id")
@ForeignKey(name = "job_service_call_fk")
@Index(name = "job_service_call_fk")
private ServiceCall serviceCall;
I wanted to ensure that the foreign key gets indexed on PostgreSQL and that the schema looks similar on MySQL, hence the @ForeignKey and @Index with the same name (MySQL always creates an index with the same name as the FK).
I cannot create the index on the inverse side because FileSystemLocation is unaware of the relationship. Hence the JoinTable.
The former example fails since Hibernate finds no column in Job to index:
org.hibernate.MappingException: Unable to find logical column name from physical name null in table jobs
Does anyone know how to create indices on JoinTable foreign keys using Hibernate?
It's not exactly the answer you would like to receive, but this is the expected behavior. In other words: this is not supported. See the following JIRA for more details:
https://hibernate.atlassian.net/browse/HHH-4263
精彩评论