2010-11-15 83 views
0

請幫我弄清楚這一點。我嘗試了這麼多的組合,但似乎沒有任何工作。我試圖使用註釋來實現hibernate映射,但在保存父對象及其子對象時,我注意到正在調用update語句而不是insert語句。冬眠@onetomany關係更新,而不是在保存期間插入

我有兩個類彼此具有一對多的關係。

這些都是類的映射:收據都有一個一對多的集合

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

的問題是保存收據等過程中:

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

它生成此錯誤:

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) 
+0

= CascadeType.ALL)但沒有任何工作 – mileesah 2010-11-15 13:53:15

回答

3

在雙向多對一/一對多關係的情況下,「多」一方應該是自己的(並且@JoinColumn被指定在擁有方),而「一方」應該具有指向擁有方的mappedBy。在你的情況,你還需要啓用保存操作的級聯:

@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; 
    ... 
} 

參見:順便我也嘗試添加@OneToMany(級聯

+0

謝謝,但現在我有另一個錯誤(它會發生,當它保存集合):org.springframework.orm.hibernate3.HibernateSystemException:與山姆不同的對象e標識符值已經與會話相關聯 – mileesah 2010-11-16 01:30:24