【问题标题】:Duplicate Insert Issue with Hibernate OneToMany relationship in WildflyWildfly 中 Hibernate OneToMany 关系的重复插入问题
【发布时间】:2015-03-05 07:28:25
【问题描述】:

我在时间表和活动之间建立了 OneToMany 关系。当我保存时间表时,它会插入两次新的活动。

这是映射 -

@JoinColumn(name = "timesheetId")
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Where(clause = "active = 1")
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<Activity> activities;

在活动实体中 -

@ManyToOne
@JoinColumn
private TimeSheet timesheet;

以下是 hibernate 的 Merge 方法的日志-它在单个活动中调用了两次插入语句。

我正在使用 Wildfly 8 和 Hibernate 4.3.5。我怀疑不是因为实体经理吗?任何帮助将不胜感激。

18:11:38,455 DEBUG [org.jboss.as.jpa] (default task-28) default task-28:[transaction scoped EntityManager]: 重用实体管理器会话已经在 tx TransactionImple

18:11:38,460 INFO [stdout](默认任务 28)休眠:选择 activityty0_.id 作为 id1_1_0_,activityty0_.active 作为 active2_1_0_,activityty0_.createdById 作为 created17_1_0_,activityty0_.createdDate 作为 createdD3_1_0_,activityty0_.description 作为 descript4_1_0_ , activityty0_.modifiedById as modifie18_1_0_, activityty0_.modifiedDate as modified5_1_0_, activityty0_.name as name6_1_0_, activityty0_.allocationGroupCd as allocat19_1_0_, activityty0_.allocationTypeCd as allocat20_1_0_, activityty0_.billable as billable7_1_0_, activityty0_.commentRequiredIndicator as commentR8_1_0_, activityty0_.dayDropable as dayDropa9_1_0_, activityty0_ .futureDropable as futureD10_1_0_, activityty0_.headDropable as headDro11_1_0_, activityty0_.holidayDropable as holiday12_1_0_, activityty0_.maximumRequiredTime as maximum13_1_0_, activityty0_.minimumRequiredTime as minimum14_1_0_, activityty0_.standardCode as standar15_1_0_, activityty0_.weekendDropable as weekend16_1_0_ from ActivityType activityty0_ wher e activityty0_.id=?

18:11:38,468 INFO [stdout](默认任务 28)休眠:选择 allocation0_.id 作为 id1_2_0_,allocation0_.active 作为 active2_2_0_,allocation0_.createdById 作为 createdB8_2_0_,allocation0_.createdDate 作为 createdD3_2_0_,allocation0_.description 作为 descript4_2_0_ , allocation0_.modifiedById as modified9_2_0_, allocation0_.modifiedDate as modified5_2_0_, allocation0_.name as name6_2_0_, allocation0_.standardCode as standard7_2_0_ from AllocationGroup allocation0_ where allocation0_.id=?

18:11:38,473 INFO [stdout](默认任务 28)休眠:选择 allocation0_.id 作为 id1_3_0_,allocation0_.active 作为 active2_3_0_,allocation0_.createdById 作为 createdB8_3_0_,allocation0_.createdDate 作为 createdD3_3_0_,allocation0_.description 作为 descript4_3_0_ , allocation0_.modifiedById as modified9_3_0_, allocation0_.modifiedDate as modified5_3_0_, allocation0_.name as name6_3_0_, allocation0_.standardCode as standard7_3_0_ from AllocationType allocation0_ where allocation0_.id=?

18:11:38,480 INFO [stdout](默认任务 28)休眠:插入 Activity(活动、createdById、createdDate、描述、modifiedById、modifiedDate、name、activityTypeId、actual、applicationId、assignmentId、businessAreaId , comment, endDate 等, startDate, timesheetId) 值 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

18:11:38,493 DEBUG [org.jboss.as.jpa](默认任务 28)为单元名称 =******.ear/*******-ejb- 创建了新的 TransactionScopedEntityManager 0.0.1-SNAPSHOT.jar#*******EM 18:11:38,495 DEBUG [org.jboss.as.jpa](默认任务 28)默认任务 28:[事务范围 EntityManager]:重用实体管理器会话已经在 tx TransactionImple 18:11:38,497调试[org.jboss.as.jpa](默认任务28)默认任务28:[事务范围的EntityManager]:重用实体管理器会话已经在tx TransactionImple 18:11:38,498 DEBUG [org.jboss.as.jpa](默认任务 28)默认任务 28:[事务范围 EntityManager]:重用实体管理器会话已经在 tx TransactionImple

18:11:38,600 INFO [stdout] (default task-28) Hibernate: 插入到 Activity (active, createdById, createdDate, description, modifiedById, modifiedDate, name, activityTypeId, actual, applicationId, assignmentId, businessAreaId , comment, endDate 等, startDate, timesheetId) 值 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

【问题讨论】:

  • 请展示您用于组装数据结构并将其添加到持久性上下文的代码。请注意,对于这些关系,通常使用 Set 而不是 List 更安全。还要确保您的 equals/hashCode 实现是正确的(并且基于业务键而不是主键)

标签: transactions duplicates nhibernate-mapping record wildfly


【解决方案1】:

在 Wildfly 8 或 Hibernate 3.6 及更高版本中,如果您遇到此问题,请制定一个拇指规则,在合并之前执行刷新。

em.flush(); o = em.merge(o);

这就是我能够解决此问题的方法。

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2013-07-06
    • 2012-10-26
    • 2010-11-15
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多