【问题标题】:Save in one to many mapping in hibernate in updating instead of inserting在更新而不是插入时保存在休眠中的一对多映射
【发布时间】:2013-02-16 04:50:09
【问题描述】:

我正在尝试使用休眠进行一对多映射以将一些信息插入数据库,但每次数据在表中更新而不是插入新行时

我有 2 个实体:ReportMaster 和 Reportdetail。其中许多 Reportdetail 数据包含的 ID 是映射到报告主主键列 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;
}

第二个实体是:

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

【问题讨论】:

  • 我很惊讶这完全有效,您没有为您的报告详细信息实体定义 Id 列。

标签: java hibernate


【解决方案1】:

您应该像这样设置 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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 2017-09-06
    • 2017-02-12
    • 2011-12-10
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2011-04-07
    相关资源
    最近更新 更多