Boa tarde pessoal.
Estou, com um problema aki, como faço para salvar os dados de tabelas distintas usando hibernate e JSF.
Tenho uma de Produto e outra de Autor
Produto
@Entity
public class Produto {
@Id
@GeneratedValue
private Long id;
//outros atributos
@JoinColumn(name = "autor", referencedColumnName = "id")
@ManyToOne
private Autor autor;
//Getter's Setter's
Autor
@Entity
public class Autor {
@Id
@GeneratedValue
private Long id;
//outros atributos
//getter's Setter's
ProdutoBean
public void grava(){
Dao<Produto> dao = new Dao<Produto>(Produto.class);
if(produto.getId() == null){
produto.setAutor(autor);
dao.salva(produto);
}else{
dao.atualiza(produto);
}
this.produto = new Produto();
this.produtos = dao.listaTudo();
}
em meu XHTML chamo este metodo ao clicar o mesmo me da o seguinte erro.
javax.servlet.ServletException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: br.com.loja.modelo.Autor
javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
root cause
javax.faces.el.EvaluationException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: br.com.loja.modelo.Autor
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:787)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1252)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
root cause
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: br.com.loja.modelo.Autor
org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3403)
org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
br.com.loja.util.HibernateUtil.commitTransaction(HibernateUtil.java:52)
br.com.loja.dao.Dao.salva(Dao.java:34)
br.com.loja.mb.ProdutoBean.grava(ProdutoBean.java:26)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.el.parser.AstValue.invoke(AstValue.java:262)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
javax.faces.component.UICommand.broadcast(UICommand.java:315)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:787)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1252)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
Alguem pode me dizer onde estou errando?
vlew pessoal