1
    private CriteriaBuilder criteriaBuilder;
    private CriteriaQuery<Points> criteriaQuery;
    private Root<Points> root;

    @PostConstruct
    private void init() {
        criteriaBuilder = entityManager.getCriteriaBuilder();
        criteriaQuery = criteriaBuilder.createQuery(Points.class);
        root = criteriaQuery.from(entityManager.getMetamodel().entity(Points.class));
    }
    public void t(Date lowerLimit, Date upperLimit) {
      Predicate startPredicate = criteriaBuilder.greaterThanOrEqualTo(root.<Date>get(Points_.createdOn), lowerLimit);
    }

Exception: java.lang.NullPointerException: null at org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:123)

I use hibernate 5.2.10.Final version.

Root is not null: enter image description here

Metamodel and entity are in the same package: enter image description here

Points_ class:

package ...domain.points.model;
@StaticMetamodel(Points.class)
public class Points_ {
    public static volatile SingularAttribute<Points, Date> createdOn;
    ....
}

Points class:

package ...domain.points.model;
@Entity
@Table(name = "POINTS")
public class Points extends AuditingEntity {
...
}

AuditingEntity class:

@Audited
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class AuditingEntity implements Serializable {
    @CreatedDate
    @Column(name = "CREATED_ON")
    protected Date createdOn;
    ......
}
Stack Over
  • 143
  • 2
  • 12
  • Check again. Try to use Point.fieldName instead of metamodel. Add your stacktrace to the question and tell which version of hibernate you use. Please. – Yan Khonski Jun 30 '17 at 13:18
  • fieldName are the same, but @Entity Points extend other table, which have field createdOn, and I use hibernate 5.2.10.Final version. – Stack Over Jun 30 '17 at 13:27
  • You mean if your class Point does not extend anything, you can use metamodel? I always used Strings, see my updated answer with example. – Yan Khonski Jun 30 '17 at 13:29

1 Answers1

1

https://stackoverflow.com/a/20097800/4587961

Is metamodel and entity in the same package? Points_ and Points You say yes.

Now, let's try to replace metamodel with String field name.

 Predicate startPredicate = criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createdOn"), lowerLimit);

I just want to see if it works, and then continue to search a solution.

Check this https://stackoverflow.com/a/4256642/4587961


I am afraid there is a bug in Hibernate https://hibernate.atlassian.net/browse/HHH-9259

I used hardcoded Strings. I would create a class PointTable with all fields from Point. These classes can be in different packages.

Point

package entities;

public class Point {

    private Long id;
    //Other fields.

    @ID
    @Column(PointTable.ID)
    public Long getId() {
        return id;
    }

    //Other getters and setters.
}

PointTable

package entities.table;

public class PointTable {

    public static final String ID = "id";
    public static final String CREATED_ON = "createdOn";
    //Other fields.
}
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114