Hibernate,liquibase和hsqldb的Id生成问题 [英] Id generation issue with Hibernate, liquibase and hsqldb
问题描述
我使用liquibase创建了一个表:
I've created a table using liquibase:
<createTable tableName="employees">
<column name="id" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)">
<constraints nullable="false"/>
</column>
</createTable>
生成以下sql ddl查询:
The following sql ddl query is generated:
CREATE TABLE employees (id BIGINT NOT NULL, name VARCHAR(50) NOT NULL, CONSTRAINT PK_EMPLOYEES PRIMARY KEY (id));
相应的实体:
@Entity
@Table(name="employees")
public class EmployeeAccessProperty ...
@Id
@GeneratedValue
public long getId() {
return id;
}
...
现在,当我尝试通过JPA实现,生成sql查询以插入数据:
Now, when I try to save it via JPA implementation, the sql query is generated to insert data:
Hibernate: insert into employees (id, name) values (default, ?)
2013-05-20T14:29:22.525+0700 WARN SQL Error: -5544, SQLState: 42544
2013-05-20T14:29:22.526+0700 ERROR DEFAULT keyword cannot be used as column has no DEFAULT
我预计,当我没有指定id生成策略时,Hibernate会选择最好的一个根据提供商。我不明白为什么ID生成尝试使用默认值。我不确定,哪一方负责错误:hibernate,liqubase或hsqldb。
I expected, that when I don't specify id generation strategy, Hibernate will choose the best one according to the provider. I don't understand why for ID generation the default value is tried to be used. I'm not sure, which side is responsible for the error: hibernate, liqubase or hsqldb.
我该怎么办才能解决这个问题?请帮助我。
What can I do to resolve the problem? Please help me.
推荐答案
您没有告诉您的数据库您的主键必须自动生成。以这种方式创建表:
You are not telling to your database that your primary key must be autogenerated. Create your table this way :
CREATE TABLE employees (id BIGINT GENERATED BY DEFAULT AS IDENTITY, name VARCHAR(50) NOT NULL, CONSTRAINT PK_EMPLOYEES PRIMARY KEY (id));
这篇关于Hibernate,liquibase和hsqldb的Id生成问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!