我的问题很简单:我有这个问题:
SELECT * FROM TABLE t WHERE
(SELECT count(*) FROM TABLE tbis WHERE TRUNC(t.date) = TRUNC(tbis.date))>1 ;
字段日期是时间戳。
如何使用条件API执行此操作?
我有这样的东西(trunc()对我的db服务器有效,sql请求有效。):
Criteria crit = session.createCriteria(Myclass.class, "t");
crit.createAlias("t.date", "dateT");
DetachedCriteria subcrit = DetachedCriteria.forclass(MyClass.class, "tbis");
subcrit.createAlias("tbis.date", "dateTbis");
subcrit.add(Restrictions.sqlRestriction("TRUNC(dateT) = TRUNC(dateTbis)"));
subcrit.setProjection(Projections.count("id"));
crit .add(Subqueries.gt(1, subcrit));
return crit.list();
在日志中,我得到了: org.hibernate.QueryException:不是关联:date
我尝试了各种各样的东西,但无法让它发挥作用......
我试过了:
Criteria crit = session.createCriteria(Myclass.class, "t");
DetachedCriteria subcrit = DetachedCriteria.forclass(MyClass.class, "tbis");
subcrit.add(Restrictions.sqlRestriction("TRUNC(t.date) = TRUNC(tbis.date)"));
subcrit.setProjection(Projections.count("id"));
crit .add(Subqueries.gt(1, subcrit));
return crit.list();
生成的SQL条件是:
TRUNC(t.date) = TRUNC(tbis.date))
日志:
18:34:04,461 WARN [JDBCExceptionReporter] SQL Error: 904, SQLState: 42000
18:34:04,461 ERROR [JDBCExceptionReporter] ORA-00904: "T"."DATE": invalid identifier
18:34:04,461 WARN [CriteriaAdapter] list exception:
org.hibernate.exception.SQLGrammarException: could not execute query
另外
Criteria crit = session.createCriteria(Myclass.class, "t");
DetachedCriteria subcrit = DetachedCriteria.forclass(MyClass.class, "tbis");
subcrit.add(Restrictions.sqlRestriction("TRUNC({t}.date) = TRUNC({tbis}.date)"));
subcrit.setProjection(Projections.count("id"));
crit .add(Subqueries.gt(1, subcrit));
return crit.list();
条件成为:
TRUNC({t}.date) = TRUNC({tbis}.date))
和日志:
18:36:28,206 WARN [TxConnectionManager] Connection error occured: org.jboss.resource.connectionmanager.TxConnectionManager$TxConnectionEventListener@1b3febd3[state=NORMAL mc=org.jboss.resource.adapter.jdbc.xa.XAManagedConnection@4cb16baf handles=1 lastUse=1343284588160 permit=true trackByTx=true mcp=org.jboss.resource.connectionmanager.JBossManagedConnectionPool$OnePool@7f16f514 context=org.jboss.resource.connectionmanager.InternalManagedConnectionPool@3c34353b xaResource=org.jboss.resource.adapter.jdbc.xa.XAManagedConnection@4cb16baf txSync=null]
java.lang.NullPointerException
at oracle.jdbc.driver.T4C8Oall.getNumRows(T4C8Oall.java:1153)
[...]
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
甚至:
Criteria crit = session.createCriteria(Myclass.class, "t");
DetachedCriteria subcrit = DetachedCriteria.forclass(MyClass.class, "tbis");
subcrit.add(Restrictions.sqlRestriction("TRUNC({t.date}) = TRUNC({tbis.date})"));
subcrit.setProjection(Projections.count("id"));
crit .add(Subqueries.gt(1, subcrit));
return crit.list();
与之前的结果相同。
我认为标准无法检测子查询的别名......但我必须访问该值,任何解决方法?
答案 0 :(得分:0)
首先你可以试试,如果你不应该只在sqlRestriction的别名周围放置{}:
subcrit.add(Restrictions.sqlRestriction("TRUNC({t}.date) = TRUNC({tbis}.date)"));
如果您不想比较日期字段的一部分(如日期),但想查看整个日期字段是否相等,您可以让Hibernate进行这样的比较:
Restrictions.eq(t.date, tbis.date)
如果您想比较日期字段的一部分,我会推荐您this question.