使用条件基于时间段从数据库中获取对象?

时间:2017-10-04 14:55:38

标签: java sql hibernate date criteria

我想根据 house_id 月/年从我的数据库中检索对象。我在SQLite数据库中存储完整的日期。每个月应该有单一的结果。这是我的代码:

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会寻找具体的月份和年份?

我真的在寻找解决方案,如何更改我的Criteria,以便查找特定的月份和年份,而不是选择所有可能的费用列表,然后查看比较日期的每个对象。

更新 基于FrançoisLEPORCQ的工作代码答案:

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()),并检查本月是否已存在任何记录。

1 个答案:

答案 0 :(得分: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)
    )
);

...