简体   繁体   中英

Hibernate jpa does honour AttributeConveter

I have defined an attribute converter like this:

@Converter(autoApply = true)
public class MyConverter implements AttributeConverter<MyType, String> {

    @Override
    @Nullable
    public String convertToDatabaseColumn(@Nullable final MyType attribute) {
        LOG.log(Level.INFO, "Converting '{'{0}'}' to DB column", attribute);

        return attribute != null ? map(attribute) : null;
    }

    @Override
    @Nullable
    public MyType convertToEntityAttribute(@Nullable final String dbData) {
        LOG.log(Level.INFO, "Converting '{'{0}'}' to type", dbData);

        return dbData != null ? map(dbData) : null;
    }

}

Then in my entity class:

@Entity
public class MyEntity implements Serializable {
  @Id
  private Long id;
  private MyType myData;
}

According to jpa2.1 specs, i do not have to annotate the attribute field with @Convert if i have specified autoApply on the converter. Nonetheless, even if i do not specify the autoApply on the converter and specify the following:

@Entity
public class MyEntity implements Serializable {
  @Id
  private Long id;
  @Convert(converter = MyConverter.class)
  private MyType myData;
}

Hibernate still does not consider this converter.

What could i be doing wrong? I have deleted the table and regenerated it, but nothing does help.

I have tried hibernate versions from 4.3.4 - 4.3.8 with no success, n wildfly 8.1

As aside note, My converter is declared in an entity-jar, which is then included in ejb-jar as a dependency.

Well.

After several hours, the solution seems to be simple. My mistake was that i declared classes in the ejb-jars persistence.xml, instead of specifying the jar-file element. Therefore, the jpa hibernate annotation engine had no idea of my entity-jar, and could not scan it for the Converter annotation

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