Hibernate envers issues

How do I have to configure Hibernate Envers with #OneToMany + #JoinColumn?
It uses relation table, but sometimes, records are presented there, sometimes not.

When I fetch from another side of relation (#ManyToOne) - it works, object is presented.

@Entity
@Table(name = "a")
@Audited(targetAuditMode = NOT_AUDITED)
public class A {

    @OneToMany(orphanRemoval = true)
    @JoinColumn(name = "A_ID", referencedColumnName = "ID")
    @MapKeyColumn(name = "SomeEnum")
    @MapKeyEnumerated(EnumType.STRING)
    @Cascade(CascadeType.ALL)
    private Map<SomeEnum, AnotherEntity> anotherEntities = new HashMap<>(0);

As I found in error messages, envers wanted table like this:

CREATE TABLE audit.a_another_entity_aud
(
    revtype bigint,
    rev bigint,
    id bigint,
    another_entity_id bigint,
    some_enum character varying(255) 
)

And table for related entity also

CREATE TABLE audit.another_entity_aud
(
    revtype bigint,
    rev bigint,
    id bigint,
    a_id bigint,
    some_enum character varying(200)
)

Also, how can I audit #ElementCollection of enums? mapping like:

public class Role {
    #ElementCollection
    #CollectionTable(name = "role_permissions", joinColumns = #JoinColumn(name = "ROLE_ID"))
    #Column(name = "PERMISSION_NAME")
    #Enumerated(EnumType.STRING)
    private Set<Permission> permissions;
}

If I audit it like

CREATE TABLE audit.role_permissions_aud (
    revtype bigint,
    rev bigint,
    role_id bigint,
    permission_code character varying(255)
);

I do only get diff
image

But there’re actually another “permissions”

(at) is replaced with # hash, because can’t post with 2+ “user mentions” :slight_smile:

@Naros can you take a look on this question? Thanks.

1 Like

I can only speculate but I suspect it has to do entirely with how you’re modifying the association and you’ve setup both sides of the association. Could you please post both sides of your mappings exactly how you have them and the code where you’re altering the association but see no entries get generated?

I would highly recommend you not generate the Envers tables manually if that is what you’re attempting. There is a number of nuances that doing so you might overlook and can lead to a plethora of unexpected errors. I would recommend you either use the hibernate.hbm2ddl.auto property in a development environment to get an idea of what is needed or use the hibernate tooling schema export task to generate a SQL script with all the mappings.

Is it possible that other Permission was removed in an earlier revision or was added in a later revision?

Its important to remember that behind the scenes we execute a query to load the set with the Permission entities that are associated with that specific Role that were in that collection at Revision 47.

If the Permission you’re looking for was removed in an earlier revision, it won’t appear. If the Permission was added in a later revision, then it effectively is not in the collection at the time Revision 47 occurred.

Okay, i got it:) I can only update schema, it adds required tables.
Made map working by schema generating.

Can I somehow initialize audit records for existing entites?
To get correct diff, in case with permissions for example

You can, but that is presently outside the scope of Envers. You would need to write a database script that effectively performs inserts into the Envers schema with the right data in order to seed your audit tables.