Thank you very much for your reply. Now I will explain what I'm doing. If I try:
Code:
EntityManager em = ServicoPilotoFeedBack.instancia().getEmf().createEntityManager();
ClienteDAO daoc = new ClienteDAO(em);
AgenteDAO daoa = new AgenteDAO(em);
em.getTransaction().begin();
OrdemServico os = new OrdemServico();
os.setCliente((Cliente)daoc.consulta(((ClienteListModel)cmbClientes.getModel()).getIdCliente(cmbClientes.getSelectedIndex())));
os.setAgente((Agente)daoa.consulta(((AgenteListModel)cmbAgentes.getModel()).getIdAgente(cmbAgentes.getSelectedIndex())));
List<DirecionamentoProducao> dss = (List<DirecionamentoProducao>)novosOSTableModel.getDirecionamentos();
os.setDirecionamentosProducao(dss);
em.getTransaction().commit();
em.close();
I get the exception:
Code:
Exception in thread "AWT-EventQueue-0" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [br.com.feedbackcard.piloto.modelo.DirecionamentoProducao#0]
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:150)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:124)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:84)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:525)
at org.hibernate.engine.CascadingAction$8.cascade(CascadingAction.java:201)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:211)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:155)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:106)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:288)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:183)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:158)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:106)
at org.hibernate.engine.Cascade.cascade(Cascade.java:246)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:124)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:115)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:62)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:736)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:330)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:36)
at br.com.feedbackcard.piloto.visao.MDIPrincipal.btnCadastraOSActionPerformed(MDIPrincipal.java:2030)
at br.com.feedbackcard.piloto.visao.MDIPrincipal.access$3200(MDIPrincipal.java:47)
at br.com.feedbackcard.piloto.visao.MDIPrincipal$33.actionPerformed(MDIPrincipal.java:1803)
If I do a "merge" before the commit like this:
Code:
for (int inc = 0; inc < dss.size(); inc++) {
dss.get(inc).setDirecionamento(em.merge(dss.get(inc).getDirecionamento()));
dss.set(inc, em.merge(dss.get(inc)));
}
I get the exception:
Code:
Caused by: java.sql.BatchUpdateException: Column 'IdCliente' cannot be null
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:891)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 42 more
And finally, if I try:
Code:
for (int inc = 0; inc < dss.size(); inc++) {
dss.get(inc).getDirecionamento().setCliente(os.getCliente());
dss.get(inc).setDirecionamento(em.merge(dss.get(inc).getDirecionamento()));
dss.get(inc).setOrdemServico(os);
dss.set(inc, em.merge(dss.get(inc)));
}
The OrdemServico object is saved by Hibernate. However, just one DirecionamentoProducao object and the respective Direcionamento object is saved. The remain objects in the List do'nt are saved.
[]s
Ary Junior