4

I need to define/override the default hibernate mapping to solve the problem of the java.util.UUID default mapping.

I've been looking at several comments related to this topic: Here Postgresql UUID supported by Hibernate? and Postgres + Hibernate + Java UUID and Persisting UUID in PostgreSQL using JPA and and Hibernate documentation.

However the easier solution of annotate; something like @Type(type="pg-uuid"), does not works for me since i need to run with different databases (e.g. H2)

So what i need is to "register org.hibernate.type.PostgresUUIDType as the default Hibernate type mapping for all attributes exposed as java.util.UUID" as has been said in several of the comments.

Sadly after checking the documentation i still don't get exactly how to do it.

Could you, please, explain or point me to a more detailed information about how to override the default type mapping in Hibernate 4.2?

Thanks, and regards,

4

1 回答 1

2

我发现的最佳解决方案是定义一个自定义类型,该类型将根据环境变量进行切换,类似于此处所做的:

https://zorq.net/b/2012/04/21/switching-hibernates-uuid-type-mapping-per-database/

所以我定义了一个 MyUuid 类,用 @Type(type="whatever") 注释实体中的相关 UUID 字段,并且在实体级别,我定义了新的映射

@TypeDef(name="whatever", defaultForType = UUID.class, typeClass = MyUuid.class)

于 2013-04-17T13:31:24.703 回答