gpt4 book ai didi

Hibernate envers,删除后事件抛出约束违反异常

转载 作者:行者123 更新时间:2023-12-02 23:45:18 27 4
gpt4 key购买 nike

项目使用 Hibernate 3.5、Spring Webflow 2 和 Hibernate Envers 进行审计。 Envers 在 hibernate.cfg.xml 中配置。我在实体“ArticleGroup”和“Article”之间有一对多关系映射。表“articles”有一个外键“article_group_id”引用表“article_groups”中的id。在前端,当我删除一篇文章时,Hibernate Envers 会针对删除后事件抛出约束冲突异常。如果我不使用 Envers,删除操作可以正常工作。两个实体定义如下

    @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
}

文章删除代码如下:

    @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);
}
}

违反约束异常:

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)

正如您所看到的,当 Envers 插入表“audit.articles_AUD”时,article_group_id 为 null,这导致了约束违规。有人知道如何修复它吗?非常感谢。

最佳答案

明白了。在配置文件中设置此属性:

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

引用: http://docs.jboss.org/hibernate/envers/3.6/reference/en-US/html/configuration.html表 3.1。恩弗斯配置属性org.hibernate.envers.store_data_at_delete

关于Hibernate envers,删除后事件抛出约束违反异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11953879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com