【问题标题】:Getting multiple variables in URL for JSON在 JSON 的 URL 中获取多个变量
【发布时间】:2014-11-06 17:29:55
【问题描述】:

我正在尝试检索 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

【问题讨论】:

    标签: mysql json hibernate url servlets


    【解决方案1】:

    根据错误详情:

    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();
    

    【讨论】:

      【解决方案2】:

      您需要在所有参数前面加上@PathVariable。喜欢

      public @ResponseBody List<GroupsDetails> getGroupList(
              @PathVariable String Name, @PathVariable String StartDate, @PathVariable String EndDate,
              @PathVariable Integer Status_GroupID)
      

      【讨论】:

      • 谢谢它的工作,但现在我有另一个问题。如果可以的话,你可以看看吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 2021-04-05
      • 1970-01-01
      • 2015-10-16
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多