DB2 + JPA 抛出错误:在表中找不到列 [英] DB2 + JPA throwing an error: column not found in the table

查看:24
本文介绍了DB2 + JPA 抛出错误:在表中找不到列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 JPA 尝试持久化实体时,DB2-express 抛出了一个看似不可能的错误:

The DB2-express is throwing an error that seems impossible, when JPA tries to persist an entity:

ReportingSQLException: "ITEMID" is not valid in the context where it is used..

INSERT INTO NULLID."DynamicDatabaseTable" ("colname", "rownumber", "value")
VALUES (?, ?, ?) [params=?, ?, ?]

由于上面的查询中没有使用ITEMID,怎么会出现这个错误? ITEMID 是自动标识生成的列.

Since ITEMID is not used in the query above, how can this error be? The ITEMID is an auto-identity-generated column.

我已经尝试手动执行查询,它工作正常,所以有人知道这是怎么回事吗?即使经过大量的谷歌搜索,我在这里完全一无所知.

I have tried executing manually the query and it works OK, so anybody know what's all about? Even after lot of googling, I'm totally clueless here.

DB2中的数据库表没问题,我有三重检查:

The database table in DB2 is fine, I have triple check:

"itemid" BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH 1...), 
"rownumber" BIGINT NOT NULL, 
"colname" CHAR(30 OCTETS) NOT NULL, 
"value" VARCHAR(254 OCTETS)

<小时>

我使用 Eclipse 向导创建了一个简单的 java表中的 JPA 实体",并且我已经三次检查该类是否正常.


I had created a simple java "JPA Entity from a table", by using the Eclipse wizard, and I've triple check the class is fine.

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name=""itemid"", unique=true, nullable=false)
private long itemid;

@Column(name=""rownumber"", nullable=false)
private long rownumber;

@Column(name=""colname"", nullable=false, length=30)
private String colname;

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

为了以防万一,我还重新启动了整台计算机,但什么都没有……而且我使用的是几周前全新安装的最新版本.

I have also restarted the whole computer just in case, but nothing... And I'm using most recent versions, clean installed couple of weeks ago.

推荐答案

"itemid" 不相等 itemid -- SQL 标识符默认转换为大写 如果没有用双引号括起来.根据您的 DDL,您使用小写列名创建了表.显然,JPA 没有正确处理该问题,并且在生成的代码中的某处使用了未加引号的 itemid,它被转换为大写,毫不奇怪,在表中找不到该列.

"itemid" is not equal itemid -- SQL identifiers are by default converted to upper case if not enclosed in double quotation marks. According to your DDL you created the table with lowercase column names. Apparently, JPA does not handle that properly and somewhere in the generated code uses unquoted itemid, which gets converted to upper case and, not surprisingly, the column cannot be found in the table.

没有理由在 SQL 中使用区分大小写的标识符,因此我建议您不要这样做以避免此类问题.

There is no reason to use case sensitive identifiers in SQL, so I suggest you don't do that to avoid these kinds of problems.

这篇关于DB2 + JPA 抛出错误:在表中找不到列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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