ed_*_*ann 5 java hibernate analytic-functions oracle10g hibernate-criteria
试图覆盖 Oracle10gDialect 并添加 over 和 partition 函数。我在hibernate.org 上阅读了有关如何覆盖方言的信息。我正在使用 Hibernate 4.1.7.Final 并且无法升级。我按照规定实现了它,但是我收到了这个错误。
15:21:21,353 WARN SqlExceptionHelper:143 - SQL Error: 907, SQLState: 42000
[10/8/13 15:21:21:354 CDT] 00000021 SystemOut O ORA-00907: missing right parenthesis
[10/8/13 15:21:21:354 CDT] 00000021 SystemOut O 15:21:21,354 ERROR SqlExceptionHelper:144 - ORA-00907: missing right parenthesis
Run Code Online (Sandbox Code Playgroud)
这是我构建的类:
package com.edmann.util;
import org.apache.log4j.Logger;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.function.StandardSQLFunction;
/**
* Because i need some analytic functions i am going to extend the
* oracle10gDialect so i can register my functions i want to use.
*
* @author Edward Mann
*
*/
public class Oracle10gDialectExtended extends Oracle10gDialect {
// get log4j handler
private static final Logger LOG = Logger
.getLogger(Oracle10gDialectExtended.class);
/**
* Override registerFunctions so that we can register the ones we want to
* use, then we will call the registerFunctions from the parent class.
*
*/
@Override
protected void registerFunctions() {
LOG.info("Trying to register custom functions");
registerFunction("over", new StandardSQLFunction("over"));
registerFunction("partition", new StandardSQLFunction("partition"));
super.registerFunctions();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在 hbm.xml 文件中的条目:
<property name="rank" type="integer" formula="(ROW_NUMBER() over(partition by ENTRY_NUMBER ORDER BY ENTRY_DATE DESC))"/>
Run Code Online (Sandbox Code Playgroud)
我面临的挑战是阻止休眠添加 this_。到分区。在我链接到的休眠论坛线程中,最后一个条目是一个有同样问题的人。我不确定这是否可以通过 Hibernate Criteria 实现。
谢谢你的帮助。
更新: 我找到了我添加的答案
registerKeyword("partition");
Run Code Online (Sandbox Code Playgroud)
对于 registerFunctions 方法中的 Oracle10gDialectExtended 类,它现在按预期工作。
所以关于我如何让它发挥作用的长答案。以下是我如何在休眠中使用 ROW_NUMBER() 窗口函数。
我不知道是否还有其他人会想做类似的事情,我的答案只是部分的。我提供的更新允许在对数据库的查询中正确调用分区依据,但我无法使用排名值来限制结果。我所做的以下代码更改允许我在查询中使用排名值。
已知限制
在上面提到的Oracle10gDialectExtended类中还需要添加两个方法
/**
* I need to override the getLimitString so that i can append the partition
* by column on the end. If partition is not found in the string then it
* will not be added.
*/
@Override
public String getLimitString(String sql, boolean hasOffset) {
// LOG.info("Using our getLimitString value");
sql = sql.trim();
String forUpdateClause = null;
boolean isForUpdate = false;
final int forUpdateIndex = sql.toLowerCase().lastIndexOf( "for update") ;
if ( forUpdateIndex > -1 ) {
// save 'for update ...' and then remove it
forUpdateClause = sql.substring( forUpdateIndex );
sql = sql.substring( 0, forUpdateIndex-1 );
isForUpdate = true;
}
String rank = "";
if (sql.contains("partition")) {
rank = findRank(sql);
}
StringBuilder pagingSelect = new StringBuilder( sql.length() + 100 );
if (hasOffset) {
pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
}
else {
pagingSelect.append("select * from ( ");
}
pagingSelect.append(sql);
if (hasOffset) {
pagingSelect.append(" ) row_ where rownum <= ? ");
pagingSelect.append(rank);
pagingSelect.append(") where rownum_ > ?");
}
else {
pagingSelect.append(" ) where rownum <= ?");
pagingSelect.append(rank);
}
if ( isForUpdate ) {
pagingSelect.append( " " );
pagingSelect.append( forUpdateClause );
}
return pagingSelect.toString();
}
/**
* Take our sql query find the partition line and pull off the hibernate
* generated alias name.
*
* @param sql
* @return String - sql with rank limit
*/
private String addRank(String sql) {
int partition = sql.indexOf("partition");
String rank = "";
if (partition != -1) {
int start = partition;
int end = sql.indexOf(',', start);
String aliasName = end == -1 ? sql.substring(start) : sql
.substring(start, end);
int last = aliasName.indexOf("as");
if (last != -1) {
rank = " AND "+aliasName.substring(last + 2)+ " = 1";
}
}
return rank;
}
Run Code Online (Sandbox Code Playgroud)
通过这些更改,我可以使用我的排名值,而无需更改条件查询中的其他任何内容。输出查询类似于以下内容:
SELECT * FROM (SELECT ...
ROW_NUMBER() over(partition BY this_.ENTRY_NUMBER ORDER BY this_.ENTRY_DATE DESC) AS formula1_22_,
... )
WHERE rownum <= 250 AND formula1_22_ =1
Run Code Online (Sandbox Code Playgroud)
更新:进行了更改,因为我没有测试偏移查询。排名被添加到查询的错误部分。采用原始 getLimitString 查询并在适当的情况下添加rank = 1 值会更容易。
归档时间: |
|
查看次数: |
3656 次 |
最近记录: |