我正在尝试使用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 Annotation的类:
@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,但它也不起作用。
感谢您的帮助!
答案 0 :(得分:0)
从@Embeddable Class到@Entity Class的IDENTITY字段为我工作。
现在看起来像这样:
@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 ................
}