tags:

views:

389

answers:

2

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 classs (Person and Address) have an Auditable interface. In our Hibernate AuditInterceptor class (implements Interceptor), we perform some auditing function if the entity is an instanceof Auditable. In short, all of this code works great...

However, there's this PersonAddress table that doesn't get audited because we don't technically have a POJO for it...so we can't check for some "instanceof". Is there any way in the hibernate configuration that we can tell it to Intercept this PersonAddress table? Maybe we need to handle one of the other actions from the Interceptor (like onCollectionUpdate). I'm not sure...

The only other way I can think to force it to Audit is to convert the relationship to a one-to-many from Person to PersonAddress, one-to-many from Address to PersonAddress and create another class element for PersonAddress that does the many-to-one mappings back to Person and Address.

That seems kind of like extra work and I really want to avoid extra work.

Can somebody suggest a better solution?

Thanks!

A: 

How are you adding addresses to a person, and persons to an address? Since you do not have a domain object for PersonAddress, then the edits to those relations must be happening one of Person or Address, no?

If so, then have the auditing occur as part of the Person- or Address-auditing.

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, record the entries recorded to addedAddresses if it's not empty.

Of course, this is quite simple (what to do with deletes? with edits to addresses?) but don't have more to go on in your example.

eqbridges
We do in fact add the data to both Person and Address objects. We found that Hibernate wouldn't otherwise update the PersonAddress table.We're using a modified version of the AuditLogInterceptor found in Manning's Hibernate In Action (pg. 343). The general idea remains the same, which means we don't have specialty logic for checking our POJOs. If the supplied entity is of type Auditable, we add it to the set of updates, inserts, or deletes.We could drop in some logic for these two specific classes, however, we really don't know if the relationship had changed in some way...
T Reddy
A: 

I couldn't figure it out via many-to-many mappings, so I converted that mapping into a one-to-many and created a model bean for PersonAddress with appropriate hibernate mappings.

Thanks!

T Reddy