在使用byte []作为JPA实体中的主键时是否存在任何问题?将UUID用作数据库主键,Java类型是一个字节[]
我想使用一个UUID作为我的主键,但存储为一个字符串,我觉得它会太大。
我的想法做这样的事情的ID存储为一个byte []并将其设置为我的实体的ID的:
public static byte[] byteArray(UUID uuid) {
long lsb = uuid.getLeastSignificantBits();
long msb = uuid.getMostSignificantBits();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
try {
dos.writeLong(lsb);
dos.writeLong(msb);
dos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] data = bos.toByteArray();
// System.out.println("Byte Array Length "+data.length);
return data;
}
我有没有把指数在此DB中的任何麻烦?我正在使用Postgres和HSQL。我使用Hibernate作为我的JPA提供程序。
需要在数据库之外生成ID,并且需要能够使用UUID从DB中检索任何对象。 – systemoutprintln 2009-04-08 21:31:02