如何使用UUID作为Hibernate实体的主键?

发布于 2024-09-09 10:41:25 字数 1626 浏览 2 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-09-16 10:41:25

我将把键表示为字符串:

private UUID id;

@Id
@Column(name="id")
public String getId()
{
    return id.toString();
}

public void setId(String value)
{
    id = UUID.fromString(value);
}

public UUID idAsUUID()
{
    return id;
}

I would typify the key as String:

private UUID id;

@Id
@Column(name="id")
public String getId()
{
    return id.toString();
}

public void setId(String value)
{
    id = UUID.fromString(value);
}

public UUID idAsUUID()
{
    return id;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文