@TypeDef doesn't work for enums

Description

@TypeDef is supposed to allow users to define standard types to be used for specific classes. However, due to a bug in implementation, it simply doesn't work for enums.

Enums are implicitly handled by the JPA spec. However, if a @TypeDef is defined for a particular enum, it will never be picked up due to bug in org.hibernate.cfg.annotations.SimpleValueBinder .

This class has a concept of a "explicitType", i.e., a type that was explicitly specified for a property. However, even if @EnumType is not used to annotate a property, method setType(XProperty,XClass) considers the property as annotated with it. Then, when fillSimpleValue() is called as part of second-pass compilation, the ternary in line 344 chooses org.hibernate.type.EnumType instead of the enum FQN, causing any @TypeDef to be ignored.

This behaviour should be fixed by considering a non-annotated enum property as implicitly typed, not explicitly. Then, the resolution order should be changed to explicitType -> returnedClassName -> implicitType. This would preserve default, expected behaviour for regular enums, while allowing @TypeDef to be used for enums as well.

Environment

hibernate-jpa-2.0-api:1.0.1-Final; hibernate-core:4.1.0.Final; hibernate-entitymanager:4.1.0.Final

Activity

Show:
Gail Badner
March 1, 2012, 2:52 AM

Please create a pull request from your fork of the hibernate-orm repository (https://github.com/hibernate/hibernate-orm). Please include your fix and a test case that reproduces your issue in the pull request.

Thanks,
Gail

Janario Oliveira
July 12, 2012, 12:32 AM
Edited

I sent a pull request with a test and the correction.
It is with that description: "HHH-7108 TypeDef in Enum"

Basically there are 3 kinds of type:
explicit: When an annotation that define a type is informed how @Type, @Lob, @Temporal, @Enumerated etc.
implicit: When annotation is not informed but there is a global type for the returned class
default: When annotation is not informed and it has a default type for returned class like @Enumerated(ORDINAL), int, string etc.

This provide an easy way to extend EnumType

https://github.com/hibernate/hibernate-orm/pull/355
https://github.com/Janario/hibernate-orm

Brett Meyer
March 7, 2014, 10:08 PM

Bulk closing tickets resolved in released versions

Fixed

Assignee

Strong Liu

Reporter

Michael Nascimento

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure