【问题标题】:Hibernate: No results when ran a query to get results between a date range休眠:运行查询以获取日期范围之间的结果时没有结果
【发布时间】:2017-04-29 19:38:48
【问题描述】:

我在我的 REST API 中使用了Hibernate 来完成数据库工作。我基本上有3层;

  1. JSON 服务层 - 这将处理 JSON 请求
  2. 服务层 - 这将处理对从数据库层发送或获取的数据的任何操作
  3. 数据库层 - 与数据库一起使用。保存、更新等功能。

请检查以下代码

JSON 服务层

@GET
    @Path("/getByTimeRange/{fromDate}/{toDate}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Measurements> getByTimeRange(@PathParam("fromDate") String fromDate, @PathParam("toDate") String toDate)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        MeasurementsService measurementsService = new MeasurementsService();
        List<Measurements> byTimeRange = null;
        try {
            byTimeRange = measurementsService.getByTimeRange(sdf.parse(fromDate), sdf.parse(toDate));
        } catch (ParseException ex) {
            Logger.getLogger(MeasurementsJSONService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return byTimeRange;
    }

服务层

public List<Measurements> getByTimeRange(Date fromDate, Date toDate)
    {
        Session session = measurementsDAOInterface.openCurrentSession();
        Transaction transaction = null;
        List<Measurements> byTimeRange = null;

        try
        {
            transaction = measurementsDAOInterface.openTransaction(session);
            byTimeRange = measurementsDAOInterface.getByTimeRange(fromDate, toDate, session);
            transaction.commit();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            session.close();
        }

        return byTimeRange;
    }

数据库层

public List<Measurements> getByTimeRange(Date fromDate, Date toDate, Session session) 
    {
        Query query = session.createQuery("FROM Units where last_updated BETWEEN :fromDate and :toDate");
        query.setParameter("fromDate", fromDate);
        query.setParameter("toDate", toDate);
        List<Measurements> list = query.list();

        System.out.println("Size "+list.size() );
        System.out.println("Dates "+toDate +" : "+ fromDate);

        for(int i=0;i<list.size();i++)
        {
            System.out.println("Item: "+list.get(i).getLastUpdated());
        }

        return list;
    }

所以基本上我要做的是检索属于特定日期范围的数据集 - 假设在 14-02-2016 到 14-09-2016 之间。在Hibernate,我使用了下面的代码

FROM Units where last_updated BETWEEN :fromDate and :toDate

Units 是表,last_updated 是列名。

无论如何,我总是没有结果,只是空白。甚至数据库层内的代码System.out.println("Size "+list.size() ); 也会打印出0

这里有什么问题?

【问题讨论】:

    标签: java mysql json hibernate rest


    【解决方案1】:

    你能把 Units 类放在这里吗?
    您在 Units 中有 last_updated 名称的成员吗?
    你在数据库中有数据吗?
    如果您在数据库中运行正常的 SQL 查询,它有什么结果?

    这里我给你一个带日期的Between示例。

    @Override
    public List<Auto> getAutosFechaVenta(Calendar fechaDesde, Calendar fechaHasta) {
    
        EntityManager em = null;
        List<Auto> listaAutos = null;
    
        try {
            em = emf.createEntityManager();
            em.getTransaction().begin();
            String autosFechabetween = "from Auto a where fechaVenta between :desde and :hasta ";
            Query query = em.createQuery(autosFechabetween);
            query.setParameter("desde", fechaDesde);
            query.setParameter("hasta", fechaHasta);
    
            listaAutos = query.getResultList();
    
        } catch (Exception e) {
        } finally {
            try {
                if (em != null) {
                    em.close();
                }
            } catch (Exception e) {
            }
    
        }
        return listaAutos;
    }
    

    什么时候开始交易?查看我的代码: em.getTransaction().begin();

    也许你可以试试 query.getResultList();

    我希望这个例子可以帮助你。让我知道你的结果。

    【讨论】:

    • 感谢您的回复。 ''ast_updated' 是列名。在“单位”类中,它是“lastUpdated”。无论如何,如果我以前的查询都使用了这些列名,那么我就是。
    • 我想我找到了这个案子。我叫错了桌子!但是让我检查一下。
    • 是的,这就是问题所在。表名不正确。非常感谢您为我提供支持。当您在Units 表中专门询问last_updated 时,我发现了错误。来自我的 +1。
    • 很高兴为您提供帮助并且您找到了解决方案!
    【解决方案2】:

    问题是我在查询中使用了不正确的表名。应该是FROM Measurements where last_updated BETWEEN :fromDate and :toDate

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2023-02-21
      • 2014-09-15
      • 2019-06-19
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多