views:

56

answers:

1

When I use the maven-hibernate3-plugin (aka hbm2ddl) to generate my database schema, it creates many database constraints with terrifically hard-to-remember constraint names like FK7770538AEE7BC70 .

Is there any way to provide a more useful name such as FOO_FK_BAR_ID ?

If so, it would make it a tad easier to track down issues in the log files and other places where the violation doesn't tell you anything other than the constraint name.

+3  A: 

Hibernate has a @ForeignKey annotation allowing to override the constraint name. From the reference documentation:

2.4.6. Collection related annotations

(...)

Foreign key constraints, while generated by Hibernate, have a fairly unreadable name. You can override the constraint name by use @ForeignKey. Note that this annotation has to be placed on the owning side of the relationship, inverseName referencing to the other side constraint.

@Entity
public class Woman {
    ...
    @ManyToMany(cascade = {CascadeType.ALL})
    @ForeignKey(name = "TO_WOMAN_FK", inverseName = "TO_MAN_FK")
    public Set<Man> getMens() {
        return mens;
    }
}

alter table Man_Woman add constraint TO_WOMAN_FK foreign key (woman_id) references Woman
alter table Man_Woman add constraint TO_MAN_FK foreign key (man_id) references Man

But I'm not aware of a standard JPA equivalent.

Pascal Thivent
Arrgggghhhhh. Thanks.
HDave
In JPA: @JoinTable(joinColumns=@JoinColumn(name="TO_WOMAN_FK"), inverseJoinColumns=@JoinColumn(name="TO_MAN_FK"))
arjan
@arjantop: No, this doesn't allow to specify the name of the foreign key constraint. You missed the point.
Pascal Thivent
my bad, i read the question too quickly
arjan