如何使用UUID作为Hibernate实体的主键? [英] How to use UUID as primary key for Hibernate Entity?

查看:357
本文介绍了如何使用UUID作为Hibernate实体的主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



具有以下 @Entity 基类描述( @MappedSuperclass 注解):

  @Id 
@列(name =id)
私人UUID ID;

public UUID getId()
{
return id;





$ b

为了测试,我试图从数据库中读取我的类的所有实体(数据库存在,记录存在)。我的数据库是PostgreSQL 8.4,支持UUID,主键是UUID类型。



运行我的测试我在日志中获得以下内容:

  [junit] 14:21:34,83​​9 INFO LongType:203  - 无法从结果集中读取列值:id0_0_;对于long类型的错误值:d46668b8-e494-42ba-803f-c43524ac3f31 

...

  [junit] org.postgresql.util.PSQLException: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.loader.Loader.getKeyFromResultSet(Loader.java:1121)
[junit] C>

看起来Hibernate并不真正使用UUID类型,它可能会从我的实体注释描述中解析出来。与Spring相同的情况,而不是UUID。



我可以告诉Hibernate怎么样我想使用UUID或 String 而不是 Long 作为主键?



PS:Hibernate我使用3.3.2.GA.我不使用 EntityManager 。我描述了使用注释映射和使用Spring配置Hibernate。

解决方案


$ b

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

解决方案

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;
}

这篇关于如何使用UUID作为Hibernate实体的主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆