Hibernate typeresolver

I know that hibernate has recently changed its type system to 3.6. I think this now allows you to associate a Java class with a type (or UserType). For example, I use joda-time and have a pair of UserTypes that map LocalDate and LocalDateTime to the corresponding SQL types.

This works great when working with objects, but if I want to pass the joda type as an HQL parameter, hibernation gets confused, so I have to remember that every time I make a call, I have to specify the type.

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

I think that now at the configuration step Configuration / SessionFactory we can say that LocalDateTime → LocalDatetimeType, but I'm not sure how to do this. I found TypeResolver, but I had problems decrypting the method that I have to call to achieve this.

Or please correct me if this is not possible even when using the new type in version 3.6.

+15
java types hibernate jodatime
Jan 18 2018-11-18T00:
source share
1 answer

Answering my own question.

Simple enough when you know how to do this.

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

A search in TypeResolver for unknown types uses the class name, so it registers the fully qualified name of the class as registeringKey is doing the job.

+10
Jan 26 2018-11-11T00:
source share
— -



All Articles