Hibernate TypeResolver

心已入冬 提交于 2019-12-18 04:34:07

问题


I'm aware that hibernate recently redid its type system in 3.6. I think this now allows you do associate a java Class with a Type (or UserType). For example I use joda-time and have a couple of UserTypes that map the LocalDate and LocalDateTime into appropriate SQL types.

This works fine when working with objects but if I want to pass a joda type as a HQL parameter hibernate gets confused so I have to remember to supply the Type each time I make a call.

query.setParameter( "now", new LocalDateTime(), Hibernate.custom( LocalDateTimeType.class ) );

I think it is now possible during the Configuration/SessionFactory setup phase to say LocalDateTime -> LocalDatetimeType but I'm not sure how to do this. I found the TypeResolver but had trouble deciphering which method I should be calling to achieve this.

Or please correct me if this is not possible even with the new type stuff in 3.6.


回答1:


Answering my own question.

Simple enough when you know how.

configuration.getTypeResolver().registerTypeOverride( LocalDateTimeType.TYPE, new String[]{ LocalDateTime.class.getName() } );
configuration.getTypeResolver().registerTypeOverride( LocalDateType.TYPE, new String[]{ LocalDate.class.getName() } );

The TypeResolver lookups for unknown types use the class name so registering the full name of the class as the registrationKey does the job.



来源:https://stackoverflow.com/questions/4725719/hibernate-typeresolver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!