【问题标题】:Hibernate jpa does honour AttributeConveterHibernate jpa 确实尊重 AttributeConveter
【发布时间】:2015-02-22 10:26:51
【问题描述】:

我已经定义了一个这样的属性转换器:

@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;
    }

}

然后在我的实体类中:

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

根据 jpa2.1 规范,如果我在转换器上指定了 autoApply,我不必使用 @Convert 注释属性字段。 尽管如此,即使我没有在转换器上指定自动应用并指定以下内容:

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

Hibernate 仍然不考虑这个转换器。

我做错了什么? 我已删除该表并重新生成它,但没有任何帮助。

我尝试过 4.3.4 - 4.3.8 的休眠版本,但没有成功,n wildfly 8.1

顺便说一句,我的转换器在 entity-jar 中声明,然后作为依赖项包含在 ejb-jar 中。

【问题讨论】:

    标签: java hibernate jpa ejb-3.1


    【解决方案1】:

    嗯。

    几个小时后,解决方案似乎很简单。 我的错误是我在 ejb-jars persistence.xml 中声明了类,而不是指定 jar-file 元素。因此,jpa hibernate 注释引擎不知道我的 entity-jar,并且无法扫描它以查找 Converter 注释

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 1970-01-01
      • 2015-09-13
      • 2012-04-06
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多