如何使用Spring Boot注册自定义休眠类型? [英] How to register custom hibernate type using spring boot?

查看:117
本文介绍了如何使用Spring Boot注册自定义休眠类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在休眠状态下文档使用 配置 实例.如何使用Spring Boot访问该实例并注册我的类型?

In hibernate documentation custom types are registered using a Configuration instance. How can I access this instance and register my type using spring boot?

对实体使用 @TypeDef 可以正常工作,但是通过jpa本地查询,我得到了"javax.persistence.PersistenceException: org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111"

Using @TypeDef for entities works fine, but with a jpa native query i get a "javax.persistence.PersistenceException: org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111"

它与我自己的方言兼容,并通过

It works with my own dialect and registering via registerHibernateType, but with only one custom type registered to column type Types.OTHER (JDBC type 1111). Any other custom type registration overwrites old one.

查询很简单,仅选择给定自定义类型的常量值.

The query is simple, only selects a constant value of the given custom type.

使用休眠5.0.9和春季启动1.4.0.

Using hibernate 5.0.9 and spring boot 1.4.0.

推荐答案

似乎没有将结果自动映射到自定义类型的方法.

It seems there is no automatic mapping of the result to a custom type.

对于postgres,返回的jdbc PGObject 包含列类型名称(例如:'tsvector'),但是休眠自定义类型仅知道sql类型(在这种情况下为OTHER),而不是db类型名称,因此无法进行匹配.

For postgres, the returned jdbc PGObject contains the column type name (ex: 'tsvector'), but the hibernate custom types knows only the sql type (OTHER in this case), not the db type name, so it cannot do the match.

解决方法是解开jpa本机查询并注册返回的标量类型(自定义休眠类型),例如:

The workaround is to unwrap the jpa native query and register the returned scalar type (custom hibernate type), ex:

Query query = entityManager.createNativeQuery( "SELECT <SOMETHING> AS res" );
query.unwrap( SQLQuery.class ).addScalar( "res", CustomHibernateType.INSTANCE );

请注意,这会影响QueryDSL(4.1.4),因为没有将标量注册为休眠自定义类型的机制,因此jpa本机查询会失败(

Note that this affects QueryDSL (4.1.4), as there is no mechanism to register the scalar as hibernate custom type, so the jpa native query fails (JPASQLQuery). The workaround in this case is to use a non-jpa native query with a AbstractType registered with QueryDSL, and the conversion will be done automatically.

这篇关于如何使用Spring Boot注册自定义休眠类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆