JPA/Hibernate - Java EE - Unable to find model.Composer with id model.ComposerId@0 -
i'm trying persist in db commande object (sorry it's in french ;)). db : here mapping : composer.java:
package model; import java.io.serializable; import javax.persistence.*; import model.panier; import model.produit; /** * entity implementation class entity: composer * */ @entity @table(name="composer") @idclass(composerid.class) public class composer implements serializable { @id private int idpan; @id private int idprod; @manytoone //@primarykeyjoincolumn(name="idpan", referencedcolumnname="id") @joincolumn(name="idpan", referencedcolumnname="id", insertable = false, updatable = false) private panier panier; @manytoone //@primarykeyjoincolumn(name="idprod", referencedcolumnname="id") @joincolumn(name="idprod", referencedcolumnname="id" , insertable = false, updatable = false) private produit produit; private int nbprodpan; private static final long serialversionuid = 1l; public composer() { super(); } public panier getpanier() { return this.panier; } public void setpanier(panier panier) { this.panier = panier; } public produit getproduit() { return this.produit; } public void setproduit(produit produit) { this.produit = produit; } public int getnbprodpan() { return this.nbprodpan; } public void setnbprodpan(int nbprodpan) { this.nbprodpan = nbprodpan; } }
commande.java
package model; import java.io.serializable; import javax.persistence.*; /** * entity implementation class entity: commande * */ @entity @table(name="commande") public class commande implements serializable { @id @generatedvalue private int id; private int modlivrcom; private static final long serialversionuid = 1l; @onetoone(cascade = cascadetype.all) @joincolumn(name="idpan") private panier panier; @manytoone(cascade = cascadetype.all) @joincolumn(name="idutil") private utilisateur utilisateur; @manytoone(fetch=fetchtype.eager) @joincolumn(name="idadr") private adresse adresse; @manytoone(fetch=fetchtype.eager) @joincolumn(name="idetat") private etatcmd etatcmd; public commande() { super(); } public int getid() { return this.id; } public void setid(int id) { this.id = id; } public int getmodlivrcom() { return this.modlivrcom; } public void setmodlivrcom(int modlivrcom) { this.modlivrcom = modlivrcom; } public panier getpanier() { return panier; } public void setpanier(panier panier) { this.panier = panier; } public utilisateur getutilisateur() { return utilisateur; } public void setutilisateur(utilisateur utilisateur) { this.utilisateur = utilisateur; } public adresse getadresse() { return adresse; } public void setadresse(adresse adresse) { this.adresse = adresse; } public etatcmd getetatcmd() { return etatcmd; } public void setetatcmd(etatcmd etatcmd) { this.etatcmd = etatcmd; } }
panier.java :
package model; import java.io.serializable; import java.util.list; import javax.persistence.*; /** * entity implementation class entity: panier * */ @entity @table(name="panier") public class panier implements serializable { @id @generatedvalue private int id; private float prixtotpan; private static final long serialversionuid = 1l; @onetoone(mappedby="panier") private commande commande; @onetomany(mappedby="panier") private list<composer> composers; public panier() { super(); } public int getid() { return this.id; } public void setid(int id) { this.id = id; } public float getprixtotpan() { return this.prixtotpan; } public void setprixtotpan(float prixtotpan) { this.prixtotpan = prixtotpan; } public list<composer> getcomposers() { return composers; } public void setcomposers(list<composer> composers) { this.composers = composers; } }
buy.java
commande lacommande = new commande(); if(integer.parseint(request.getparameter("idcommande"))==-1){ utilisateur utilisateur = utilisateurdao.find(((utilisateur)request.getsession().getattribute("user")).getid()); lacommande.setadresse(utilisateur.getadresse()); } else{ enseigne enseigne = enseignedao.find(integer.parseint(request.getparameter("idlivraison"))); lacommande.setadresse(enseigne.getadresse()); } model.panier panier = (model.panier)request.getsession().getattribute("panier"); panierdao.update(panier); etatcmd etatcmd = etatcmddao.find(0); lacommande.setetatcmd(etatcmd); lacommande.setmodlivrcom(integer.parseint(request.getparameter("idcommande"))); lacommande.setpanier(panier); lacommande.setutilisateur((utilisateur)request.getsession().getattribute("user")); commandedao.create(lacommande); requestdispatcher dispatcher = request.getrequestdispatcher("/jsp/confirmation.jsp"); dispatcher.forward(request, response);
when try execute code, no object persisted, , obtain error page :
http status 500 - unable find model.composer id model.composerid@0 type exception report message unable find model.composer id model.composerid@0 description server encountered internal error prevented fulfilling request. exception javax.persistence.entitynotfoundexception: unable find model.composer id model.composerid@0 org.hibernate.jpa.boot.internal.entitymanagerfactorybuilderimpl$jpaentitynotfounddelegate.handleentitynotfound(entitymanagerfactorybuilderimpl.java:183) org.hibernate.event.internal.defaultloadeventlistener.load(defaultloadeventlistener.java:219) org.hibernate.event.internal.defaultloadeventlistener.proxyorload(defaultloadeventlistener.java:275) org.hibernate.event.internal.defaultloadeventlistener.onload(defaultloadeventlistener.java:151) org.hibernate.internal.sessionimpl.fireload(sessionimpl.java:1070) org.hibernate.internal.sessionimpl.internalload(sessionimpl.java:989) org.hibernate.type.entitytype.resolveidentifier(entitytype.java:716) org.hibernate.type.entitytype.resolve(entitytype.java:502) org.hibernate.type.entitytype.replace(entitytype.java:366) org.hibernate.type.collectiontype.replaceelements(collectiontype.java:549) org.hibernate.type.collectiontype.replace(collectiontype.java:690) org.hibernate.type.abstracttype.replace(abstracttype.java:178) org.hibernate.type.typehelper.replaceassociations(typehelper.java:261) org.hibernate.event.internal.defaultmergeeventlistener.copyvalues(defaultmergeeventlistener.java:433) org.hibernate.event.internal.defaultmergeeventlistener.entityistransient(defaultmergeeventlistener.java:256) org.hibernate.event.internal.defaultmergeeventlistener.onmerge(defaultmergeeventlistener.java:189) org.hibernate.event.internal.defaultmergeeventlistener.onmerge(defaultmergeeventlistener.java:85) org.hibernate.internal.sessionimpl.firemerge(sessionimpl.java:876) org.hibernate.internal.sessionimpl.merge(sessionimpl.java:858) org.hibernate.internal.sessionimpl.merge(sessionimpl.java:863) org.hibernate.jpa.spi.abstractentitymanagerimpl.merge(abstractentitymanagerimpl.java:1196) dao.panierdao.update(panierdao.java:44) front.buy.dopost(buy.java:60) javax.servlet.http.httpservlet.service(httpservlet.java:646) javax.servlet.http.httpservlet.service(httpservlet.java:727)
what can do?
your composer.class
has 2 fields annotated @id
. in sql can have 1 primary key. if both field should compose primary key, consider using composite primary key, see example jpa documentation, says:
composite primary keys denoted using
javax.persistence.embeddedid
,javax.persistence.idclass
annotations.
Comments
Post a Comment