0

我试图在某个日期间隔内获取所有 MainIntervals。但我总是为空。

这是 JPA 实体

@Table(name="MAIN_INTERVAL")
 @NamedQueries({
 @NamedQuery(name = MainInterval.FIND_ALL, query = " select m from MainInterval m"),
 @NamedQuery(name = MainInterval.FIND_BETWEEN, 
    query = "select m from MainInterval m where m.mainIntervalStart 
    BETWEEN :startDate AND :endDate 
    AND m.mainIntervalEnd BETWEEN :startDate AND :endDate"
   )  }) 


public class MainInterval implements Serializable {

public static final String FIND_ALL = "MainInterval.findAll";
 public static final String FIND_BETWEEN = "MainInterval.findBetween";

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="MAIN_INTERVAL_ID")
private Long id;

@Column(name="MAIN_INTERVAL_START")
@Temporal(javax.persistence.TemporalType.DATE)
private Date mainIntervalStart;

@Column(name="MAIN_INTERVAL_END")
@Temporal(javax.persistence.TemporalType.DATE)
private Date mainIntervalEnd; }

在 EJB SessionBeans 我有方法:

public List<MainInterval> findMainIntervalsBetween(Date startDate, Date endDate){
      List<MainInterval> resultList = em.createNamedQuery(MainInterval.FIND_BETWEEN, MainInterval.class)
              .setParameter("startDate", startDate, TemporalType.DATE).setParameter("endDate", endDate, TemporalType.DATE).getResultList();
      return resultList;
}

但是当我使用 CDI 从 JSF 调用它时,结果列表始终为空。虽然我有一些 MainIntervals 满足 startDate 和 endDate 之间的条件。

我将非常感谢教程的每个答案或链接。最好的祝福!

4

1 回答 1

3

您的查询是:

select m from MainInterval m where m.mainIntervalStart 
BETWEEN :startDate AND :endDate 
AND m.mainIntervalEnd BETWEEN :startDate AND :endDate

第一行的间隔是 [01.05.2012, 31.05.2012],第二行的间隔是 [01.05.2012, 01.08.2012]。查询的参数是 10.05.2012, 20.05.2012。

因此,对于您的第一行:

m.mainIntervalStart BETWEEN :startDate AND :endDate 
01.05.2012          BETWEEN 10.05.2012 AND 20.05.2012 : false

所以第一行没有返回。

对于您的第二行:

m.mainIntervalStart BETWEEN :startDate AND :endDate 
01.05.2012          BETWEEN 10.05.2012 AND 20.05.2012 : false

所以第二行也不返回。

在我看来一切都很正常。

于 2012-05-19T16:58:38.173 回答