gpt4 book ai didi

java - 从 Hibernate 中的插入中排除自动生成的标识列

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:21 28 4
gpt4 key购买 nike

我正在尝试使用 Hibernate 在 DB2 数据库中插入一个实体。我的实体有一个@EmbeddedId,带有@Embeddedable 的类有一个字段,它是我的 DB2 表中自动生成的列。持久化实体后,它因此错误而失败

SQL Error: -798, SQLState: 428C9
DB2 SQL Error: SQLCODE=-798, SQLSTATE=428C9, SQLERRMC=ID, DRIVER=4.9.78
SQL Error: -727, SQLState: 56098
DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-798;428C9;ID, DRIVER=4.9.78
On release of batch it still contained JDBC statements
Error Refeshing Firmware Level Sources :org.springframework.dao.InvalidDataAccessResourceUsageException: could not perform addBatch; SQL [insert into Db2Schema.table_entity ( ID, KEY1, KEY2, KEY3, .....) values (?, ?, ?, ?, ......)]; nested exception is org.hibernate.exception.SQLGrammarException: could not perform addBatch

这是我的实体(只显示使用@EmbeddedId 的部分)

@Entity
@Table(name="table_entity", schema="Db2Schema")
@DynamicUpdate @DynamicInsert
public class TableEntity implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private TableEntityKey tableEntityKey;

............ Rest of the entity Fields .............

}

这是带有@Embeddable 注解的类:

@Embeddable
public class TableEntityKey implements Serializable{

@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID", insertable = false, updatable = false)
// @Transient
private Integer id;

@Column(name="key1", length=254, insertable=false, updatable=false)
private String key1;

@Column(name="key2", nullable=false, length=256, insertable=false, updatable=false)
private String key2;

@Column(name="key3", nullable=false, length=256, insertable=false, updatable=false)
private String key3;

..... getters and setters go here ................

}

POM.xml 中的 Hibernate 依赖项

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>4.3.11.Final</version>
</dependency>

这是DDL

CREATE TABLE
TABLE_ENTITY
(
..... REST OF THE COLUMNS OF THIS TABLE .......

ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 14350),
KEY1 VARCHAR(256) NOT NULL,
KEY2 VARCHAR(256) NOT NULL,
KEY3 VARCHAR(256) NOT NULL,
PRIMARY KEY (ID)
);

hibernate 生成的 INSERT 语句:

insert into Db2Schema.table_entity ( ID, KEY1, KEY2, KEY3, .....) values (?, ?, ?, ?, ......)

如果它在 Id 字段上放置 @Transient 注释,效果很好,但我不想使用 @Transient 注释,因为它会一起忽略这个字段,我需要在我的用例中进一步使用它。我想要做的就是在插入过程中忽略这个字段。我试过设置 insertable=false 但它也不起作用。

感谢您的帮助!

最佳答案

将 IDENTITY 字段从 @Embeddable Class 转移到 @Entity Class 对我有用。

它现在看起来像这样:

    @Entity
@Table(name="table_entity", schema="Db2Schema")
@DynamicUpdate @DynamicInsert
public class TableEntity implements Serializable {

private static final long serialVersionUID = 1L;

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

@EmbeddedId
private TableEntityKey tableEntityKey;

............ Rest of the entity Fields .............

}


@Embeddable
public class TableEntityKey implements Serializable{


@Column(name="key1", length=254)
private String key1;

@Column(name="key2", nullable=false, length=256)
private String key2;

@Column(name="key3", nullable=false, length=256)
private String key3;

..... getters and setters go here ................

}

关于java - 从 Hibernate 中的插入中排除自动生成的标识列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49447915/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com