0
@OneToOne
private AnyEntity entity;

hibernate will create a mapping as follows:

CONSTRAINT fk_kcn86scsc0pasdasdngmrqc5i0 FOREIGN KEY (text_id)
 REFERENCES some_table (id) MATCH SIMPLE
 ON UPDATE NO ACTION ON DELETE NO ACTION

Question: how can I set the constraint name explicit from within java?

I cannot introduce a bidirectional mapping, as the AnyEntity class will be used in multiple other @Entity classes.

prab2112
  • 1,024
  • 10
  • 36
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • possible duplicate of [Changing the generated name of a foreign key in Hibernate](http://stackoverflow.com/questions/16564789/changing-the-generated-name-of-a-foreign-key-in-hibernate) – Jokab Aug 04 '15 at 08:54

1 Answers1

3

You can use @ForeignKey annotation.

@ForeignKey(name="constraint_name")
prab2112
  • 1,024
  • 10
  • 36
  • No, this will set the name of `FOREIGN KEY (contraint_name)`, but I want to change the `fk_...` name. – membersound Aug 04 '15 at 08:16
  • @membersound Have you tried it? It seems to be the right solution: http://stackoverflow.com/a/16564911/1225328. – sp00m Aug 04 '15 at 08:29
  • 1
    I found out that `@ForeignKey` annotation is disallowed on fields. But it can be used as follows: `@JoinColumn(name = "BAR_ID", foreignKey = @ForeignKey(name = FK_BAR_OF_FOO))`, which worked as expected. – membersound Aug 04 '15 at 08:57