Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

java - Hibernate TypeResolver

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...