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

发布于 2024-09-16 01:49:01 字数 1303 浏览 4 评论 0原文

我想使用 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 

异常:

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

I want to create my database tables automatically with Hibernate and Postgresql, but I get errors about sequences. Is it possible to auto create sequences too with Hibernate, or do I have generate sequences manually?

Example of my entity:

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 config:

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

Exceptions:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖上得阳光 2024-09-23 01:49:02

这是我的解决方案。适用于 Postgres

<hibernate-mapping>

  <!-- ... -->

  <database-object>
    <create>CREATE SEQUENCE my_sequence</create>
    <drop>DROP SEQUENCE IF EXISTS my_sequence</drop>
  </database-object>

</hibernate-mapping>

Here is my solution. Applicable for Postgres

<hibernate-mapping>

  <!-- ... -->

  <database-object>
    <create>CREATE SEQUENCE my_sequence</create>
    <drop>DROP SEQUENCE IF EXISTS my_sequence</drop>
  </database-object>

</hibernate-mapping>
风柔一江水 2024-09-23 01:49:02

-我们无法使用 hbm2ddl 属性创建序列
-我们可以任意次数创建任意数量的表,但序列不是

特定于数据库的,我们必须在数据库中创建我们自己的序列

-We cant create sequences by using hbm2ddl property
-we can create any number of tables any no of times but not sequence

sequence is db specific and we have to create our selves sequence in db

夜吻♂芭芘 2024-09-23 01:49:01

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

  • org.hibernate.tool.hbm2ddl:在执行时记录所有 SQL DDL 语句

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

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

参考资料

Your mapping seems correct and I suggest activating logging of the following category to see what is happening exactly:

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

Set it to DEBUG and check the DDL statements (maybe update the question with the relevant parts).

PS: An allocationSize=1 is not very wise, using the default would be better. But that's unrelated.

References

站稳脚跟 2024-09-23 01:49:01

您必须在 application.properties 中启用自动架构创建,如 Hibernate 文档

hibernate.hbm2ddl.auto = create

或使用 Spring 时:

spring.jpa.hibernate.ddl-auto = create

但一般建议是不要在生产中使用它,请参阅Hibernate: hbm2ddl.auto=update in production?

使用数据库架构迁移工具,例如 LiquibaseFlyway 代替。

You have to enable automatic schema creation in application.properties as described in Hibernate docs:

hibernate.hbm2ddl.auto = create

or when using Spring:

spring.jpa.hibernate.ddl-auto = create

But the general recommendation is not to use this in production, see Hibernate: hbm2ddl.auto=update in production?

Use database schema migration tools like Liquibase or Flyway instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文