gpt4 book ai didi

使用自动创建表时,Hibernate 不会自动创建数据库序列

转载 作者:行者123 更新时间:2023-11-29 11:30:19 25 4
gpt4 key购买 nike

我想使用 Hibernate 和 Postgresql 自动创建我的数据库表,但我收到有关序列的错误。是否也可以使用 Hibernate 自动创建序列,或者我是否手动生成序列?

我的实体示例:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(uniqueConstraints = { @UniqueConstraint( columnNames = { "id" }) })
@SequenceGenerator(name="SEQ_EXAMPLE_ID", sequenceName="example_id_seq", allocationSize=1)
public class Example {

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_EXAMPLE_ID")
private Long id;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String g
}

hibernate 配置:

hbm2ddl.auto=create-drop
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true

异常(exception)情况:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not get next sequence value

org.postgresql.util.PSQLException: ERROR: relation "example_id_seq" does not exist

最佳答案

您的映射似乎是正确的,我建议激活以下类别的日志记录以查看到底发生了什么:

  • org.hibernate.tool.hbm2ddl: Log all SQL DDL statements as they are executed

将其设置为 DEBUG 并检查 DDL 语句(可能会用相关部分更新问题)。

PS: allocationSize=1 不是很明智,使用默认值会更好。但那是无关的。

引用资料

关于使用自动创建表时,Hibernate 不会自动创建数据库序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3591062/

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