简体   繁体   English

如何将 hibernate 从 4.3 迁移到 5.3 -Type Resolver Class

[英]How to migrate hibernate from 4.3 to 5.3 -Type Resolver Class

I am upgrading my application from hibernate 4.3 to 5.3.我正在将我的应用程序从 hibernate 4.3 升级到 5.3。 I have used heuristicType method from org.hibernate.type.TypeResolver in 4.3.我在 4.3 中使用了来自 org.hibernate.type.TypeResolver 的 heuristicType 方法。 This class is deprecated from 5.3 version, how can rewrite this code.此 class 已从 5.3 版本中弃用,如何重写此代码。

Sample Code:示例代码:

Query query = (Query) session.createQuery(queryString);
query.setParameter(0,value,  (new TypeResolver()).heuristicType(value.getClass().getName()));

If you're worried about it not detecting the type when the value is null , you can just pass a not-null constant once with setParameter and then the real value.如果您担心当值为null时它无法检测到类型,您可以使用setParameter传递一个非空常量,然后传递实际值。 The first type handler will be determined and cached for subsequent calls on the same Query object.第一个类型的处理程序将被确定和缓存,以供对同一Query object 的后续调用。 Do remember, however, that 0 is an invalid index.但是请记住,0 是无效索引。 They start at 1 in JPA.它们从 JPA 中的 1 开始。 It was only due to an implementation failure that they once allowed 0-indexed parameters.只是由于实施失败,他们曾经允许 0 索引参数。 Just change them to names and you'll never have to worry about it.只需将它们更改为名称,您就不必担心它。

In short:简而言之:

Query query = (Query) session.createQuery(queryString);
query.setParameter("name", NOT_NULL_CONSTANT);
query.setParameter("name", value); // actual value

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

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