问题
When auditing a foreign key, Envers seems to be ignoring the JoinColumn annotation.
E.g. I have a simple class like this:
@Audited
@Entity
public class Address {
@Id
@GeneratedValue
private int id;
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@ManyToOne
@JoinColumn (name="addressTypeFk", referencedColumnName="EntityId",nullable=false)
private AddressTypeLookup addressType;
Which references a lookup table like this:
@Entity
public class AddressTypeLookup
{
@Id
@GeneratedValue
private int id;
private String descr;
private int entityId;
Notice that the addressType attribute on Address is joining to the entityId column, not the primary key "id" column. Also notice the use of RelationTargetAuditMode.NOT_AUDITED : I want to audit the foreign key, but I don't want to Audit changes to AddressTypeLookup. The problem is, when Envers records changes to the foreign key, it's recording the AddressTypeLookup primary key value "id", not the entityId. How do I make Envers record the entityId values used?
[In case you're wondering why I want this: we have a database of lookup data (aka master / reference data). It records history: all rows have effective from/to dates. The id column is properly unique; the entityId identifies a particular thing - e.g. all versions of a particular address type. All the lookup tables are replicated into an application database - but only the current data is replicated, not the historic data. ]
回答1:
This may be a bug with the intersection of using referencedColumnName and RTAM.NOT_AUDITED. Please file a bug report @ https://hibernate.onjira.com/, envers component.
来源:https://stackoverflow.com/questions/15340382/envers-for-a-manytoone-with-joincolumn-is-auditing-the-wrong-column