我正在尝试在一个包含带有日期的列(Oracle 11G)的模型中进行标准查询(JPA / Hibernate)。例如,我有
我的功能就像:
public MyEntity getEntitiesFromCertainFilters(int a, int b, java.util.date c) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery< MyEntity > query = builder.createQuery(MyEntity.class); Root< MyEntity > root = query.from(MyEntity.class); query.select(root).where ( builder.equal ( root.get ( "id" ).get ( "codEstablec" ) , establecimiento), builder.equal ( root.get ( "id" ).get ( "correlGrupo" ) , correlGrupo), //HERE I NEED TO ADD FILTER BY C.MONTH AND C.YEAR FROM THE DATE ATTRIBUTE ); List < MyEntity > resultList = entityManager.createQuery(query).getResultList(); return resultList.get(0); }
如果我想按c =“ 01-JAN-09”进行过滤,则应返回:
任何帮助将不胜感激,在此先感谢。
您可以使用builder.function方法。例如
query.select(root).where ( builder.equal ( root.get ( "id" ).get ( "codEstablec" ) , establecimiento), builder.equal ( root.get ( "id" ).get ( "correlGrupo" ) , correlGrupo), builder.equal(builder.function("YEAR", Integer.class, root.get("DATE_FIELD") ), C.YEAR), builder.equal(builder.function("MONTH", Integer.class, root.get("DATE_FIELD")), C.MONTH) );