它需要在 Criteria API 中创建带有日期的谓词,但仍然会出现异常。如何构建这样的谓词?
It needs to create Predicate with date in Criteria API, but still gets an exception. How to build such Predicate?
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);
}
异常:
java.lang.NullPointerException: 空
在 org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:123)
我用的是hibernate 5.2.10.Final版本。
根不为空:
元模型和实体在同一个包中:
积分_class:
package ...domain.points.model;
@StaticMetamodel(Points.class)
public class Points_ {
public static volatile SingularAttribute<Points, Date> createdOn;
....
}
点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;
......
}
元模型和实体在同一个包中吗? Points_
和 Points
你说是.
现在,让我们尝试用字符串字段名称替换元模型。
Predicate startPredicate = criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createdOn"), lowerLimit);
我只是想看看是否可行,然后继续搜索解决方案。
检查这个
我担心 Hibernate 有一个错误
https://hibernate.atlassian.net/browse/HHH-9259
我使用了硬编码字符串。我将创建一个 class PointTable
,其中包含 Point
中的所有字段。这些 class 可以在不同的包中。
点
package entities;
public class Point {
private Long id;
//Other fields.
@ID
@Column(PointTable.ID)
public Long getId() {
return id;
}
//Other getters and setters.
}
点表
package entities.table;
public class PointTable {
public static final String ID = "id";
public static final String CREATED_ON = "createdOn";
//Other fields.
}
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);
}
异常: java.lang.NullPointerException: 空 在 org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:123)
我用的是hibernate 5.2.10.Final版本。
根不为空:
元模型和实体在同一个包中:
积分_class:
package ...domain.points.model;
@StaticMetamodel(Points.class)
public class Points_ {
public static volatile SingularAttribute<Points, Date> createdOn;
....
}
点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;
......
}
元模型和实体在同一个包中吗? Points_
和 Points
你说是.
现在,让我们尝试用字符串字段名称替换元模型。
Predicate startPredicate = criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createdOn"), lowerLimit);
我只是想看看是否可行,然后继续搜索解决方案。
检查这个
我担心 Hibernate 有一个错误 https://hibernate.atlassian.net/browse/HHH-9259
我使用了硬编码字符串。我将创建一个 class PointTable
,其中包含 Point
中的所有字段。这些 class 可以在不同的包中。
点
package entities;
public class Point {
private Long id;
//Other fields.
@ID
@Column(PointTable.ID)
public Long getId() {
return id;
}
//Other getters and setters.
}
点表
package entities.table;
public class PointTable {
public static final String ID = "id";
public static final String CREATED_ON = "createdOn";
//Other fields.
}