Hibernate外键到串行列

时间:2016-03-25 09:29:41

标签: java hibernate postgresql jpa

Hibernate生成奇怪的DDL,用于配置串行列和FK。示例(Book.author * -1 Authors.id):

@Entity
@Table(name = "books")
public class Book {
    private Integer id;
    private Author author;

    @Id
    @Column(name = "id", columnDefinition = "serial")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @JoinColumn(name = "author", nullable = true)
    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }
}

@Entity
@Table(name = "authors")
public class Author {
    private Integer id;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

书籍表格中列专家的结果DDL:

ALTER TABLE books ADD COLUMN author integer;
ALTER TABLE books ALTER COLUMN author SET NOT NULL;
ALTER TABLE books ALTER COLUMN author SET DEFAULT nextval('authors_seq'::regclass);

它有奇怪的默认值,我也不能让它可以为空。是否可以在不为FK编写columnDefinition的情况下修复它?

1 个答案:

答案 0 :(得分:1)

我已将其报告为错误https://hibernate.atlassian.net/browse/HHH-10647。目前的解决方法是使用columnDefinition for FK column:columnDefinition = "integer"