java HSQLDB 的 Hibernate @generatedvalue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3596848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:34:37  来源:igfitidea点击:

Hibernate @generatedvalue for HSQLDB

javahibernateormjpahsqldb

提问by calvinkrishy

I have the following definition for an id field in an entity that is mapped to a table in HSQLDB.

对于映射到 HSQLDB 中的表的实体中的 id 字段,我有以下定义。

...
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID")
private Integer id;
...

But this does not seem to generate the an unique id; instead an attempt is made to insert null into the column which results in failure. If, I manually create a sequence and generation strategy to use that sequence then the data is persisted as expected.

但这似乎并没有生成唯一的 id;相反,尝试将空值插入到导致失败的列中。如果我手动创建一个序列和生成策略来使用该序列,那么数据将按预期持久化。

Doesn't a generation strategy of auto imply that the provider (hibernate in this case) will automatically choose the correct approach and do all the heavy lifting as needed (create sequence, use a native approach or whatever works for that particular platform)? Is my understanding incorrect?

auto 的生成策略是否意味着提供者(在这种情况下为休眠)将自动选择正确的方法并根据需要执行所有繁重的工作(创建序列、使用本机方法或任何适用于该特定平台的方法)?我的理解有误吗?

回答by Pascal Thivent

Doesn't a generation strategy of auto imply that the provider (hibernate in this case) will automatically choose the correct approach and do all the heavy lifting as needed (create sequence, use a native approach or whatever works for that particular platform)? Is my understanding incorrect?

auto 的生成策略是否意味着提供者(在这种情况下为休眠)将自动选择正确的方法并根据需要执行所有繁重的工作(创建序列、使用本机方法或任何适用于该特定平台的方法)?我的理解有误吗?

It does in theory (it defaults to IDENTITY with HSQLDB) and it works for me. This begs the following questions:

理论上它确实如此(它默认为带有 HSQLDB 的 IDENTITY)并且它对我有用。这引出了以下问题:

  • What dialect are you using (just in case)?
  • How did you create the table?
  • Can you show the DDL (activate the logging of org.hibernate.tool.hbm2ddlif required)?
  • How do you insert (through Hibernate's API, right?)?
  • 你用什么方言(以防万一)?
  • 你是如何创建表的?
  • 你能显示 DDL(org.hibernate.tool.hbm2ddl如果需要,激活日志记录)吗?
  • 你如何插入(通过 Hibernate 的 API,对吧?)?

Here is a sample DDL for an entity Foowhen using HSQLDB:

以下是Foo使用 HSQLDB 时实体的示例 DDL :

create table Foo (
    id bigint generated by default as identity (start with 1), 
    bar varchar(100),
    primary key (id)
)


I created the table using the HSQL DB manager. Just normal create table address... I had not set the id column as identity in my case - just set it as primary key.

我使用 HSQL DB 管理器创建了该表。只是正常的创建表地址......在我的情况下,我没有将 id 列设置为身份 - 只需将其设置为主键。

Then you have your answer, use an IDENTITYcolumn.

然后你有你的答案,使用一个IDENTITY列。

While Hibernate does choose the right strategy and does generate the appropriate INSERTstatements (passing nullinto the id which is expected to be persisted into an IDENTITYcolumn), it won't create or alter your physical model if you don't use the DDL generation and export capabilities.

虽然 Hibernate 确实选择了正确的策略并生成了适当的INSERT语句(传递null到预期会持久化到IDENTITY列中的 id ),但如果您不使用 DDL 生成和导出,它不会创建或更改您的物理模型能力。

回答by Dieter Hubau

I had the same issue when using a JpaSchemaGeneratorutility class that I wrote.

使用JpaSchemaGenerator我编写的实用程序类时,我遇到了同样的问题。

When generating the schema for a org.hibernate.dialect.HSQLDialect(where I use a SEQUENCE to generate my unique IDs), I use the following Hibernate property:

在为 a 生成架构时org.hibernate.dialect.HSQLDialect(我使用 SEQUENCE 生成我的唯一 ID),我使用以下 Hibernate 属性:

hibernate.id.new_generator_mappings=true

hibernate.id.new_generator_mappings=true

This results in the following CREATEstatement:

这导致以下CREATE语句:

CREATE TABLE BATCH (
    BAT_ID NUMBER(19,0) NOT NULL,
    BAT_EXPIRY_DATE TIMESTAMP,
    BAT_NUMBER VARCHAR2(255 CHAR),
    BAT_MAT_ID NUMBER(19,0),
    PRIMARY KEY (BAT_ID)
);

But when I use this same property in my utility class to generate a schema using the org.hibernate.dialect.HSQLDialect, I get the following CREATEstatement:

但是,当我在实用程序类中使用相同的属性来使用 生成模式时org.hibernate.dialect.HSQLDialect,我得到以下CREATE语句:

CREATE TABLE BATCH (
    BAT_ID BIGINT NOT NULL,
    BAT_EXPIRY_DATE TIMESTAMP,
    BAT_NUMBER VARCHAR(255),
    BAT_MAT_ID BIGINT,
    PRIMARY KEY (BAT_ID)
);

This would mean that if I created a Batch without an ID, it would not generate it for me and the NOT NULLconstraint would cause an exception.

这意味着如果我创建了一个没有 ID 的 Batch,它不会为我生成它,并且NOT NULL约束会导致异常。

If I change the Hibernate property to the following:

如果我将 Hibernate 属性更改为以下内容:

hibernate.id.new_generator_mappings=false

hibernate.id.new_generator_mappings=false

Then it would generate the following CREATEstatement:

然后它将生成以下CREATE语句:

CREATE TABLE BATCH (
    BAT_ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
    BAT_EXPIRY_DATE TIMESTAMP,
    BAT_NUMBER VARCHAR(255),
    BAT_MAT_ID BIGINT,
    PRIMARY KEY (BAT_ID)
);

Which works perfectly when creating JPA entities with Hibernate.

在使用 Hibernate 创建 JPA 实体时,它完美地工作。