![](/img/trans.png)
[英]ORA-01465: invalid hex number or data mismatch error using hibernate jpa and oracle in coalesce
[英]JPA and Oracle DB: Distinct query with pagination leads to Error: ORA-00909: Invalid amount of arguments
我正在運行帶有 oracle 數據庫的 spring 后端。 我使用規范 API 來執行查詢:
@Slf4j
public class ContentSpecification implements Specification<Content> {
private final transient ContentFilter filter;
public ContentSpecification(final ContentFilter filter) {
this.filter = filter;
}
@Override
public Predicate toPredicate(
@NonNull final Root<Content> root,
@NonNull final CriteriaQuery<?> query,
@NonNull final CriteriaBuilder builder) {
final List<Predicate> predicates = new ArrayList<>();
if (!isEmpty(filter.getTerm())) {
// ... not relevant
predicates.add(builder.or(title, subtitle, body, keywords));
}
query.distinct(true);
return builder.and(predicates.toArray(new Predicate[0]));
}
}
public PagedModel<ContentModel> getContent(
final String term,
final Integer page,
final Integer size) {
final int pageSize = size == 0 ? MAX_VALUE : size;
return pagedAssembler
.toModel(
repository.findAll(
new ContentSpecification(
new ContentFilter(term)),
PageRequest.of(page, pageSize)),
contentAssembler);
}
這與 H2 和 MySQL 數據庫完美配合。 但是,一旦我使用 Oracle 數據庫,它將無法工作:
spring:
datasource:
driver-class-name: oracle.jdbc.OracleDriver
initialization-mode: never
url: <url>
username: <user>
password: <pw>
jpa:
database-platform: org.hibernate.dialect.Oracle12cDialect
hibernate:
ddl-auto: validate
properties:
hibernate:
format_sql: true
current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext
show-sql: true
我得到的控制台 output 如下:
2021-12-07 15:24:22.487 INFO [my-service,4d641a9f03674994,4d641a9f03674994,false] 14244 --- [nio-8082-exec-7] d.b.b.p.p.content.ContentController : get content: null, null, null, 0, 5
Hibernate:
select
*
from
( select
distinct content0_.id as id2_3_,
content0_.version as version3_3_,
content0_.status as status9_3_,
content0_.status_date as status_date10_3_,
content0_.subtitle as subtitle11_3_,
content0_.title as title12_3_,
from
content content0_
where
content0_.status=? )
where
rownum <= ?
Hibernate:
select
distinct count(distinct content0_.id,
content0_.version) as col_0_0_
from
content content0_
where
content0_.status=?
2021-12-07 15:24:22.508 WARN [my-service,4d641a9f03674994,4d641a9f03674994,false] 14244 --- [nio-8082-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 909, SQLState: 42000
2021-12-07 15:24:22.508 ERROR [my-service,4d641a9f03674994,4d641a9f03674994,false] 14244 --- [nio-8082-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-00909: Invalid amount of arguments
2021-12-07 15:24:22.514 ERROR [my-service,4d641a9f03674994,4d641a9f03674994,false] 14244 --- [nio-8082-exec-7] d.b.b.p.p.ApplicationExceptionHandler : runtime exception:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
...
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
... 111 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: ORA-00909: Ungültige Anzahl von Argumenten
at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:509) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:461) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1104) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:550) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:268) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:655) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:270) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:91) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:807) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:983) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1168) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3666) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.T4CPreparedStatement.executeInternal(T4CPreparedStatement.java:1426) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3713) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1167) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-3.4.2.jar:na]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
... 148 common frames omitted
Caused by: oracle.jdbc.OracleDatabaseException: ORA-00909: Ungültige Anzahl von Argumenten
at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:513) ~[ojdbc8-19.3.0.0.jar:19.3.0.0.0]
... 165 common frames omitted
一旦我刪除了distinct
的或pagination
,它也將與 oracle db 一起使用。
oracle 是否有解決方案可以與分頁一起運行?
編輯
所以看起來 oracle 計數查詢失敗。 只是為了比較,使用 H2 會生成以下 sql:
Hibernate:
select
distinct content0_.id as id2_3_,
content0_.version as version3_3_,
content0_.status as status9_3_,
content0_.status_date as status_10_3_,
content0_.subtitle as subtitl11_3_,
content0_.title as title12_3_,
from
content content0_
where
content0_.status=? limit ?
Hibernate:
select
distinct count(distinct (content0_.id,
content0_.version)) as col_0_0_
from
content content0_
where
content0_.status=?
錯誤ORA-00909: invalid number of arguments
是提供行數的語句
select
distinct count(distinct content0_.id,
content0_.version) as col_0_0_
from
content content0_
...
MySQL可能允許這種構造COUNT(DISTINCT expr,[expr...])
但在 Oracle 中是非法的。
由於您的方言是Oracle12cDialect
我懷疑在實體中存在多個標識列的情況下,這種方言的實施存在問題。 有關正確的 SQL 解決方案,請參見此處。
另請注意,此查詢中的第一個DISTINCT
是可能的,但完全不相關,因為帶有count
的查詢只會產生一行-
話雖如此,您應該 IMO 對您的數據 model 做出一些努力,這將使distinct
的使用變得不必要。 恕我直言,正確的方法。
實際上失敗的查詢:
select distinct count(distinct (content0_.id, content0_.version)) as col_0_0_ from content content0_ where content0_.status=? ```
是 spring-data 發出的查詢以獲取可用記錄的總量(請檢查:org.springframework.data.jpa.repository.support.SimpleJpaRepository#executeCountQuery - 實際上,值得提供完整的堆棧跟蹤)。 而且您的內容實體似乎具有復合 PK,即(id,版本)(同樣,沒有提供實體定義),但 HBN 在此類查詢中遇到了一些困難: https://hibernate.atlassian.net/browse/HHH-11042
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.