Hibernate Envers fails with @Converter and AttributeConverter (JPA 2.1)

穿精又带淫゛_ 提交于 2019-11-29 04:38:29

Try using @Convert in Party Entity. Sometimes autoApply flag will not work

@Entity
@Audited
public class Party
    {
    @Convert(converter = NametoStringConverter.class)
    protected Name name;
    ...
    }

This seems to be a known problem with Hibernate Envers (HHH-9042).

A simple workaround would be a manual invocation of the Convter and an additional transient field, like this:

@Entity
public class Party {

  protected Name name;

  @Transient
  protected String nameString;

  //...

  public void setName(Name name) {
    this.nameString = (new NametoStringConverter()).convertToDatabaseColumn(name);
    this.name = name;
  }

  //...

  public void setNameString(String nameString) {
    this.name = (new NametoStringConverter()).convertToEntityAttribute(nameString);
    this.nameString = nameString;
  }

}

Depending on the conversion functions, the code could be further simplified by making them staticand importing them.

I see the text "GeneratedConstructorAccessor43" in the exception. May be you need a public void constructor so that JPA can create an instance of NametoStringConverter.

The default constructor should work but check if you have another one with receives parameters or not public.

I had the same problem, what I found out that it only occured when I didn't put the @Column annotation with columnDefinition = "VARCHAR(255)" to the Enum. So I think that is a better work around than what in hibernate tracker has.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!