gpt4 book ai didi

java - 无法解决属性查询异常

转载 作者:行者123 更新时间:2023-12-01 13:07:29 26 4
gpt4 key购买 nike

当我尝试显示来自 EmergencyContact 的数据时,出现以下异常。 SurveyData 和 EmergencyData 通过 OneToMany 关联。我能够保留数据,但无法将其检索回来。

我想提供一个 SurveyID 作为输入并获取该调查的紧急联系人。我验证了所有的 setter 和 getter 方法,它们看起来没问题。我无法追踪我在哪里犯了错误。如有任何帮助,我们将不胜感激。

16:53:42,029 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) Caused by: org.hibernate.QueryException: could not resolve property: survey_id of: masonsurveyejb.businesslogic.EmergencyContact [SELECT emc FROM masonsurveyejb.businesslogic.EmergencyContact emc WHERE emc.survey_id = 11]

我的查询字符串如下所示。我收到 Survey_id 变量值作为此方法中的参数。

"SELECT emc FROM EmergencyContact emc WHERE emc.survey_id = " + survey_id

以下是我的 EmergencyContact 和 SurveyData 类

package masonsurveyejb.businesslogic;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "EmergencyData")
public class EmergencyContact implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "EMERGENCY_SEQUENCE", sequenceName = "Survey_Seq", initialValue = 100, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMERGENCY_SEQUENCE")
private Integer contactId;
@Column(name = "Name")
private String emName;
@Column(name = "Phone")
private String emPhone;
@Column(name = "Email")
private String emEmail;

@ManyToOne
@JoinColumn(name = "SurveyID")
private SurveyData surveyData;

public Integer getContactId() {
return contactId;
}

public void setContactId(Integer contactId) {
this.contactId = contactId;
}

public SurveyData getSurveyData() {
return surveyData;
}

public void setSurveyData(SurveyData surveyData) {
this.surveyData = surveyData;
}

public String getEmName() {
return emName;
}

public void setEmName(String emName) {
this.emName = emName;
}

public String getEmPhone() {
return emPhone;
}

public void setEmPhone(String emPhone) {
this.emPhone = emPhone;
}

public String getEmEmail() {
return emEmail;
}

public void setEmEmail(String emEmail) {
this.emEmail = emEmail;
}
}


///////////////////////////////////////////////////////////////////////////////
// @filename: Student.java
// @description: This file is the a simple class which is used as a model
// object for Student.
//
// Modification History
// Date Author Change Reason
// ==== ====== =============
// 2014-02-20 Santosh K Tadikonda Initial Creation
//
///////////////////////////////////////////////////////////////////////////////

package masonsurveyejb.businesslogic;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;

@Entity
public class SurveyData {

@Id
@SequenceGenerator(name = "SURVEY_SEQUENCE", sequenceName = "Survey_Seq", initialValue = 100, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SURVEY_SEQUENCE")
@Column(name = "SurveyID")
private Integer survey_id;
private String firstname;
private String lastname;
private String address;
private String city;
private String state;
private String zipcode;
private String telephone;
private String email;
private String surveydate;
@Column(name = "Likes")
private String chklike;
@Column(name = "Know")
private String radioknow;
private String recommend;
private String raffle;
private String comments;

@OneToMany(mappedBy = "surveyData", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<EmergencyContact> emContacts;

public List<EmergencyContact> getEmContacts() {
return emContacts;
}

public void setEmContacts(List<EmergencyContact> emContacts) {
this.emContacts = emContacts;
}

public Integer getSurvey_id() {
return survey_id;
}

public void setSurvey_id(Integer survey_id) {
this.survey_id = survey_id;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZipcode() {
return zipcode;
}

public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}

public String getTelephone() {
return telephone;
}

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

public String getEmail() {
return email;
}

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

public String getSurveydate() {
return surveydate;
}

public void setSurveydate(String surveydate) {
this.surveydate = surveydate;
}

public String getChklike() {
return chklike;
}

public void setChklike(String chklike) {
this.chklike = chklike;
}

public String getRadioknow() {
return radioknow;
}

public void setRadioknow(String radioknow) {
this.radioknow = radioknow;
}

public String getRecommend() {
return recommend;
}

public void setRecommend(String recommend) {
this.recommend = recommend;
}

public String getRaffle() {
return raffle;
}

public void setRaffle(String raffle) {
this.raffle = raffle;
}

public String getComments() {
return comments;
}

public void setComments(String comments) {
this.comments = comments;
}

public String toString() {

String studentString;

studentString = getSurvey_id() + ";" +getFirstname() + ";" + getLastname() + ";"
+ getAddress() + ";" + getCity() + ";" + getState() + ";"
+ getZipcode() + ";" + getTelephone() + ";" + getEmail() + ";"
+ getSurveydate() + ";" + getChklike() + ";" + getRadioknow()
+ ";" + getRecommend() + ";" + getRaffle() + ";"
+ getComments();

return studentString;
}
}

以下是我进行查询的方法

@Override
public ArrayList<EmergencyContact> GetEmergencyContacts(String survey_id) {

try {

if (survey_id == null || survey_id.length() == 0) {
System.out.println("Received a null or empty survey ID.");
return null;
}

System.out.println("Getting emergency contacts for " + survey_id);
String queryStr = "SELECT emc FROM EmergencyContact emc WHERE emc.survey_id = "
+ survey_id;
Query query = entityManager.createQuery(queryStr);
List resultList = query.getResultList();

if (resultList != null && resultList.size() > 0) {
ArrayList<EmergencyContact> emList = new ArrayList<EmergencyContact>();
for (Object result : resultList) {
EmergencyContact emc = new EmergencyContact();
emc = (EmergencyContact) result;
emList.add(emc);
}

return emList;
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

最佳答案

您收到异常,因为实体“EmergencyContact”没有名为“survey_id”的属性。

您的查询应该是:

String queryStr = "SELECT emc FROM EmergencyContact emc WHERE emc.surveyData.survey_id = :id";
Query query = entityManager.createQuery(queryStr).setParameter("id", survey_id);
List resultList = query.getResultList();

请考虑两件事:

  1. 在 JPQL 查询中使用属性名称 (surveyData.survey_id)
  2. 最好使用特殊方法“setParameter”设置查询参数

关于java - 无法解决属性查询异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23162483/

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