Search code examples
hibernatehibernate-envers

Property of Composite Key Not Found


I have the following entities:

@Entity @Table @Audited
public class MyParent {

    @Id
    @Column
    Integer id;
    @OneToMany(mappedBy = "id.myParent", orphanRemoval = true)
    @Cascade({ CascadeType.ALL })
    List<Child> children;
}

@Entity @Table @Audited
public class Child {

    @EmbeddedId
    ChildId id = new ChildId();
}

@Embeddable
public class ChildId implements Serializable {

    @MapsId("my_parent")
    @JoinColumn(name = "my_parent_id") 
    @ManyToOne
    MyParent myParent;
    @Column
    String name;
}

Before I added the annotation @Audited everything worked fine. Afterwards everything failed because Hibernate 5.1 did not like composite keys (see HHH-7625).

It took forever, but I managed to update to 5.2 just now, and now everything works... except for auditing. I get the following exception:

org.hibernate.QueryException: could not resolve property: myParent_id of: org.acme.project.Child_AUD [select e__ from org.acme.project.Child_AUD e__ where e__.myParent_id = :myParent_id and e__.originalId.REV.id = (<removed internal query>)]

I used this code:

AuditReader auditReader = AuditReaderFactory.get(this.em);
MyParent parent = auditReader .find(MyParent.class, id, revision);
parent.getChildren(); // exception is here

So where does Envers get myParent_id from? I'm not sure. I tried to rename the column my_parent_id to that ID, but it did nothing.

And I tried to replace ChildId.myParent with an ID, but that does not help either.

This paragraph in the documentation looks eerily similar, but I don't know what I should do based on what it says.

Am I missing something? Or is this yet another bug? How do I get the code to work?


Solution

  • After further review, this is a known problem reported as HHH-11770 which has since been fixed and is included in the 5.2.11.Final Hibernate Envers release.

    The underlying problem is that OneAuditedEntityQueryGenerator only had support for when the reference pointed to an @IdClass but did not support the use case for @EmbeddedId.

    You can either use the latest snapshot for hibernate-envers if you cannot wait for the official release of 5.2.11.Final or you can integrate the code change yourself using this commit.

    If you do decide to use the latest snapshot, I do advise you apply the snapshot for ORM too.