When using the following mappings: {code} @Entity @Audited public class Customer { @Id private Integer id;
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) @OneToMany(fetch = FetchType.LAZY) @JoinColumn(name = "customerId", foreignKey = @ForeignKey(name = "FK_CUSTOMER_ADDRESS") private List<CustomerAddress> addresses; }
@Entity @Audited public class Address { @Id private Integer id; }
@Embeddable public class CustomerAddressId implements Serializable { @ManyToOne private Address address; @ManyToOne private Customer customer; }
@Entity @Audited public class CustomerAddress { @EmbeddedId private CustomerAddressId id; } {code} If a user attempts to remove the associated {{CustomerAddress}} and {{Address}} rows from the database, a constraint violation is thrown.
The XML mapping for {{CustomerAddress}} looks like: {noformat} <hibernate-mapping auto-import="false"> <class entity-name="org.hibernate.envers.test.integration.QuickTest$CustomerAddress_AUD" discriminator-value="CustomerAddress" table="CustomerAddress_AUD" abstract="false"> <composite-id name="originalId"> <key-many-to-one name="address" class="org.hibernate.envers.test.integration.QuickTest$Address" foreign-key="none"> <column name="address_id" length="255" scale="2" precision="19"/> </key-many-to-one> <key-many-to-one name="customer" class="org.hibernate.envers.test.integration.QuickTest$Customer" foreign-key="none"> <column name="customer_id" length="255" scale="2" precision="19"/> </key-many-to-one> <key-many-to-one type="integer" class="org.hibernate.envers.enhanced.SequenceIdRevisionEntity" name="REV"> <column name="REV"/> </key-many-to-one> </composite-id> <property insert="true" update="false" name="REVTYPE" type="org.hibernate.envers.internal.entities.RevisionTypeType"/> </class> </hibernate-mapping> {noformat} As is evident, the {{foreign-key="none"}} is specified for the relationship between {{CustomerAddress}}'s audit table and the {{Address}} and {{Customer}} entities. But despite specifying that, the DDL generates a foreign key anyway: {noformat} 19:50:16,837 DEBUG SQL:92 - alter table QuickTest$Customer_QuickTest$CustomerAddress_AUD add constraint FKh42k0wh7fugg1s0xj3uehab86 foreign key (REV) references REVINFO 19:50:16,839 DEBUG SQL:92 - alter table QuickTest$Customer_QuickTest$CustomerAddress_AUD add constraint FKdc6u4b3e7qgvioi57s8dda1sq foreign key (address_id) references Address 19:50:16,841 DEBUG SQL:92 - alter table QuickTest$Customer_QuickTest$CustomerAddress_AUD add constraint FKr1fk4pgyeaslfw8q9d1ufe890 foreign key (customer_id) references Customer {noformat} Taken from stackoverflow: http://stackoverflow.com/questions/27629230/envers-audit-table-creates-a-foreign-key-to-and-prevents-deletion-from-another-t |
|