Reputation: 6411
In my Postgres 9.1 database I have a li
table that is mapped by ORM (Hibernate) in the following Li
class:
package a.b;
@Entity
@Table(name = "li", schema = "public")
@TypeDef(name="inet", typeClass=InetType.class)
public class 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)
}
Now, for being able to create a criteria query for Li
entity class, I created its metamodel:
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;
}
Because I need to search on li
table after hour extracted from requestDate
field, I created the following postgres
function:
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$;
Until now everithing tested and works great. So I started to create my criteria:
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();
When I run the code above I get the following error:
java.lang.NullPointerException
at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:146)
...............
of course, that error comes from that line:
Expression<Double> extractHour = cb.function("extract_hour", Double.class, l.get(Li_.requestDate));
Can anybody help me on solving that issue?
Upvotes: 0
Views: 580
Reputation: 6411
The real problem with my criteria query was the fact that I put Li
entity in a package, and its metamodel Li_
on another package. I moved Li_
on the same package with Li
and now it seems that this issue is gone.
Upvotes: 0
Reputation: 2563
Not sure but, in your entity the field is named requestTime
but in your MetaModel you named it requestDate
. I think that MetaModel works with Introspection so maybe your error comes from that Hibernate cannot locate the field named requestDate
as it does not exists.
Upvotes: 1