java - Hibernate unable to save child objects using cascade all -


i have following 2 classes. class 'type' has object of class 'content'. want save referenced objects of class 'type' using hibernate.save() method. have specified cascade type in class 'content'.

following error face: object references unsaved transient instance - save transient instance before flushing.

can please me identify 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 unsaved transient instance - save transient instance before flushing: .content -> .content @ org.hibernate.engine.cascadingaction$9.nocascade(cascadingaction.java:387) @ org.hibernate.engine.cascade.cascade(cascade.java:172) @ org.hibernate.event.def.abstractflushingeventlistener.cascadeonflush(abstractflushingeventlistener.java:154) @ 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) @ org.hibernate.ejb.abstractentitymanagerimpl.flush(abstractentitymanagerimpl.java:795) ... 27 more org.springframework.transaction.transactionsystemexception: not commit jpa transaction; nested exception javax.persistence.rollbackexception: transacti on marked rollbackonly @ org.springframework.orm.jpa.jpatransactionmanager.docommit(jpatransactionmanager.java:526) @ org.springframework.transaction.support.abstractplatformtransactionmanager.processcommit(abstractplatformtransactionmanager.java:761) @ org.springframework.transaction.support.abstractplatformtransactionmanager.commit(abstractplatformtransactionmanager.java:730) @ org.springframework.transaction.interceptor.transactionaspectsupport.committransactionafterreturning(transactionaspectsupport.java:504) @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:292) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:96) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:213) @ com.sun.proxy.$proxy29.persist(unknown source) caused by: javax.persistence.rollbackexception: transaction marked rollbackonly @ org.hibernate.ejb.transactionimpl.commit(transactionimpl.java:73) @ org.springframework.orm.jpa.jpatransactionmanager.docommit(jpatransactionmanager.java:517) ... 12 more

hibernate unable save child objects using cascade all

the reason why not work has way you're trying persit entities.

  • let see following code doing:

    type type = new type(); content content = somemethodtogetcontent(); type.setcontent(content); save(type);  

you telling persistence provider want save type calling save(type) , expecting content should saved database. in entity type onetoone annotation not contain persist cascade option. have tell persistence provider to pesist instance of content when instance of type persisted changing onetoone annotation follows:

@onetoone(cascade=cascadetype.persist)    // modified @joincolumn(name = "content_id") private content content; 

and because defined entities have bi-directional relationship should wire them follows correctly:

type type = new type(); content content = somemethodtogetcontent(); content.settype(type);                       // modified type.setcontent(content); save(type);  

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -