简体   繁体   English

Hibernate无法使用层叠全部保存子对象

[英]Hibernate unable to save child objects using cascade all

I have the following two classes. 我有以下两节课。 Class 'Type' has a an object of class 'Content'. “类型”类具有一个“内容”类的对象。 I want to save all referenced objects of class 'Type' by using the hibernate.save() method. 我想使用hibernate.save()方法保存“类型”类的所有引用对象。 I have specified the cascade type as ALL in class 'Content'. 我在“内容”类中将级联类型指定为ALL。

Following is the error i face: object references an unsaved transient instance - save the transient instance before flushing. 以下是我遇到的错误:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例。

Can someone please help me identify what the problem is? 有人可以帮我找出问题所在吗?

public class Type {
    @OneToOne
    @JoinColumn(name = "content_id")
    private Content content;
    }

    public class Content {
        @OneToOne(mappedBy = "content", cascade = CascadeType.ALL)
        private Type type;
    }

    public class Test {
        public void createType() {
            Type type = new Type();
            Content content = someMethodToGetContent();
            type.setContent(content);
            save(type); 
        }

        public void save(Object domainObj) {
            getEntityManager().persist(domainObj);
            getEntityManager().flush();
        }
    }

Stacktrace: 堆栈跟踪:

Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: .content -> .Content at org.hibernate.engine.CascadingAction$9.noCascade(CascadingAction.java:387) at org.hibernate.engine.Cascade.cascade(Cascade.java:172) at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:154) at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:145) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795) ... 27 more org.springframework.transaction.TransactionSystemException: Could not commit JPA tr 由以下原因导致:org.hibernate.TransientObjectException:对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例:.content-> .Content在org.hibernate.engine.CascadingAction $ 9.noCascade(CascadingAction.java:387)处。 org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:154)处的hibernate.engine.Cascade.cascade(Cascade.java:172)org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java: 145)在org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88)在org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)在org.hibernate.impl.SessionImpl.flush (SessionImpl.java:1216)at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)...还有27个org.springframework.transaction.TransactionSystemException:无法提交JPA tr ansaction; 无动于衷 nested exception is javax.persistence.RollbackException: Transacti on marked as rollbackOnly at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.in 嵌套的异常是javax.persistence.RollbackException:仅在org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526)上标记为rollback的事务761)在org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)在org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)在org.springframework.transaction.interceptor.Transaction org.org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)上的org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)上的.invokeWithinTransaction(TransactionAspectSupport.java:292)。 springframework.aop.framework.JdkDynamicAopProxy.in voke(JdkDynamicAopProxy.java:213) at com.sun.proxy.$Proxy29.persist(Unknown Source) Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73) at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517) ... 12 more com.sun.proxy。$ Proxy29.persist上的voke(JdkDynamicAopProxy.java:213)(未知源)原因:javax.persistence.RollbackException:事务标记为rollbackOnly在org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java :73)at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)...还有12个

Please change your createType() as below. 请如下更改您的createType()。

public void createType() {
Type type = new Type();
Content content = someMethodToGetContent();
content.setType();
type.setContent(content);
save(type); 
}

Hibernate unable to save child objects using cascade all Hibernate无法使用层叠全部保存子对象

The reason why this does not work has to do with the way you're trying to persit the entities. 之所以不起作用,与您尝试使用实体的方式有关。

  • Let us see what the following code is doing: 让我们看看下面的代码在做什么:

     Type type = new Type(); Content content = someMethodToGetContent(); type.setContent(content); save(type); 

You are telling the persistence provider you want to save type by calling save(type) and expecting content should also be saved to the database. 您通过调用save(type)告诉持久性提供程序要保存type ,并且期望content也应保存到数据库中。 But in the entity Type the OneToOne annotation is does not contain the PERSIST cascade option. 但是在实体TypeOneToOne注释不包含PERSIST级联选项。 So you have to tell the persistence provider to to pesist an instance of Content when an instance of Type is persisted by changing your OneToOne annotation as follows: 因此,当Type的实例被持久化时,您必须告诉持久化提供者持久化Content的实例,方法是更改OneToOne批注,如下所示:

@OneToOne(cascade=CascadeType.PERSIST)    // modified
@JoinColumn(name = "content_id")
private Content content;

And because you defined to entities to have bi-directional relationship you should wire them as follows correctly: 并且由于您已将实体定义为具有双向关系,因此您应按如下所示正确连接它们:

Type type = new Type();
Content content = someMethodToGetContent();
content.setType(type);                       // modified
type.setContent(content);
save(type); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM