Olá, estou com duvidas sobre gerar classe de entidade de banco de dados, gerei a classe normalmente e ela criou os controller automatico pelo netbeans, lista, excluir, inclui tudo altomatico… e agora me surgiu erros e duvidas. entao…
Entidade 1: Plano de pagamento @OnetoMany ou seja uma plano de pagamento tem várias parcelas(lista de parcela).
Entidade 2: Parcelas do plano de pagamento @ManyToOne ou seja varias parcelas estão relacionadas a um plano de pagamento.
Quando vou inserir o plano de pagamento dá erro dizendo que o codigo da PARCELA está nulo, porem ele é autoIncrement. Vou colar abaixo as 2 entidades.
Plano de pagamento:
@Id
@Basic(optional = false)
@SequenceGenerator(name = "id", initialValue=1, allocationSize = 1)
@GeneratedValue(strategy=GenerationType.TABLE, generator = "id")
@Column(name = "CODPLANOPAG")
private Integer codplanopag;
@Column(name = "NOMEPLANOPAG")
private String nomeplanopag;
@Column(name = "ACRESPLANOPAG")
private Double acresplanopag;
@Column(name = "DESCPLANOPAG")
private Double descplanopag;
@Column(name = "ENTRADAPLANOPAG")
private Character entradaplanopag;
@Column(name = "TIPOPLANOPAG")
private Character tipoplanopag;
@Column(name = "STATUSPLANOPAG")
private Character statusplanopag;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "planoPagamentoCodplanopag")
private List<Pedido> pedidoCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "planoPagamentoCodplanopag")
private List<Compra> compraCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "planoPagamentoCodplanopag")
private List<ParcelasPlanoPagamento> parcelasPlanoPagamentoCollection;
Parcelas Plano de pagamento:
@Id
@Basic(optional = false)
@SequenceGenerator(name = "id_parc", initialValue=1, allocationSize = 1)
@GeneratedValue(strategy=GenerationType.TABLE, generator = "id_parc")
@Column(name = "CODPARCELA")
private Integer codparcela;
@Column(name = "NRPARCELA")
private Integer nrparcela;
@Column(name = "INTERVALODIAPARCELA")
private Integer intervalodiaparcela;
@JoinColumn(name = "PLANO_PAGAMENTO_CODPLANOPAG", referencedColumnName = "CODPLANOPAG")
@ManyToOne(optional = false)
private PlanoPagamento planoPagamentoCodplanopag;
Entidade de banco de dados criado pelo netbeans. Metodo INCLUIR:
public void create(PlanoPagamento planoPagamento) throws PreexistingEntityException, Exception {
if (planoPagamento.getParcelasPlanoPagamentoCollection() == null) {
planoPagamento.setParcelasPlanoPagamentoCollection(new ArrayList<ParcelasPlanoPagamento>());
}
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
List<ParcelasPlanoPagamento> attachedParcelasPlanoPagamentoCollection = new ArrayList<ParcelasPlanoPagamento>();
for (ParcelasPlanoPagamento parcelasPlanoPagamentoCollectionParcelasPlanoPagamentoToAttach : planoPagamento.getParcelasPlanoPagamentoCollection()) {
parcelasPlanoPagamentoCollectionParcelasPlanoPagamentoToAttach = em.getReference(parcelasPlanoPagamentoCollectionParcelasPlanoPagamentoToAttach.getClass(), parcelasPlanoPagamentoCollectionParcelasPlanoPagamentoToAttach.getCodparcela());
attachedParcelasPlanoPagamentoCollection.add(parcelasPlanoPagamentoCollectionParcelasPlanoPagamentoToAttach);
}
planoPagamento.setParcelasPlanoPagamentoCollection(attachedParcelasPlanoPagamentoCollection);
em.persist(planoPagamento);
for (ParcelasPlanoPagamento parcelasPlanoPagamentoCollectionParcelasPlanoPagamento : planoPagamento.getParcelasPlanoPagamentoCollection()) {
PlanoPagamento oldPlanoPagamentoCodplanopagOfParcelasPlanoPagamentoCollectionParcelasPlanoPagamento = parcelasPlanoPagamentoCollectionParcelasPlanoPagamento.getPlanoPagamentoCodplanopag();
parcelasPlanoPagamentoCollectionParcelasPlanoPagamento.setPlanoPagamentoCodplanopag(planoPagamento);
parcelasPlanoPagamentoCollectionParcelasPlanoPagamento = em.merge(parcelasPlanoPagamentoCollectionParcelasPlanoPagamento);
if (oldPlanoPagamentoCodplanopagOfParcelasPlanoPagamentoCollectionParcelasPlanoPagamento != null) {
oldPlanoPagamentoCodplanopagOfParcelasPlanoPagamentoCollectionParcelasPlanoPagamento.getParcelasPlanoPagamentoCollection().remove(parcelasPlanoPagamentoCollectionParcelasPlanoPagamento);
oldPlanoPagamentoCodplanopagOfParcelasPlanoPagamentoCollectionParcelasPlanoPagamento = em.merge(oldPlanoPagamentoCodplanopagOfParcelasPlanoPagamentoCollectionParcelasPlanoPagamento);
}
}
em.getTransaction().commit();
} catch (Exception ex) {
if (findPlanoPagamento(planoPagamento.getCodplanopag()) != null) {
throw new PreexistingEntityException("PlanoPagamento " + planoPagamento + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
Erro gerado:
java.lang.IllegalArgumentException: id to load is required for loading
at org.hibernate.event.LoadEvent.<init>(LoadEvent.java:51)
at org.hibernate.event.LoadEvent.<init>(LoadEvent.java:33)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:812)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
at erp.data.PlanoPagamentoJpaController.findPlanoPagamento(PlanoPagamentoJpaController.java:301)
at erp.data.PlanoPagamentoJpaController.create(PlanoPagamentoJpaController.java:107)
at erp.gui.GUIPlanoPagamentoCad.btnSalvarActionPerformed(GUIPlanoPagamentoCad.java:432)
at erp.gui.GUIPlanoPagamentoCad.access$300(GUIPlanoPagamentoCad.java:31)
at erp.gui.GUIPlanoPagamentoCad$2.actionPerformed(GUIPlanoPagamentoCad.java:189)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)