简体   繁体   中英

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. I have used heuristicType method from org.hibernate.type.TypeResolver in 4.3. This class is deprecated from 5.3 version, how can rewrite this code.

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. The first type handler will be determined and cached for subsequent calls on the same Query object. Do remember, however, that 0 is an invalid index. They start at 1 in JPA. It was only due to an implementation failure that they once allowed 0-indexed parameters. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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