我从Hibernate开始, 我有以下代码:
public List<Table> getDaoByDate(Date valDate, boolean checkPast, Class daoClass) {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
CriteriaBuilder builder = factory.getCriteriaBuilder();
CriteriaQuery criteriaQuery = builder.createQuery(daoClass);
Root table = criteriaQuery.from(daoClass);
criteriaQuery.select(table);
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String[] let = { "A", "B", "CD", "D", "DB", "DN", "DR", "DV", "E", "ER", "H", "J", "K", "KR", "L", "M",
"N", "Q", "RE", "S", "W", "Z" };
List<String> valuesErtae = Arrays.asList(let);
In<Object> inLet = builder.in(table.get("let"));
for (String value : valuesErtae) {
inLet.value(value);
}
Predicate globalRestr = inLet;
if (!checkPast) {
In<Object> inAnnouncement = builder.in(table.get("FirstDate"));
inAnnouncement.value(sdf.format(valDate));
globalRestr = builder.and(globalRestr, inAnnouncement);
}
globalRestr = builder.and(globalRestr, builder.lt(builder.concat(builder.concat( builder.substring(table.get("TestDate"), 5, 4), builder.substring(table.get("TestDate"), 3, 2)), builder.substring(table.get("TestDate"), 1, 2)).as(Number.class), 20180219));
criteriaQuery.where(globalRestr);
TypedQuery q = em.createQuery(criteriaQuery);
List<Table> dao = null;
try {
dao = q.getResultList();
...
我加入后 globalRestr = builder.and(globalRestr,builder.lt(builder.concat(builder.concat(builder.substring(table.get(“TestDate”),5,4),builder.substring(table.get(“TestDate”)) ,3,2)),builder.substring(table.get(“TestDate”),1,2))。as(Number.class),20180219));
得到:
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '.' near line 1, column 501
我想要实现的目标是:
and substring(TestDate, 5,4)+ substring(TestDate, 3,2)+ substring(TestDate, 1,2) < '20180312'
添加此条件应该是什么方法? THX