2013-02-16 101 views
0

我试图做一个一对多映射使用休眠插入一些信息到数据库但每次数据更新表中而不是插入新行保存在一对多映射休眠更新而不是插入

我有2个实体:ReportMaster和Reportdetail。有许多Reportdetail数据包含该外键映射到报告主主键列ID

@Entity 
@Table(name = "ReportMaster") 
public class ReportMaster implements Serializable { 

private Integer repId; 
private Set<ReportDetail> reportDetails = new HashSet<ReportDetail>(); 
@Id 
@GeneratedValue 
@Column(name = "RepId", unique = true, nullable = false) 
public Integer getRepId() { 
    return this.repId; 
} 
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "reportMaster") 
public Set<ReportDetail> getReportDetails() { 
    return this.reportDetails; 
} 

第二个实体的ID是:

@Entity 
@Table(name = "ReportDetail") 
public class ReportDetail implements Serializable { 
private String repColumn; 
private String colData; 
//.......corresponding getters and setters 
private ReportMaster reportMaster; 
@ManyToOne(targetEntity = ReportMaster.class) 
@JoinColumn(name = "RepId") 
public ReportMaster getReportMaster() { 
    return this.reportMaster; 
} 

我想插入新行到reportdetails表,但它得到了更新:

ReportMaster report=new ReportMaster(req.getReportName(), req.getCid(), req.getReportDesc(), new Date()); 
report.addDetail(new ReportDetail(repcol,desc); 
getTemplate().save(obj); 

产生的HQL是:

org.hibernate.SQL - insert into ReportMaster (CreateDate, CustomerID, RepDesc, ReportName) values (?, ?, ?, ?) 
     org.hibernate.SQL - update ReportDetail set RepId=? where ColData=? and RepColumn=? 

     2013-02-16 10:13:34,109[http-6060-1] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session 
    org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1 
     at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:71) 
     at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46) 
     at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:24) 
     at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2403) 
     at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2307) 
     at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2607) 
     at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92) 
     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250) 
     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234) 
     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142) 
     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) 
     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 
     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 
+0

我很惊讶这项工作,你没有为你的报告细节实体定义一个I列。 – Perception 2013-02-16 05:00:17

回答

0

您应该设置ReportDetail的财产 'reportMaster' 是这样的:

ReportMaster report=new ReportMaster(req.getReportName(), req.getCid(),req.getReportDesc(), new Date()); 

// create detail and set its master 
ReportDetail detail = new ReportDetail(repcol,desc) 
detail.setReportMaster(report); 
report.addDetail(detail); 
getTemplate().save(obj); 

按照this document

希望这会有所帮助。