如何更改hibernate约束名称生成?

时间:2015-04-16 15:39:43

标签: java hibernate

我使用hibernate生成ddl架构,并获取:

CONSTRAINT fk_o52asd0k9712345qhq4k5f6g FOREIGN KEY (foo_id) REFERENCES foo (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION

我如何替换禁令生成的名称?

@Entity
public class Foo {
    @Id
    private Long id;
}

@Entity
public class Bar {
    @OneToOne
    @JoinColumn
    @ForeignKey(name = "fk_foo") //this is what I tried, but did not work
    private Foo foo;
}

结果:The annotation @ForeignKey is disallowed for this location

我可以更改什么来定义自己的名字?

1 个答案:

答案 0 :(得分:1)

您可以使用org.hibernate.annotations.ForeignKey。但是这个注释在最新的hibernate中已被弃用。更好地使用JPA的ForeignKey注释版本,您必须将其用作JoinColumn的属性。

以下是重写代码:

@Entity
public class Bar {
    @OneToOne
    @JoinColumn( foreignKey = @ForeignKey(name = "fk_foo")))
    private Foo foo;
}