Replacement for Hibernate's deprecated Type annotation?
Asked Answered
F

4

37

Upon upgrading to Hibernate 5.6.0.Final, I found that org.hibernate.annotations.Type is now deprecated. I use this annotation in many of my entities, including places where I leverage Hibernate's own NumericBooleanType:

 @Type(type = "org.hibernate.type.NumericBooleanType")
 private Boolean debug = false;

I've searched Hibernate docs (migration guides - of which the 6.0 version gives me a 404 from http://hibernate.org/orm/documentation/6.0/), Javadocs, forums, and the web to find a replacement, to no avail.

Does anyone know what I can do now in Hibernate 5.6.0 to prepare my code using the Type annotation for the transition to Hibernate 6?

Frangipane answered 5/11, 2021 at 19:49 Comment(2)
Why exactly can't you just remove it ?Nieves
@ArthurKlezovich - I am not sure what you are suggesting. I am using a handful of custom types. Are you saying that, specifically for the NumericBooleanType, I change my database and entity to not use a custom type?Frangipane
C
32

For those who are wondering what's the alternative approach with hibernate 6, you have to change this from:

@Type(type = "org.hibernate.type.NumericBooleanType")
private Boolean debug = false;

to this:

@Convert(converter = org.hibernate.type.NumericBooleanConverter.class)
private Boolean debug = false;

Refer - https://docs.jboss.org/hibernate/orm/6.0/userguide/html_single/Hibernate_User_Guide.html#basic-boolean

Classic answered 29/4, 2022 at 15:4 Comment(0)
B
25

Edit: the change was reverted in 5.6.3. There should no longer be any deprecation warnings.


There is nothing you can do until you upgrade to 6.0, unless sufficient people complain on this issue that they revert the change in 5.6.1 or something.

Hibernate have made the unusual decision to deprecate these annotations early, before there's any replacement.

I don't know why. The only thing it will do is make people suppress the warnings, and then forget about it when 6.0 is released and never make the change.

Basilio answered 19/11, 2021 at 17:44 Comment(8)
Just discovered this one with the @TypeDef annotation on upgrading to SpringBoot 2.6.1 which pulls in Hibernate 5.6.1 final - very strange approach to deprecate before providing a replacement. Annoying warnings we can't get rid of now (unless we revert).Bumptious
First, people make the interpretation that a deprecation need have an immediate replacement. But show me where this is required - aka, its not. We do plan on reverting that, but I think people need to slow their roll with this being some illegal changeOverrule
@SteveEbersole From Wikipedia: In several fields, deprecation is the discouragement of use of some terminology, feature, design, or practice, typically because it has been superseded or is no longer considered efficient or safe, without completely removing it or prohibiting its use.Basilio
@SteveEbersole that is not the case here. This API has not been superseded, and there's been no statement regarding why it shouldn't be used. As far as I can see, there's nothing wrong with its performance or safety. The problem is not just that you did something very unusual, but that you didn't explain it either.Basilio
So Wikipedia is now the authority on Java? How about you find passages like this in the actual Java spec/docs?Overrule
@SteveEbersole The Java docs say "A program element annotated \@Deprecated is one that programmers are discouraged from using.", which is not the case here. If Hibernate intends to recommend that custom types are not to be used in Hibernate 5, it has not explained this anywhere.Basilio
Again, quite a leap of logic you are making here which just happens to match your preconceived idea on the subject. "discouraged from using" in no way means "there is a replacement". Look, we've reverted this, but this continued argument about the English language is unproductive and you are actually just wrong about the English meanings of these wordsOverrule
@SteveEbersole Sonar marks usage of deprecated classes as code smell. This can (and does) break qualility builds for many Java projects, mine among them.Maecenas
P
0

In Hibernate 4 and 5, you need to write custom mapping. Do not worry. It's not as difficult as it might seem. It is necessary to implement the UserType interface and register the mapping.

With Hibernate 6 everything is much simpler. It supports JSON mapping out of the box.

for example:

With JSON support introduced in Hibernate 6, you now only need to annotate an object field with the @JdbcTypeCode annotation and set the type to SqlTypes.JSON. Hibernate will detect the JSON library in the classpath and use it to serialize and deserialize the value.

Here's a good article about it: https://habr.com/ru/companies/otus/articles/688714/

Pulling answered 19/2 at 10:49 Comment(1)
The article you linked to is in Russian.Sassanid
O
-13

That's quite a logical leap that just happens to fit with your preconceived opinion. "discouraged from using" in no way implies "there is a current replacement

Overrule answered 9/12, 2021 at 17:51 Comment(2)
that's a hill worthy to die on. Nobody googles anymore, nobody recognized you from your work either. Sad truth...Garver
I recognized him, and I still think this answer is silly. If you look at Javadoc for @Deprecated, it specifically mentions that you should include instructions on the correct replacement. Not threaten to take a feature away with no opportunity for gradual transition.Bub

© 2022 - 2024 — McMap. All rights reserved.