DB2 + JPA throwing an error: column not found in the table

房东的猫 提交于 2019-12-02 20:19:30

问题


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=?, ?, ?]

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.


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)

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/29172741/db2-jpa-throwing-an-error-column-not-found-in-the-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!