使用自动创建表时,Hibernate 不会自动创建数据库序列
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我的解决方案。适用于 Postgres
Here is my solution. Applicable for Postgres
-我们无法使用 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
您的映射似乎是正确的,我建议激活以下类别的日志记录以查看到底发生了什么:
将其设置为 DEBUG 并检查 DDL 语句(也许用相关部分更新问题) 。
PS:
allocationSize=1
不是很明智,使用默认值会更好。但这是无关的。参考资料
Your mapping seems correct and I suggest activating logging of the following category to see what is happening exactly:
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
您必须在
application.properties
中启用自动架构创建,如 Hibernate 文档:或使用 Spring 时:
但一般建议是不要在生产中使用它,请参阅Hibernate: hbm2ddl.auto=update in production?
使用数据库架构迁移工具,例如 Liquibase 或 Flyway 代替。
You have to enable automatic schema creation in
application.properties
as described in Hibernate docs:or when using Spring:
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.