如何使用UUID作为Hibernate实体的主键?
我正在尝试在 Hibernate 中使用 UUID。
具有以下 @Entity
基类描述(带有 @MappedSuperclass
注释):
@Id
@Column(name="id")
private UUID id;
public UUID getId()
{
return id;
}
为了测试,我试图从数据库中读取我的类的所有实体(数据库存在,记录存在)。我的数据库是 PostgreSQL 8.4,支持 UUID,主键是 UUID 类型。
运行我的测试,我在日志中得到以下内容:
[junit] 14:21:34,839 INFO LongType:203 - could not read column value from result set: id0_0_; Bad value for type long : d46668b8-e494-42ba-803f-c43524ac3f31
...
[junit] org.postgresql.util.PSQLException: Bad value for type long : d46668b8-e494-42ba-803f-c43524ac3f31
[junit] at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toLong(AbstractJdbc2ResultSet.java:2796)
[junit] at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2019)
[junit] at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2431)
[junit] at org.apache.commons.dbcp.DelegatingResultSet.getLong(DelegatingResultSet.java:240)
[junit] at org.hibernate.type.LongType.get(LongType.java:51)
[junit] at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
[junit] at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
[junit] at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
看起来 Hibernate 并没有真正使用 UUID 类型,它可以从我的实体注释描述中解析。使用 Spring 而不是 UUID 的情况相同。
我还能如何告诉 Hibernate 我想使用 UUID 或 String
而不是 Long
作为主键?
PS:Hibernate我使用3.3.2.GA。我不使用 EntityManager。我使用注释描述映射并使用 Spring 配置 Hibernate。
I am trying to use UUID in Hibernate.
Have the following @Entity
base-class description (with @MappedSuperclass
annotation):
@Id
@Column(name="id")
private UUID id;
public UUID getId()
{
return id;
}
For test, I am trying to read all entities of my class from database (database exists, records exist). My database is PostgreSQL 8.4 with UUID support and primary key is of UUID type.
Running my test I get the following in log:
[junit] 14:21:34,839 INFO LongType:203 - could not read column value from result set: id0_0_; Bad value for type long : d46668b8-e494-42ba-803f-c43524ac3f31
...
[junit] org.postgresql.util.PSQLException: Bad value for type long : d46668b8-e494-42ba-803f-c43524ac3f31
[junit] at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toLong(AbstractJdbc2ResultSet.java:2796)
[junit] at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2019)
[junit] at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getLong(AbstractJdbc2ResultSet.java:2431)
[junit] at org.apache.commons.dbcp.DelegatingResultSet.getLong(DelegatingResultSet.java:240)
[junit] at org.hibernate.type.LongType.get(LongType.java:51)
[junit] at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
[junit] at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
[junit] at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1121)
Looks like Hibernate doesn't really use UUID type it may parse from my entity annotated description. The same situation with Spring instead of UUID.
How else I can tell Hibernate I would like to user either UUID or String
instead of Long
for primary key?
PS: Hibernate I use 3.3.2.GA. I don't use EntityManager
. I describe mapping with annotations and configure Hibernate with Spring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将把键表示为字符串:
I would typify the key as String: