HSQLDB的Hibernate @generatedvalue [英] Hibernate @generatedvalue for HSQLDB

查看:87
本文介绍了HSQLDB的Hibernate @generatedvalue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  ..我有以下定义, 
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =ID)
private Integer id;
...

但是这似乎不会生成唯一的ID;而是尝试将空插入到导致失败的列中。如果我手动创建一个序列和生成策略来使用该序列,那么数据将按预期保持。

不是auto的生成策略意味着提供者(在这种情况下,hibernate会自动选择正确的方法并根据需要做所有繁重的工作(创建序列,使用本地方法或适用于特定平台的任何工具)?我的理解是不正确的吗?

解决方案


不自动生成策略意味着提供者在这种情况下,hibernate会自动选择正确的方法并根据需要做所有繁重的工作(创建序列,使用本地方法或适用于特定平台的任何工具)?我的理解是不正确的?


理论上它(它默认为使用HSQLDB的IDENTITY),它适用于我。这引出了以下问题:


  • 您使用什么方言(以防万一)?

  • 你是如何创建表的?

  • 你能否显示DDL(如果需要,激活 org.hibernate.tool.hbm2ddl 的日志记录)?

  • 如何插入(通过Hibernate的API,对吧?)?



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

  create表Foo(
id bigint默认生成为身份(从1开始),
bar varchar(100),
主键(id)







我使用HSQL DB管理器创建了表。只是普通的创建表地址...我没有设置ID列作为身份在我的情况 - 只需将其设置为主键。


然后你有答案,使用 IDENTITY 列。 Hibernate确实选择了正确的策略,并且生成了适当的 INSERT 语句(将中的保存到 IDENTITY 列中),它不会创建或更改物理模型,使用DDL生成和导出功能。

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

...
@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.

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?

解决方案

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?

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

  • 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.hbm2ddl if required)?
  • How do you insert (through Hibernate's API, right?)?

Here is a sample DDL for an entity Foo when using HSQLDB:

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.

Then you have your answer, use an IDENTITY column.

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

这篇关于HSQLDB的Hibernate @generatedvalue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆