在我的Postgres 9.1 数据库中,我有一个由ORM ( Hibernate ) 在以下类li
中映射的表: package ab; @Entity @Table(name = "li", schema = "public") @TypeDef(name="inet", typeClass=InetType.class) public class Li {Li
private long id;
private Date requestTime;
private String mac;
private String username;
private Serializable srcIp;
private Serializable dstIp;
private Short length;
private Short ipProtocol;
private Integer srcPort;
private Integer dstPort;
//..... ... some other related functions (getters, setters, etc)
}
现在,为了能够为Li
实体类创建条件查询,我创建了它的元模型:
package a.metamodel.b;
@StaticMetamodel(Li.class)
public class Li_ {
public static volatile SingularAttribute<Li, Long> id;
public static volatile SingularAttribute<Li, Date> requestDate;
public static volatile SingularAttribute<Li, String> mac;
public static volatile SingularAttribute<Li, String> username;
public static volatile SingularAttribute<Li, Serializable> srcIp;
public static volatile SingularAttribute<Li, Serializable> dstIp;
public static volatile SingularAttribute<Li, Short> length;
public static volatile SingularAttribute<Li, Short> ipProtocol;
public static volatile SingularAttribute<Li, Integer> srcPort;
public static volatile SingularAttribute<Li, Integer> dstPort;
}
因为我需要在li
从字段中提取的小时后搜索表requestDate
,所以我创建了以下postgres
函数:
create or replace function public.extract_hour(time_param timestamp with time zone)
returns double precision language sql as $function$
SELECT EXTRACT(hour from $1);
$function$;
到目前为止,一切都经过测试并且效果很好。所以我开始创建我的标准:
EntityManager em = ...
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Li> c = cb.createQuery(Li.class);
Root<Li> l = c.from(Li.class);
//pay attention here
Expression<Double> extractHour = cb.function("extract_hour", Double.class, l.get(Li_.requestDate));
c.where(cb.equal(extractHour, Double.parseDouble(aDynamicHourValue)));
TypedQuery<Li> q = em.createQuery(c);
List<Li> lis = q.getResultList();
当我运行上面的代码时,出现以下错误:
java.lang.NullPointerException
at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:146)
...............
当然,该错误来自该行:
Expression<Double> extractHour = cb.function("extract_hour", Double.class, l.get(Li_.requestDate));
有人可以帮我解决这个问题吗?