java - Hibernate 外键到串行列

标签 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的情况下修复它?

最佳答案

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

关于java - Hibernate 外键到串行列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36217309/

相关文章:

java - 可滚动分页客户端-服务器

java - 当我需要使用 Optional.orElseGet() 而不是 Optional.orElse()

java - 如何从java桌面应用程序中保存fos_user数据

java - 将模块包含到项目后得到 "unsupported class file version 52.0"

java - 双向一对多可连接

node.js - 在 Nodejs-Express API 中 Sequelize body-parser 错误

python - 我可以在 Flask-SQLAlchemy 中使用数组枚举列吗?

java - JPQL 连接 2 个表 : Writing jpql

java - Hibernate Criteria API 中 Restrictions.like 和 .ilike 的区别

python - Heroku/ flask /Postgres : Can not connect to server