gpt4 book ai didi

java - USE JpaWhere 带有列表 := Key

转载 作者:行者123 更新时间:2023-12-01 10:34:35 24 4
gpt4 key购买 nike

我想使用列表对象执行 SQL JPA 查询,但是当我尝试时出现以下错误消息:

Cannot be resolved to a valid type.

我将 JPA 与 Maven 一起使用。我想这样做:

@Entity
@Table(name = "Personne")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Personne.findAll", query = "SELECT p FROM Personne p"),
@NamedQuery(name = "Personne.findByKeyClient", query = "SELECT p FROM Personne p WHERE p.keyClient = :keyClient"),
@NamedQuery(name = "Personne.CountPersonne", query = "SELECT COUNT(p) FROM Personne p WHERE p.sessionCollection.keySession = :keySession "),
@NamedQuery(name = "Personne.findByNom", query = "SELECT p FROM Personne p WHERE p.nom = :nom"),
@NamedQuery(name = "Personne.findByPrenom", query = "SELECT p FROM Personne p WHERE p.prenom = :prenom"),
@NamedQuery(name = "Personne.findByEmail", query = "SELECT p FROM Personne p WHERE p.email = :email"),
@NamedQuery(name = "Personne.findByTelephone", query = "SELECT p FROM Personne p WHERE p.telephone = :telephone"),
@NamedQuery(name = "Personne.findByPortable", query = "SELECT p FROM Personne p WHERE p.portable = :portable"),
@NamedQuery(name = "Personne.findByFax", query = "SELECT p FROM Personne p WHERE p.fax = :fax"),
@NamedQuery(name = "Personne.findByDateModification", query = "SELECT p FROM Personne p WHERE p.dateModification = :dateModification"),
@NamedQuery(name = "Personne.findByDateCreation", query = "SELECT p FROM Personne p WHERE p.dateCreation = :dateCreation"),
@NamedQuery(name = "Personne.findByActif", query = "SELECT p FROM Personne p WHERE p.actif = :actif")})
public class Personne implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "KeyClient")
private String keyClient;
@Size(max = 255)
@Column(name = "Nom")
private String nom;
@Size(max = 255)
@Column(name = "Prenom")
private String prenom;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Size(max = 255)
@Column(name = "Email")
private String email;
@Size(max = 255)
@Column(name = "Telephone")
private String telephone;
@Size(max = 255)
@Column(name = "Portable")
private String portable;
@Size(max = 255)
@Column(name = "TypeInscription")
private String typeInscription;
// @Pattern(regexp="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$", message="Invalid phone/fax format, should be as xxx-xxx-xxxx")//if the field contains phone or fax number consider using this annotation to enforce field validation
@Size(max = 255)
@Column(name = "Fax")
private String fax;
@Column(name = "DateModification")
@Temporal(TemporalType.DATE)
private Date dateModification;
@Column(name = "DateCreation")
@Temporal(TemporalType.DATE)
private Date dateCreation;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "KeyTypeUser")
private TypeUser typeUser;
@Size(max = 1)
@Column(name = "actif")
private String actif;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "KeyEntreprise")
private Entreprise entreprise;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "KeyCivilite")
private Civilite civilite;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Login> loginCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<SourceInscription> sourceInscriptionCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<CorrespondanceImportation> CorrespondanceImportationCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Hotesse> hotesseCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Passage> passageCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Produit> produitCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Droit> droitCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<ReponseClient> reponseClientCollection;
@OneToMany(mappedBy = "utilisateurSource",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<SourceInscription> PersonneUtilisateurSourceCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<AutreInformation> autreInformationCollection;
@OneToMany(mappedBy = "personne",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<CrmLien> CrmLienCollection;
@ManyToMany(cascade = CascadeType.ALL)
private List<Session> sessionCollection;

public Personne() {
}

public Personne(String keyClient) {
this.keyClient = keyClient;
}

public String getKeyClient() {
return keyClient;
}

public void setKeyClient(String keyClient) {
this.keyClient = keyClient;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}

public String getPrenom() {
return prenom;
}

public void setPrenom(String prenom) {
this.prenom = prenom;
}

public String getTypeInscription() {
return typeInscription;
}

public void setTypeInscription(String typeInscription) {
this.typeInscription = typeInscription;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getTelephone() {
return telephone;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public String getPortable() {
return portable;
}

public void setPortable(String portable) {
this.portable = portable;
}

public String getFax() {
return fax;
}

public void setFax(String fax) {
this.fax = fax;
}

public Date getDateModification() {
return dateModification;
}

public void setDateModification(Date dateModification) {
this.dateModification = dateModification;
}

public Date getDateCreation() {
return dateCreation;
}

public void setDateCreation(Date dateCreation) {
this.dateCreation = dateCreation;
}

public String getActif() {
return actif;
}

public void setActif(String actif) {
this.actif = actif;
}

public TypeUser getTypeUser() {
return typeUser;
}

public void setTypeUser(TypeUser typeUser) {
this.typeUser = typeUser;
}
@XmlTransient
public List<Login> getLoginCollection() {
return loginCollection;
}

public void setLoginCollection(List<Login> loginCollection) {
this.loginCollection = loginCollection;
}
@XmlTransient
public List<CrmLien> getCrmLienCollection() {
return CrmLienCollection;
}

public void setCrmLienCollection(List<CrmLien> CrmLienCollection) {
this.CrmLienCollection = CrmLienCollection;
}

@XmlTransient
public List<SourceInscription> getSourceInscriptionCollection() {
return sourceInscriptionCollection;
}

public void setSourceInscriptionCollection(List<SourceInscription> sourceInscriptionCollection) {
this.sourceInscriptionCollection = sourceInscriptionCollection;
}

public Entreprise getEntreprise() {
return entreprise;
}

public void setEntreprise(Entreprise entreprise) {
this.entreprise = entreprise;
}
@XmlTransient
public List<CorrespondanceImportation> getCorrespondanceImportationCollection() {
return CorrespondanceImportationCollection;
}

public void setCorrespondanceImportationCollection(List<CorrespondanceImportation> CorrespondanceImportationCollection) {
this.CorrespondanceImportationCollection = CorrespondanceImportationCollection;
}

public Civilite getCivilite() {
return civilite;
}

public void setCivilite(Civilite civilite) {
this.civilite = civilite;
}
@XmlTransient
public List<Hotesse> getHotesseCollection() {
return hotesseCollection;
}

public void setHotesseCollection(List<Hotesse> hotesseCollection) {
this.hotesseCollection = hotesseCollection;
}
@XmlTransient
public List<Passage> getPassageCollection() {
return passageCollection;
}

public void setPassageCollection(List<Passage> passageCollection) {
this.passageCollection = passageCollection;
}
@XmlTransient
public List<SourceInscription> getPersonneUtilisateurSourceCollection() {
return PersonneUtilisateurSourceCollection;
}

public void setPersonneUtilisateurSourceCollection(List<SourceInscription> PersonneUtilisateurSourceCollection) {
this.PersonneUtilisateurSourceCollection = PersonneUtilisateurSourceCollection;
}

@XmlTransient
public List<Produit> getProduitCollection() {
return produitCollection;
}

public void setProduitCollection(List<Produit> produitCollection) {
this.produitCollection = produitCollection;
}
@XmlTransient
public List<Droit> getDroitCollection() {
return droitCollection;
}

public void setDroitCollection(List<Droit> droitCollection) {
this.droitCollection = droitCollection;
}
@XmlTransient
public List<ReponseClient> getReponseClientCollection() {
return reponseClientCollection;
}

public void setReponseClientCollection(List<ReponseClient> reponseClientCollection) {
this.reponseClientCollection = reponseClientCollection;
}
@XmlTransient
public List<AutreInformation> getAutreInformationCollection() {
return autreInformationCollection;
}

public void setAutreInformationCollection(List<AutreInformation> autreInformationCollection) {
this.autreInformationCollection = autreInformationCollection;
}
@XmlTransient
public List<Session> getSessionCollection() {
return sessionCollection;
}

public void setSessionCollection(List<Session> sessionCollection) {
this.sessionCollection = sessionCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (keyClient != null ? keyClient.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Personne)) {
return false;
}
Personne other = (Personne) object;
if ((this.keyClient == null && other.keyClient != null) || (this.keyClient != null && !this.keyClient.equals(other.keyClient))) {
return false;
}
return true;
}

@Override
public String toString() {
return "fr.ietevents.client.Entity.Personne[ keyClient=" + keyClient + " ]";
}

}

session 实体:

@Entity
@Table(name = "Session")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Session.findAll", query = "SELECT s FROM Session s"),
@NamedQuery(name = "Session.findByKeyEdition", query = "SELECT s FROM Session s WHERE s.keySession = :keySession"),
@NamedQuery(name = "Session.findByNom", query = "SELECT s FROM Session s WHERE s.nom = :nom")})
public class Session implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "KeySession")
private String keySession;
@Size(max = 90)
@Column(name = "Nom")
private String nom;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "KeyAdresse")
private Adresse adresse ;
@OneToMany(mappedBy = "session",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Passage> passageCollection;
@OneToMany(mappedBy = "session",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Droit> droitCollection;
@OneToMany(mappedBy = "session",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Question> questionCollection;
@OneToMany(mappedBy = "session",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Css> cssCollection;
@OneToMany(mappedBy = "session",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<Site> siteCollection;
@OneToMany(mappedBy = "session",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List<CrmLien> crmLienCollection;
@ManyToMany(mappedBy="sessionCollection",cascade = CascadeType.ALL)
private List<Personne> personneCollection;

public Session() {
}
public Session(String keySession) {
this.keySession = keySession;
}

public String getKeySession() {
return keySession;
}

public void setKeySession(String keyEdition) {
this.keySession = keyEdition;
}

public String getNom() {
return nom;
}

public void setNom(String nom) {
this.nom = nom;
}

public Adresse getAdresse() {
return adresse;
}

public void setAdresse(Adresse adresse) {
this.adresse = adresse;
}
@XmlTransient
public List<CrmLien> getCrmLienCollection() {
return crmLienCollection;
}

public void setCrmLienCollection(List<CrmLien> crmLienCollection) {
this.crmLienCollection = crmLienCollection;
}

@XmlTransient
public List<Passage> getPassageCollection() {
return passageCollection;
}

public void setPassageCollection(List<Passage> passageCollection) {
this.passageCollection = passageCollection;
}
@XmlTransient
public List<Droit> getDroitCollection() {
return droitCollection;
}

public void setDroitCollection(List<Droit> droitCollection) {
this.droitCollection = droitCollection;
}
@XmlTransient
public List<Question> getQuestionCollection() {
return questionCollection;
}

public void setQuestionCollection(List<Question> questionCollection) {
this.questionCollection = questionCollection;
}
@XmlTransient
public List<Css> getCssCollection() {
return cssCollection;
}

public void setCssCollection(List<Css> CssCollection) {
this.cssCollection = CssCollection;
}
@XmlTransient
public List<Site> getSiteCollection() {
return siteCollection;
}

public void setSiteCollection(List<Site> siteCollection) {
this.siteCollection = siteCollection;
}
@XmlTransient
public List<Personne> getPersonneCollection() {
return personneCollection;
}

public void setPersonneCollection(List<Personne> personneCollection) {
this.personneCollection = personneCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (keySession != null ? keySession.hashCode() : 0);
return hash;
}



@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Session)) {
return false;
}
Session other = (Session) object;
if ((this.keySession == null && other.keySession != null) || (this.keySession != null && !this.keySession.equals(other.keySession))) {
return false;
}
return true;
}

@Override
public String toString() {
return "fr.ietevents.client.Entity.Session[ keyEdition=" + keySession + " ]";
}

}

我的 Collection session 是一个对象 session 列表。

public Long CountPersonne(Session  session,Pays pays){
EntityManager em = getEntityManager();
try{
Query query = em.createNamedQuery("Personne.CountPersonne");
query.setParameter("keySession",session.getKeySession());
// query.setParameter("keyPays",pays.getKeyPays());
return (Long) query.getSingleResult();

}catch(Exception e){
System.out.println(e);
return 0l;
}finally{
em.close();

}
}

错误消息:

javax.persistence.PersistenceException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [fr.ietevents_Client_war_1.0-SNAPSHOTPU] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT COUNT(p) FROM Personne p WHERE p.sessionCollection.keySession = :keySession ].
[38, 68] The state field path 'p.sessionCollection.keySession' cannot be resolved to a valid type.
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:820)
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:760)
org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:182)
org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getDatabaseSession(EntityManagerFactoryImpl.java:527)
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:140)
org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
fr.ietevents.client.Facades.SessionFacade.getEntityManager(SessionFacade.java:27)
fr.ietevents.client.Facades.AbstractFacade.find(AbstractFacade.java:96)
fr.ietevents.client.MyResource.getIt(MyResource.java:395)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
org.glassfish.jersey.internal.Errors.process(Errors.java:315)
org.glassfish.jersey.internal.Errors.process(Errors.java:297)
org.glassfish.jersey.internal.Errors.process(Errors.java:267)
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

最佳答案

您不能使用

p.sessionCollection.keySession

在 JPQL 中,因为那里有一个多值字段。您需要自己在查询中形成 JOIN,如下所示

SELECT COUNT(p) FROM Personne p JOIN p.sessionCollection sc WHERE sc.keySession = :keySession 

关于java - USE JpaWhere 带有列表 := Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34851281/

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