本文介绍了冬眠@OneToMany关系的更新,而不是插入保存期间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我想出解决办法。我试过这么多的组合,但似乎没有任何工作。我试图使用注解来实现Hibernate映射,但我的父对象及其子女的保存过程中我注意到一个更新语句是被称为插入语句来代替。

我有一个彼此有一个一对多的关系的两个班。

这些都是类的映射:收据有一个一对多的集合

  @Entity
公共类收据实现Serializable {    @ID
    @GeneratedValue(策略= GenerationType.AUTO)
    私人长期身份证;
    @OneToMany
    @JoinColumn(NAME =ReceiptId)
    私人列表<收集和GT;收藏;    // setter方法​​,getter方法
}
@实体
公共类集合实现Serializable {    @ID
    @GeneratedValue(策略= GenerationType.AUTO)
    私人长期身份证;
    @ManyToOne
    @JoinColumn(NAME =ReceiptId,插入=假,可更新= FALSE)
    私人收据收据;    // setter方法​​getter方法
}

问题是保存收据等过程中:

 收据R =新收据();
清单<收集和GT; COLS =新的ArrayList<收集和GT;();
cols.add(新集());
r.setCollections(COLS);
。getHibernateTemplate()保存(R);

它会产生此错误:

 休眠:(??????,,,,,)插入收据(用户ID,dateCreated会,付款人,receiptDate,receiptNumber,总数)值
休眠:更新收集组ReceiptId =?其中id =?
二零一零年十一月十五日下午八时46分00秒org.hibernate.jdbc.BatchingBatcher doExecuteBatch
重度:异常执行批处理:
org.hibernate.StaleStateException:批量更新更新从[0]返回了意外的行计数;实际行计数:0;预期:1
    在org.hibernate.jdbc.Expectations $ BasicExpectation.checkBatched(Expectations.java:85)
    在org.hibernate.jdbc.Expectations $ BasicExpectation.verifyOutcome(Expectations.java:70)
    在org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
    在org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    在org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
    在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
    在org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    在org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    在org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
    在org.springframework.orm.hibernate3.HibernateTemplate $ 28.doInHibernate(HibernateTemplate.java:883)
    在org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    在org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    在org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
    在com.coa.acctreports.daoImp​​.AccountingReportsImpl.update(AccountingReportsImpl.java:52)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
    在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    在java.lang.reflect.Method.invoke(Method.java:597)
    在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)


解决方案

在的情况下,双向多对1个/ 1一对多的关系,多方应拥有方(和 @JoinColumn 的拥有方)指定的,而一方应该有的mappedBy poiniting到拥有方。在你的情况,你还需要启用保存操作的级联:

  @Entity
公共类收据实现Serializable {
    @OneToMany(的cascade = CascadeType.ALL,的mappedBy =回执)
    私人列表<收集和GT;收藏;
    ...
}@实体
公共类集合实现Serializable {
    @ManyToOne
    @JoinColumn(NAME =ReceiptId)
    私人收据收据;
    ...
}

参见:



Please help me figure this out. I've tried so many combinations but nothing seems to work. I'm trying to implement hibernate mapping using annotations but during the saving of my parent object and its children i noticed that an update statement is being called instead of an insert statement.

I have two classes which have a one-to-many relationship with each other.

These are the class' mappings: Receipt has one-to-many Collections

@Entity
public class Receipt implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @OneToMany
    @JoinColumn(name="ReceiptId")
    private List<Collection> collections;

    //setters, getters
}


@Entity
public class Collection implements Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    @ManyToOne
    @JoinColumn(name="ReceiptId", insertable=false, updatable=false)
    private Receipt receipt;

    //setters getters
}

The problem is during saving a receipt like:

Receipt r = new Receipt();
List<Collection> cols = new ArrayList<Collection>();
cols.add(new Collection());
r.setCollections(cols);
getHibernateTemplate().save(r);

It generates this error:

Hibernate: insert into Receipt (userId, dateCreated, payor, receiptDate, receiptNumber, total) values (?, ?, ?, ?, ?, ?)
Hibernate: update Collection set ReceiptId=? where id=?
Nov 15, 2010 8:46:00 PM org.hibernate.jdbc.BatchingBatcher doExecuteBatch
SEVERE: Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
    at org.springframework.orm.hibernate3.HibernateTemplate$28.doInHibernate(HibernateTemplate.java:883)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)
    at com.coa.acctreports.daoImp.AccountingReportsImpl.update(AccountingReportsImpl.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
解决方案

In the case of bidirectional many-to-one/one-to-many relationship, "many" side should be the owning side (and @JoinColumn is specified at the owning side), whereas "one" side should have mappedBy poiniting to the owning side. In your case you also need to enable cascading of the save operation:

@Entity
public class Receipt implements Serializable {
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "receipt")
    private List<Collection> collections;
    ...
}

@Entity
public class Collection implements Serializable {
    @ManyToOne
    @JoinColumn(name="ReceiptId")
    private Receipt receipt;
    ...
}

See also:

这篇关于冬眠@OneToMany关系的更新,而不是插入保存期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 04:01