Hibernate envers, post-delete event throws a constraint violation exception

The project uses Hibernate 3.5, Spring Webflow 2, and Hibernate Envers for auditing. Envers is configured in the hibernate.cfg.xml file. I have a one-to-many relationship mapping between an ArticleGroup and Article entity. In the "articles" of the table there is a foreign key "article_group_id" link to the identifier in the table "article_groups". In the interface, when I delete an article, Hibernate Envers throws a constraint exception for the post-delete event. If I do not use Envers, remote operations work fine. These two objects are defined as follows:

@Entity @Table(name="article_groups") @Audited public class ArticleGroup implements Serializable { @OneToMany(mappedBy="articleGroup", fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OrderBy("id") private List<Article> articles; // other fields, getters and setters } @Entity @Table(name="articles") @Audited public class Article implements Serializable { @ManyToOne @JoinColumn(name = "article_group_id") private ArticleGroup articleGroup; // other fields, getters, setters } 

Deleting an article is encoded as follows:

  @Service("articleManager") public class ArticleManagerImpl implements ArticleManager { // inject dao @SuppressWarnings("unchecked") @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public void deleteArticle(Article article, Object articles) { articleDao.delete(article); ((List<Article>) ((OneSelectionTrackingListDataModel)articles).getWrappedData()).remove(article); } } 

Exclusion of violation of restrictions:

 Hibernate: delete from live.articles where id=? Hibernate: select nextval ('hibernate_sequence') Hibernate: insert into audit.REVINFO (REVTSTMP, REV) values (?, ?) Hibernate: insert into audit.articles_AUD (REVTYPE, content, language, name, order_number, title, article_group_id, id, REV) values (?, ?, ?, ?, ?, ?, ?, ?, ?) 82828 [http-bio-8080-exec-2] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager - Initiating transaction rollback after commit exception org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update ... Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into audit.articles_AUD (REVTYPE, content, language, name, order_number, title, article_group_id, id, REV) values ('2', NULL, NULL, NULL, NULL, NULL, NULL, '14', '17') was aborted. Call getNextException to see the cause. at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2619) 

As you can see, when Envers inserts 'audit.articles_AUD' into the table, article_group_id is null, which caused a constraint violation. Does anyone know how to fix this? Thank you very much.

+4
source share
2 answers

Got it. Set this property in the configuration file:

 <prop key="org.hibernate.envers.store_data_at_delete">true</prop> 

Link: http://docs.jboss.org/hibernate/envers/3.6/reference/en-US/html/configuration.html Table 3.1. Includes org.hibernate.envers.store_data_at_delete configuration properties

+6
source

If you are post-deleting and articles_AUD has any restrictions on articles or any other post that you delete during this operation, you should try adding a post to articles_AUD with a link that no longer exists.

As a rule, we keep links from audit tables to improve the history of server revisions. If audit tables contain restrictions on non-audit tables, then the change history could potentially be broken if any record is deleted from the table being checked.


Regarding your solution working when setting up org.hibernate.envers.store_data_at_delete .

If the entity data is stored in the revision when the object ( instead of saving only the identifier and all other properties as zero ). This is usually not required because the data is present in the latest version. Sometimes, however, it is easier and more to access it in the last revision (then the data that the object contained before the deletion is stored twice).

This tells me that there are columns in your audit table that are NOT NULL , therefore a violation of the constraints, since Envers is trying to insert null . You can enable null in your audit tables.

+3
source

All Articles