DB2 不接受 UUID
DB2 not accepting UUID
我正在使用 UUID 数据类型生成字母数字主键。以下是代码:
public class Example {
private UUID id;
@Id
@Column(name = "ID", nullable = false, length = 36)
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
}
Java 代码正在生成类似于 c70e22b4-d2a8-11ea-87d0-0242ac130003 的 uuid。
但是在 DB2 table 中保存值时,在 UUID 列(在 DB2 中是 VARCHAR(25) 类型)中,我得到的值如 table 中的那样:
<failed to load>
com.ibm.db2.jcc.am.SqlException: [jcc][t4][1065][12306][4.23.42] Caught java.io.CharConversionException. See attached Throwable for details. ERRORCODE=-4220, SQLSTATE=null
at com.ibm.db2.jcc.am.ld.a(ld.java:794)
at com.ibm.db2.jcc.am.ld.a(ld.java:66)
at com.ibm.db2.jcc.am.ld.a(ld.java:125)
at com.ibm.db2.jcc.am.mc.a(mc.java:2963)
at com.ibm.db2.jcc.am.mc.p(mc.java:575)
at com.ibm.db2.jcc.am.mc.P(mc.java:1656)
at com.ibm.db2.jcc.am.ResultSet.getStringX(ResultSet.java:1223)
at com.ibm.db2.jcc.am.ResultSet.getString(ResultSet.java:1192)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.tryGetObject(RemoteResultSetImpl.java:1289)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getObject(RemoteResultSetImpl.java:1272)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getCurrentRow(RemoteResultSetImpl.java:1249)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getObjects(RemoteResultSetImpl.java:1229)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run[=11=](TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
at com.ibm.db2.jcc.am.r.a(r.java:52)
at com.ibm.db2.jcc.am.mc.a(mc.java:2952)
... 25 more
看来我无法将 UUID 映射到 Varchar,但找不到替代方案。
任何帮助将不胜感激。
不确定从哪里得到 25 的长度....
c70e22b4-d2a8-11ea-87d0-0242ac130003
是 36 个字符...
如果要存储二进制值,只需要16个字节,但必须CHAR(16) FOR BIT DATA
您可以使用 [VARCHAR_BIT_FORMAT][1]
函数
将 Db2 中的文本 UUID 字符串转换为二进制文件
CREATE TABLE UUID(U BINARY(16) NOT NULL PRIMARY KEY)
;
INSERT INTO UUID
VALUES VARCHAR_BIT_FORMAT(
'c70e22b4-d2a8-11ea-87d0-0242ac130003'
, 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
)
我正在使用 UUID 数据类型生成字母数字主键。以下是代码:
public class Example {
private UUID id;
@Id
@Column(name = "ID", nullable = false, length = 36)
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
}
Java 代码正在生成类似于 c70e22b4-d2a8-11ea-87d0-0242ac130003 的 uuid。 但是在 DB2 table 中保存值时,在 UUID 列(在 DB2 中是 VARCHAR(25) 类型)中,我得到的值如 table 中的那样:
<failed to load>
com.ibm.db2.jcc.am.SqlException: [jcc][t4][1065][12306][4.23.42] Caught java.io.CharConversionException. See attached Throwable for details. ERRORCODE=-4220, SQLSTATE=null
at com.ibm.db2.jcc.am.ld.a(ld.java:794)
at com.ibm.db2.jcc.am.ld.a(ld.java:66)
at com.ibm.db2.jcc.am.ld.a(ld.java:125)
at com.ibm.db2.jcc.am.mc.a(mc.java:2963)
at com.ibm.db2.jcc.am.mc.p(mc.java:575)
at com.ibm.db2.jcc.am.mc.P(mc.java:1656)
at com.ibm.db2.jcc.am.ResultSet.getStringX(ResultSet.java:1223)
at com.ibm.db2.jcc.am.ResultSet.getString(ResultSet.java:1192)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.tryGetObject(RemoteResultSetImpl.java:1289)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getObject(RemoteResultSetImpl.java:1272)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getCurrentRow(RemoteResultSetImpl.java:1249)
at com.intellij.database.remote.jdbc.impl.RemoteResultSetImpl.getObjects(RemoteResultSetImpl.java:1229)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run[=11=](TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
at com.ibm.db2.jcc.am.r.a(r.java:52)
at com.ibm.db2.jcc.am.mc.a(mc.java:2952)
... 25 more
看来我无法将 UUID 映射到 Varchar,但找不到替代方案。 任何帮助将不胜感激。
不确定从哪里得到 25 的长度....
c70e22b4-d2a8-11ea-87d0-0242ac130003
是 36 个字符...
如果要存储二进制值,只需要16个字节,但必须CHAR(16) FOR BIT DATA
您可以使用 [VARCHAR_BIT_FORMAT][1]
函数
CREATE TABLE UUID(U BINARY(16) NOT NULL PRIMARY KEY)
;
INSERT INTO UUID
VALUES VARCHAR_BIT_FORMAT(
'c70e22b4-d2a8-11ea-87d0-0242ac130003'
, 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
)