有人知道为什么下面的sqlRestriction
不起作用吗。我使用本机Oracletrunc(..)
函数来修剪DATE的时间部分。
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Publication.class);
// Some other, non-SQL restrictions...
criteria.add(Restrictions.sqlRestriction("trunc(pubmedImportDate) >= ?",
todayMinus24Months,
org.hibernate.type.StandardBasicTypes.DATE));
错误:由以下原因引起:java.sql.SQLSyntaxErrorException:ORA-00904:"PUBMEDIMPORTDATE":无效标识符
查询基于域类Publication
,该域类具有此字段
public class Publication implements java.io.Serializable {
//...
private Date pubmedImportDate;
//..
public Date getPubmedImportDate() {
return this.pubmedImportDate;
}
public void setPubmedImportDate(Date pubmedImportDate) {
this.pubmedImportDate = pubmedImportDate;
}
}
我需要在pubmedImportDate
前面加一些别名吗?我看到pubmedImportDate
从未链接到Hibernate的别名this_
。
这两种我都试过了:
Criteria criteria = session.createCriteria(Publication.class);
Criteria criteria = session.createCriteria(Publication.class, "p"); // with p.pubmedImportDate
但问题总是这样:
from
PUBLICATIONS_T this_
...
trunc(p.pubmedImportDate) >= ?
如何将其连接到this_
?
我找到了解决方案:必须使用{alias}.column_name
将其连接到this_
:
criteria.add(Restrictions.sqlRestriction("trunc({alias}.pubmed_import_date) >= ?",
todayMinus24Months,
org.hibernate.type.StandardBasicTypes.DATE));
还要注意,它必须是真实的DB列名(pubmed_import_date
(,而不是类属性(pubmedImportDate
(。