简体   繁体   English

Defina在Hibernate 4上的默认类型映射

[英]Defina a default type mapping on Hibernate 4

I need to define/override the default hibernate mapping to solve the problem of the java.util.UUID default mapping. 我需要定义/覆盖默认的休眠映射来解决java.util.UUID默认映射的问题。

I've been looking at several comments related to this topic: Here Postgresql UUID supported by Hibernate? 我一直在寻找与此主题相关的一些评论:在这里,Hibernate支持的Postgresql UUID吗? and Postgres + Hibernate + Java UUID and Persisting UUID in PostgreSQL using JPA and and Hibernate documentation. 使用JPA和Hibernate文档在PostgreSQL中使用 Postgres + Hibernate + Java UUID持久性UUID

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 (eg H2) 像@Type(type =“ pg-uuid”)之类的东西对我不起作用,因为我需要使用其他数据库(例如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. 因此,正如一些评论中所述,我需要“将org.hibernate.type.PostgresUUIDType注册为默认的Hibernate类型映射,以显示为java.util.UUID的所有属性”。

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? 您能否解释一下或为我提供有关如何在Hibernate 4.2中覆盖默认类型映射的更详细的信息?

Thanks, and regards, 谢谢并恭祝安康,

The best solution i've found is to define a custom type that will switch depending on environmental variables, similar to what is done here: 我发现最好的解决方案是定义一个自定义类型,该类型将根据环境变量进行切换,类似于此处所做的:

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

So i defined a MyUuid class, annotate the related UUID fields in the entity with @Type(type="whatever") and, at entity level, i defined the new mapping 因此,我定义了一个MyUuid类,使用@Type(type =“ type” whatever“)注释了实体中的相关UUID字段,并在实体级别定义了新的映射

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM