我正在尝试在URL中检索多个变量,因此我可以向数据库发送查询以选择特定对象,但似乎它拾取第一个对象而其他对象为空。
@RequestMapping(value = "getGroup/{Name}/{StartDate}/{EndDate}/{Status_GroupID}", method = RequestMethod.GET)
public @ResponseBody List<GroupsDetails> getGroupList(
@PathVariable String Name, String StartDate, String EndDate,
Integer Status_GroupID) {
return musicStoreService.getGroupList(Name, StartDate, EndDate, Status_GroupID);
}
我收到的错误:
Hibernate: select groupsdeta0_.Status_GroupID as Status1_1_, groupsdeta0_.GroupsID as GroupsID1_, groupsdeta0_.EndDate as EndDate1_, groupsdeta0_.Name as Name1_, groupsdeta0_.StartDate as StartDate1_ from Groups groupsdeta0_ where groupsdeta0_.Name=Gamma and (groupsdeta0_.StartDate is null) and (groupsdeta0_.EndDate is null) and (groupsdeta0_.Status_GroupID is null)
Sep 12, 2014 2:41:40 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/ATS] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Gamma' in 'where clause'
该表名为Groups,它有行:Name,StartDate,EndDate,Status_GroupID和GroupsID
更新
我试图通过提供行详细信息来检索主键,但它无效。
@Override
public List<GroupsDetails> getGroupList(String Name, String StartDate, String EndDate, Integer Status_GroupID) {
SessionFactory sessionFactory=HibernateSessionManager.getSessionFactory();
Session session=sessionFactory.getCurrentSession();
Transaction transaction=session.beginTransaction();
try{
transaction.begin();
@SuppressWarnings("unchecked")
List<GroupsDetails> groupList=session.createSQLQuery("FROM Groups WHERE Name=" + Name + " AND StartDate=" + StartDate + " AND EndDate=" + EndDate + " AND Status_GroupID=" + Status_GroupID).list();
return groupList;
}finally{
session.close();
}
}
但它会抛出异常,即语法错误,即使我看不到语法错误。 错误是:
Hibernate: FROM Groups WHERE Name=Gamma AND StartDate=01-06-2014 AND EndDate=01-09-2014 AND Status_GroupID=1
Sep 12, 2014 3:35:51 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/ATS] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM Groups WHERE Name=Gamma AND StartDate=01-06-2014 AND EndDate=01-09-2014 AND' at line 1
答案 0 :(得分:0)
你需要拥有所有参数的@PathVariable。喜欢
public @ResponseBody List<GroupsDetails> getGroupList(
@PathVariable String Name, @PathVariable String StartDate, @PathVariable String EndDate,
@PathVariable Integer Status_GroupID)
答案 1 :(得分:0)
根据错误详情:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near
'FROM Groups WHERE Name=Gamma AND StartDate=01-06-2014 AND EndDate=01-09-2014 AND' at line 1
它清楚地表明您缺少引号,例如:Name=Gamma
应为Name='Gamma'
或者,您可以使用Parameters创建Query
,例如:
query = sess.createSQLQuery("SELECT * FROM Groups
WHERE NAME=:name AND StartDate=:startDate AND EndDate=:endDate
AND Status_GroupID=:status_GroupID").addEntity(Groups.class);
query.setString("name", name);
query.setDate("startDate", StartDate);
query.setString("endDate", EndDate);
query.setString("status_GroupID", Status_GroupID);
List<GroupsDetails> groupList = query.list();