我在Timesheet和活动之间拥有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方法的日志 - 它调用insert语句两次单个活动。
我正在使用Wildfly 8和Hibernate 4.3.5。如果不是因为实体经理,我有疑问吗?任何帮助将不胜感激。
18:11:38,455 DEBUG [org.jboss.as.jpa](默认任务-28)默认任务-28:[事务范围EntityManager]:已在tx TransactionImple&lt;中重用实体管理器会话ac,BasicAction:0:ffff0a05a293:604cc77a:54f72b23:213状态:ActionStatus.RUNNING&gt;
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如modifie18_1_0_,activityty0_.modifiedDate如modified5_1_0_,activityty0_.name如name6_1_0_,activityty0_.allocationGroupCd如allocat19_1_0_,activityty0_.allocationTypeCd如allocat20_1_0_,activityty0_.billable如billable7_1_0_,activityty0_.commentRequiredIndicator如commentR8_1_0_,activityty0_.dayDropable如dayDropa9_1_0_,activityty0_ .futureDropable为futureD10_1_0_,activityty0_.headDropable为headDro11_1_0_,activityty0_.holidayDropable为holiday12_1_0_,activityty0_.maximumRequiredTime为maximum13_1_0_,activityty0_.minimumRequiredTime为minimum14_1_0_,activityty0_.standardCode为standar15_1_0_,activityty0_.weekendDropable为weekend16_1_0_ from ActivityType activityty0_ wher e activityty0_.id =?
18:11:38,468 INFO [stdout](默认任务-28)Hibernate:选择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)Hibernate:选择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)Hibernate:插入Activity(active,createdById,createdDate,description,modifiedById,modifiedDate,name,activityTypeId,actual,applicationId,assignmentId,businessAreaId ,注释,endDate等,startDate,timesheetId)值(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
18:11:38,493 DEBUG [org.jboss.as.jpa](默认任务-28)为单位名称= ******创建了新的TransactionScopedEntityManager。耳/ ******* - ejb- 0.0.1-SNAPSHOT.jar#******* EM 18:11:38,495 DEBUG [org.jboss.as.jpa](默认任务-28)默认任务-28:[事务范围EntityManager]:已在tx TransactionImple&lt;中重用实体管理器会话ac,BasicAction:0:ffff0a05a293:604cc77a:54f72b23:213 status:ActionStatus.RUNNING&gt; 18:11:38,497 DEBUG [org.jboss.as.jpa](默认任务-28)默认任务-28:[事务范围EntityManager]:已在tx TransactionImple&lt;中重用实体管理器会话ac,BasicAction:0:ffff0a05a293:604cc77a:54f72b23:213 status:ActionStatus.RUNNING&gt; 18:11:38,498 DEBUG [org.jboss.as.jpa](默认任务-28)默认任务-28:[事务范围EntityManager]:已在tx TransactionImple&lt;中重用实体管理器会话ac,BasicAction:0:ffff0a05a293:604cc77a:54f72b23:213状态:ActionStatus.RUNNING&gt;
18:11:38,600 INFO [stdout](默认任务-28)Hibernate:插入Activity(active,createdById,createdDate,description,modifiedById,modifiedDate,name,activityTypeId,actual,applicationId,assignmentId,businessAreaId ,注释,endDate等,startDate,timesheetId)值(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
答案 0 :(得分:0)
在Wildfly 8或Hibernate 3.6及更高版本中,如果您遇到此问题,请在合并前制作一个拇指规则以进行刷新。
em.flush(); o = em.merge(o);
我是如何解决这个问题的。