我正在一個Spring-MVC應用程序中,我想同時在多個變量上運行搜索。但是代碼在爲第一個值本身設置參數時保持失敗。我不知道爲什麼。你能幫忙的話,我會很高興。Hibernate:org.hibernate.QueryParameterException:無法找到命名參數
ListStudents:
@Override
public List<Student> addHostSearchHistory(HostSearchHistory hostSearchHistory, Long hostId) {
session = this.sessionFactory.getCurrentSession();
Host host = (Host) session.get(Host.class,hostId);
host.getHostSearchHistorySet().add(hostSearchHistory);
hostSearchHistory.setHsHistory(host);
session.save(hostSearchHistory);
session.flush();
StringBuilder sb = new StringBuilder();
sb.append("from Student as s where ");
if(!(hostSearchHistory.getCountry()==null)){
sb.append("s.studentCountry=:").append(hostSearchHistory.getCountry());
}
if(!(hostSearchHistory.getCity()==null)){
sb.append(" OR s.city=:").append(hostSearchHistory.getCity());
}
if(!(hostSearchHistory.getDrivingLicense()==null)){
sb.append(" OR s.studentInfoDetails.drivingLicense=").append(hostSearchHistory.getDrivingLicense());
}
if(!(hostSearchHistory.getGender()==null)){
sb.append(" OR s.gender=").append(hostSearchHistory.getGender());
}
if(!(hostSearchHistory.getMotherTongue()==null)){
sb.append(" OR s.studentInfoDetails.motherTongue=:").append(hostSearchHistory.getMotherTongue());
}
if(!(hostSearchHistory.getSmoker()==null)){
sb.append(" OR s.studentInfoDetails.smoker=").append(hostSearchHistory.getSmoker());
}
if(!(hostSearchHistory.getPreviousAuPair()==null)){
sb.append(" OR s.studentInfoDetails.previouslyAuPair=").append(hostSearchHistory.getPreviousAuPair());
}
if(!(hostSearchHistory.getWillingToWork()==null)){
sb.append(" OR s.studentInfoDetails.willingToWork=").append(hostSearchHistory.getWillingToWork());
}
if(!(hostSearchHistory.getWorkForSingleParent()==null)){
sb.append(" OR s.studentInfoDetails.workForSingleParent=").append(hostSearchHistory.getWorkForSingleParent());
}
if(!(hostSearchHistory.getWorkingForDisabledChild()==null)){
sb.append(" OR s.studentInfoDetails.workingForDisabledChild=").append(hostSearchHistory.getWorkingForDisabledChild());
}
if(!(hostSearchHistory.getOtherLanguages()==null)){
sb.append(" OR s.studentInfoDetails.otherLanguages=:").append(hostSearchHistory.getOtherLanguages());
}
sb.append(" order by s.registrationDate desc");
System.out.println("Sb.toString is "+sb.toString());
Query query = session.createQuery(sb.toString());
// The code fails here
if(!(hostSearchHistory.getCountry()==null)){
query.setParameter("studentCountry",hostSearchHistory.getCountry());
}
if(!(hostSearchHistory.getCity()==null)){
query.setParameter("city",hostSearchHistory.getCity());
}
if(!(hostSearchHistory.getDrivingLicense()==null)){
query.setParameter("drivingLicense",hostSearchHistory.getDrivingLicense());
}
if(!(hostSearchHistory.getGender()==null)){
query.setParameter("gender",hostSearchHistory.getGender());
}
if(!(hostSearchHistory.getMotherTongue()==null)){
query.setParameter("motherTongue",hostSearchHistory.getMotherTongue());
}
if(!(hostSearchHistory.getSmoker()==null)){
query.setParameter("smoker",hostSearchHistory.getSmoker());
}
if(!(hostSearchHistory.getPreviousAuPair()==null)){
query.setParameter("previouslyAuPair",hostSearchHistory.getPreviousAuPair());
}
if(!(hostSearchHistory.getWillingToWork()==null)){
query.setParameter("willingToWork",hostSearchHistory.getWillingToWork());
}
if(!(hostSearchHistory.getWorkForSingleParent()==null)){
query.setParameter("workForSingleParent",hostSearchHistory.getWorkForSingleParent());
}
if(!(hostSearchHistory.getWorkingForDisabledChild()==null)){
query.setParameter("workingForDisabledChild",hostSearchHistory.getWorkingForDisabledChild());
}
if(!(hostSearchHistory.getOtherLanguages()==null)){
query.setParameter("otherLanguages",hostSearchHistory.getOtherLanguages());
}
List<Student> studentList = query.list();
for(Student student : studentList){
System.out.println("Student name is "+student.getUsername());
}
return studentList;
}
sb.toString()的輸出:
Sb.toString is from Student as s where s.studentCountry=:Germany OR s.city=:Hamburg OR s.studentInfoDetails.drivingLicense=true OR s.gender=male OR s.studentInfoDetails.smoker=true OR s.studentInfoDetails.willingToWork=true OR s.studentInfoDetails.workingForDisabledChild=true order by s.registrationDate desc
錯誤日誌:
org.hibernate.QueryParameterException: could not locate named parameter [studentCountry]
org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:148)
org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:165)
org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:523)
org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:493)
com.journaldev.spring.dao.HostSearchHistoryDAOImpl.addHostSearchHistory(HostSearchHistoryDAOImpl.java:82)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
我在做什麼錯了人。你能幫忙的話,我會很高興。非常感謝。 :-)
我喜歡第二個選項,我根本不使用參數。我會嘗試你的解決方案,請等待。你的意思是我不需要使用query.setParameter,如果我使用你的最後一個方法。 –
該死的東西工作我的朋友,只是一件事之前,我標記你的答案,你能告訴我如何可以進行搜索,因此它是不區分大小寫的,並尋找類似的東西。就像當用戶輸入國家名稱爲GerMAN時,德國在數據庫中是一個有效的國家,結果應該被返回。 –
嘗試像這樣'sb.append(「upper(s.studentCountry)='」)。append(hostSearchHistory.getCountry()。toUpperCase())。append(「'」)'。 –