gpt4 book ai didi

java - JPA/Hibernate - Java EE - 无法找到 ID 为 model.ComposerId@0 的 model.Composer

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:20 25 4
gpt4 key购买 nike

我试图在我的数据库中保留一个 COMMANDE 对象(抱歉它是法语;))。我的数据库: Description of my BD这是我的映射: Composer .java:

package model;

import java.io.Serializable;

import javax.persistence.*;

import model.Panier;
import model.Produit;

/**
* Entity implementation class for 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 for 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 for 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;
}

}

购买.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);

当我尝试执行此代码时,没有对象被持久化,并且我收到此错误页面:

HTTP Status 500 - Unable to find model.Composer with id model.ComposerId@0

type Exception report

message Unable to find model.Composer with id model.ComposerId@0

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.persistence.EntityNotFoundException: Unable to find model.Composer with 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)

我能做什么?

最佳答案

您的Composer.class有两个用@Id注释的字段。在 SQL 中只能有一个主键。如果两个字段都应构成主键,请考虑使用复合主键,例如,请参阅 JPA documentation ,其中写着:

Composite primary keys are denoted using the javax.persistence.EmbeddedId and javax.persistence.IdClass annotations.

关于java - JPA/Hibernate - Java EE - 无法找到 ID 为 model.ComposerId@0 的 model.Composer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30172223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com