2017-10-04 146 views
0

我想從我的數據庫檢索對象,基於house_id月/年。我在SQLite數據庫中存儲完整日期。每個月應該有單一的結果。這是我的代碼:使用Criteria根據時間段從數據庫獲取對象?

public static Costs getCosts(Date lookUpDate, House house){ 
     Costs costs = null; 
     Session sess = mainApp.getSessionFactory().openSession(); 
     Transaction tx = null; 
     try { 
      tx = sess.beginTransaction(); 

      // create criteria builder 
      CriteriaBuilder builder = sess.getCriteriaBuilder(); 
      // create criteria 
      CriteriaQuery<Costs> query = builder.createQuery(Costs.class); 
      // specify criteria root 
      Root<Costs> root = query.from(Costs.class); 
      query.select(root).where(builder.and(builder.equal(root.get("house"), house), 
      builder.equal(root.get("date"), lookUpDate))); //where month/year equal to lookUpDate 
      costs = sess.createQuery(query).getSingleResult(); 

      tx.commit(); 
     } 
     catch (Exception e) { 

我該如何調整它,以便標準將尋找特定的月份和年份?

我真的在尋找解決方案,如何改變我的Criteria,以便它會查找特定的月份和年份,而不是選擇所有可能的成本列表,然後通過每個對象比較日期。

更新:根據弗朗索瓦LEPORCQ答案 工作代碼:

public static Costs getCosts(Date lookUpDate, House house) { 
     Costs costs = null; 

     Calendar myCalendar = Calendar.getInstance(); 
     myCalendar.setTime(lookUpDate); 
     myCalendar.set(Calendar.DAY_OF_MONTH, 1); 
     Date monthStart = new java.sql.Date(myCalendar.getTimeInMillis()); 
     myCalendar.add(Calendar.DAY_OF_MONTH, 
       (myCalendar.getMaximum(Calendar.DAY_OF_MONTH) - myCalendar.get(Calendar.DAY_OF_MONTH))); 
     Date monthEnd = new java.sql.Date(myCalendar.getTimeInMillis()); 

     System.out.println("Start: " + monthStart); 
     System.out.println("End: " + monthEnd); 
     Session sess = mainApp.getSessionFactory().openSession(); 
     Transaction tx = null; 
     try { 
      tx = sess.beginTransaction(); 

      // create criteria builder 
      CriteriaBuilder builder = sess.getCriteriaBuilder(); 
      // create criteria 
      CriteriaQuery<Costs> query = builder.createQuery(Costs.class); 
      // specify criteria root 
      Root<Costs> root = query.from(Costs.class); 
      query.select(root) 
        .where(builder.and(builder.equal(root.get("house"), house), 
          builder.greaterThanOrEqualTo(root.get("date"), monthStart), 
          builder.lessThanOrEqualTo(root.get("date"), monthEnd))); 
      List<Costs> costsList = sess.createQuery(query).getResultList(); 
      if(!costsList.isEmpty()){ 
       costs = costsList.get(0); 
      } 

      tx.commit(); 
     } catch (Exception e) { 
      if (tx != null) 
       tx.rollback(); 
      System.out.println(e); 
      throw e; 
     } finally { 
      sess.close(); 
     } 

     return costs; 

作爲lookUpDate參數我經過修改日期(LocalDate.now()),如果有已經存在這個月的任何記錄被檢查。

+0

從SQL的角度來看,你要值日期字段大於或等於該年 - 月的第一天並且小於下一年的第一個月的第一天。 –

+0

我明白了,但我不知道如何將其實施到標準中 – Alyona

回答

1

在僞代碼

lookupStartDate = 01/month/year 
lookUpEndDate = lookupStartDate + 1 month 

where date >= lookupStartDate and date < lookUpEndDate 

使用的java.util.Calendar一個月添加到您的起始日期

嘗試這樣的事情

... 

//I assume that the day of your lookupStartDate is the first day of month 
Calendar myCal = Calendar.getInstance(); 
myCal.setTime(lookUpStartDate);  
myCal.add(Calendar.MONTH, +1);  
Date lookUpEndDate = myCal.getTime(); 

... 

query.select(root).where(
    builder.and(
     builder.equal(root.get("house"), house), 
     builder.greaterThanOrEqualTo(root.get("date"), lookUpStartDate), 
     builder.lowerThan(root.get("date"), lookUpEndDate) 
    ) 
); 

... 
相關問題