0

休止状態を使用して1対多のマッピングを実行してDBに情報を挿入しようとしていますが、新しい行を挿入するのではなく、テーブルでデータが更新されるたびに

ReportMaster と Reportdetail の 2 つのエンティティがあります。多くの 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;
}

2 番目のエンティティは次のとおりです。

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

1 に答える 1

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

このドキュメントに従ってください。

お役に立てれば。

于 2013-02-16T05:51:46.803 に答える