简体   繁体   English

休眠列的外键

[英]Hibernate foreign key to serial column

Hibernate generates strange DDL for configuration with serial column and FK to it. Hibernate会生成奇怪的DDL,以进行串行列和FK的配置。 Example (Book.author *-1 Authors.id): 示例(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;
    }
}

Result DDL for column author in books table: 图书表中列作者的结果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);

It has strange default value and also i can't make it nullable. 它具有奇怪的默认值,我也不能使其为可空。 Is it possible to fix it without writing columnDefinition for FK? 是否可以在不编写FK的columnDefinition的情况下进行修复?

I have reported it as a bug https://hibernate.atlassian.net/browse/HHH-10647 . 我已经将其报告为错误https://hibernate.atlassian.net/browse/HHH-10647 Current workaround is using columnDefinition for FK column: columnDefinition = "integer" 当前的解决方法是对FK列使用columnDefinition: columnDefinition = "integer"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM