gpt4 book ai didi

java - Hibernate 外键到串行列

转载 作者:行者123 更新时间:2023-12-01 10:09:36 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com