Hibernate Auditable Many-to-Many Relationships

Consider the following hibernate configuration:

<class name="Person">
<id name="id" column="personId">
    <generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
    <key column="personId"/>
    <many-to-many column="addressId"
        class="Address"/>
</set>
</class>

<class name="Address">
<id name="id" column="addressId">
    <generator class="native"/>
</id>
<set name="people" inverse="true" table="PersonAddress">
    <key column="addressId"/>
    <many-to-many column="personId"
        class="Person"/>
</set>
</class>

      

Now these two classes (Person and Address) have an Auditable interface. In our Hibernate AuditInterceptor class (implements Interceptor), we perform some audit function if the object is an Auditable instance. In short, all this code works great ...

However, there is this PersonAddress table that is not validated because we do not technically have a POJO for it ... so we cannot validate any "instance". Is there a way in the hibernate configuration that we can tell to intercept this PersonAddress table? Perhaps we need to handle one of the other actions from the Interceptor (for example, onCollectionUpdate). I'm not sure...

The only other way I can get it to do an Audit is to convert the one-to-many relationship from Person-PersonAddress, one-to-many from address to PersonAddress, and create another class for PersonAddress that does the mapping. " many-to-one returns to Person and Address.

It looks like extra work and I really want to avoid extra work.

Can anyone suggest a better solution?

Thank!

+1


source to share


2 answers


I couldn't figure it out with many-to-many mappings, so I converted this mapping to one-to-many and created a bean model for PersonAddress with the corresponding hibernate mappings.



Thank!

0


source


How do you add addresses to a person and persons to an address? Since you don't have a domain object for PersonAddress, then editing these relationships must happen to one of Person or Address, no?

If so, then the audit occurs as part of the Person- or Address audit.

For example, in Person:



class Person {
    private List<Address> addedAddresses;
    private Set<Address> addresses;
    .
    .
    .
    public void addAddress(Address toAdd) {
        addedAddresses.add(toAdd);
        addresses.add(toAdd);
    }
}

      

Then, when auditing Person, write down the entries written in addAddresses if they are not empty.

Of course this is pretty straightforward (what to do with deletion? With permissions on addresses?), But not needed anymore in your example.

0


source







All Articles