These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How do I create an index on join tables using annotations?
PostPosted: Fri Dec 10, 2010 9:59 am 
Newbie

Joined: Fri Jul 30, 2004 5:35 am
Posts: 9
Location: Iceland
I posted the same question on stackoverflow if you want the points: http://stackoverflow.com/questions/4400169/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:

Code:
    public class Job{
   
    ...

    @OneToMany(cascade = CascadeType.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:

Code:
    @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:

Code:
    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?


Top
 Profile  
 
 Post subject: Re: How do I create an index on join tables using annotations?
PostPosted: Wed Dec 22, 2010 7:42 am 
Newbie

Joined: Fri Jul 30, 2004 5:35 am
Posts: 9
Location: Iceland
Is this question somehow unclear, or is there simply no way to do this?


Top
 Profile  
 
 Post subject: Re: How do I create an index on join tables using annotations?
PostPosted: Wed Dec 22, 2010 7:19 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
AFAIK all databases automatically generate indexes for foreign keys, you shouldn't worry about it unless you have specific reasons to think your database is not creating them.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: How do I create an index on join tables using annotations?
PostPosted: Mon Dec 27, 2010 9:26 am 
Newbie

Joined: Fri Jul 30, 2004 5:35 am
Posts: 9
Location: Iceland
This seems to be a common misconception among the Hibernate team viewtopic.php?t=924910

AFAIK only MySQL creates indices on foreign keys automatically but not PostgreSQL or Oracle. I've tested it on PostgreSQL and read it on other posts on this forum viewtopic.php?t=948998

It is perfectly reasonable that DB's keep foreign key constraints and indices separated since there are use cases where you would not want a index on your FK. I do believe however none of the FK's created by Hibernate are such so I would have expected the DDL generated by Hibernate to include them, but it doesn't, at least not for PostgreSQL.

Anyway, I did receive the correct answer to this on stackoverflow. This is not supported and there is an open issue about it http://opensource.atlassian.com/project ... e/HHH-4263


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.