JadiraOrg / jadira

Jadira Framework
Apache License 2.0
74 stars 44 forks source link

PersistentEnum should return the mapped class as "returnedClass" #23

Closed alienisty closed 9 years ago

alienisty commented 10 years ago

The current implementation, as of the 2014-07-15, returns Enum.class as discovered by AbstractReflectionUserType using TypeHelper.getTypeArguments. That breaks some of hibernate use cases, namely calling a constructor that uses the enumeration from a query.

For example, imagine we had a class similar to:

class UsingEnum {
  @Column
  @Type(type = "org.jadira.usertype.corejava.PersistentEnum", parameters = {
  @Parameter(name = "enumClass", value = "specifically.MyEnum"),
  @Parameter(name = "identifierMethod", value = "getCode"),
  @Parameter(name = "valueOfMethod", value = "getEnum") })
  private MyEnum value;

  public UsingEnum(MyEnum value) {...}
}

If I now used a query similar to "select new UsingEnum(u.value) from UsingEnum u", that would not work because PersistentEnum would fail to return the specific enum type and hibernate could not match the constructor.

PeristentEnum should override returnedClass() method as follow:

 @Override
 @SuppressWarnings("unchecked")
 public Class<Enum<?>> returnedClass() {
    Class<?> mappedClass = getMappedClass();
    if (mappedClass == null) {
        throw new IllegalStateException("No mapped class was defined for " + this.getClass().getName());
    }
    return (Class<Enum<?>>) mappedClass;
 }

or similar, according to your coding style/standards.

chrisphe commented 9 years ago

About to push fix