Oracle xmlsequence - java.sql.SQLException - java

I tried to use xmlsequence in this statement with datagrip:
select xmlsequence(extract(river, '/river/cities/*'))
from river_xml
where extractValue(river, '/river/name/text()')='Rhein';
the output was fine:
2020-06-18 19:09:36] 1 row retrieved starting from 1 in 38 ms (execution: 0 ms, fetching: 38 ms)
but from the select statement I got:
<failed to load>
java.sql.SQLException: Interner Fehler: makeJavaArray doesn't support type 2007
at oracle.sql.ArrayDescriptor.makeJavaArray(ArrayDescriptor.java:1075)
at oracle.jdbc.oracore.OracleTypeCOLLECTION.unpickle81ImgBodyElements(OracleTypeCOLLECTION.java:571)
at oracle.jdbc.oracore.OracleTypeCOLLECTION.unpickle81ImgBody(OracleTypeCOLLECTION.java:527)
at oracle.jdbc.oracore.OracleTypeCOLLECTION.unpickle81(OracleTypeCOLLECTION.java:339)
at oracle.jdbc.oracore.OracleTypeCOLLECTION.unlinearizeInternal(OracleTypeCOLLECTION.java:235)
at oracle.jdbc.oracore.OracleTypeCOLLECTION.unlinearize(OracleTypeCOLLECTION.java:214)
at oracle.sql.ArrayDescriptor.toJavaArray(ArrayDescriptor.java:790)
at oracle.sql.ARRAY.getArray(ARRAY.java:301)
in JdbcHelperImpl.wrapIfNeeded(JdbcHelperImpl.java:461)
I can't find this problem in the internet, so maybe someone here knows how I can solve this?
Thanks for your help

The xmlsequence, extract and extractvalue functions are all deprecated. At the moment you're getting a result which is a single collection of type XMLSEQUENCETYPE, with each element of the collection a city node. Presumably it's that collection type that DataGrip isn't happy about.
You can use xmltable instead, which will give you a result with one row per city:
select x.*
from river_xml r
cross join xmltable(
'/river[name="Rhein"]/cities/city'
passing r.river
columns city xmltype path '.'
) x;
You can adapt that to get information about the city in separate columns instead of as an XMLType value instead if you want; it depends what you do with the result.
db<>fiddle doesn't seem to know what to do with XMLSEQUENCETYPE either, which is fair enough; but you can see the output from the XMLTable query.

Related

operator does not exist: record = character varying in postgres

I'm getting below error while executing the following query using JPA native query
Error :
ERROR: operator does not exist: record = character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 228
Query:
select ar.adm_rule_id
from adm_promo.adm_rule ar,
adm_promo.adm_non_comp_rule ac,
adm_promo.adm_rule_hier_level ah
where ar.rule_type =?
and ar.sales_org_id=?
and (ac.promo_market_id,ah.level_id) in (?)
and ar.adm_rule_id=ac.adm_rule_id
and ar.adm_rule_id=ah.adm_rule_id
and ar.delete_f='N'
and (ar.eff_end_date>=(?) and ar.eff_start_date<=(?))
and ar.dist_channel=?
But I'm getting result for the same query with actual inputs, when I tried in PostgreSql
Query with inputs:
select ar.adm_rule_id
from adm_promo.adm_rule ar,
adm_promo.adm_non_comp_rule ac,
adm_promo.adm_rule_hier_level ah
where ar.rule_type =1
and ar.sales_org_id=1
and (ac.promo_market_id,ah.level_id) in ((9,63))
and ar.adm_rule_id=ac.adm_rule_id
and ar.adm_rule_id=ah.adm_rule_id
and ar.delete_f='N'
and (ar.eff_end_date>=('2020-09-29')
and ar.eff_start_date<=('2020-10-17'))
and ar.dist_channel=1
I couldn't find the issue exactly, team please help me to resolve this issue.
I suspect it has a problem with this:
(ac.promo_market_id,ah.level_id) in (?)
Try to cast it, as suggested in the error, by changing the query to:
(ac.promo_market_id,ah.level_id) in (?::record)
Judging by the fact that it says "position 228" and scrolling along to that character of your query puts you near this:
(ac.promo_market_id,ah.level_id) in ((9,63))
but your template for the query has this
(ac.promo_market_id,ah.level_id) in (?)
It might well be that you are providing the wrong sort of data there. It is unlikely that your database driver supports parameterized lists of values (to match your "IN" clause). You are probably ending up with some quoted text, which Postgres is treating as "character varying" which of course fails to match.

pagination using MySQL database and Java ResultSet

To implement a pagination on a list, I need to do two queries:
Get elements count from selected table using SELECT COUNT(*)...
Get subset of list using LIMIT and OFFSET in a query.
Are there any way to avoid this?. Are There any metadata where this is stored?
The function resultSet.getRow() retrive the array index of list, then I need to make a query whose results are all rows. After I get a subSet but this is expensive so.
I want send a only query with limits and offsets and retrive the selected datas and total count of datas.
is this possible?
Thanks in advance,
Juan
I saw some things about this, then new doubts are coming on to me.
When a query is lanched with limits, we can add SQL_CALC_FOUND_ROWS * on "select" section as follow:
"SELECT SQL_CALC_FOUND_ROWS * FROM ... LIMIT 0,10"
After, I query the follow:
"SELECT FOUND_ROWS()"
I understand that first query store count in a internal var whose value will be returned on second query. The second query isn't a "select count(*) ..." query so "SELECT FOUND_ROWS()" query should be inexpensive.
Am I right?
Some test that I have made show the follow:
--fist select count(*), second select with limits--
Test 1: 194 ms
out: {"total":94607,"list":["2 - 1397199600000","2 - 1397286000000","13 - 1398150000000","13 - 1398236400000","13 - 1398322800000","13 - 1398409200000","13 - 1398495600000","14 - 1398150000000","14 - 1398236400000","14 - 1398322800000"]}
--the new way--
Test 2: 555 ms
out: {"total":94607,"list":["2 - 1397199600000","2 - 1397286000000","13 - 1398150000000","13 - 1398236400000","13 - 1398322800000","13 - 1398409200000","13 - 1398495600000","14 - 1398150000000","14 - 1398236400000","14 - 1398322800000"]}
Why the test dont show the expected result?
My assumptions are wrong?
thanks, regards
I have resolve the question
The next link has got the response.
https://www.percona.com/blog/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/

Don't know how to subtract and Don't know how to compare error Cypher query in Java

I am trying to write a query that has a filter which includes some properties whose values are bigger than 0, and includes certain nodes where the difference between two properties is bigger than 0. However, with Neo4j v 2.1.6.
This is query is run in a java program through the embedded Neo4j engine.
My query looks something like this:
MATCH (fromJar)-[:IN]->(c)<-[:IN]-(toJar) WHERE c.status='ToDo' AND fromJar.content > 0 AND (toJar.volume - toJar.content) >0 RETURN ID(c) AS contextID, c.step, fromJar.name, fromJar.volume, fromJar.content, toJar.name, toJar.volume, toJar.content
However, I am getting the following error:
Exception in thread "main" org.neo4j.cypher.CypherTypeException: Don't know how to Subtract(Property(NodeIdentifier(),volume(8),Property(NodeIdentifier(),content(9))) `0` with `5`
Any ideas as to what is causing this? Or a how to work around it?
AS #DaveBennet suggested, you are probably setting your volume and content properties to string values. If you are, then this should "fix" your problem (also, note that the last term in the WHERE clause is simplified):
MATCH (fromJar)-[:IN]->(c)<-[:IN]-(toJar)
WHERE c.status='ToDo' AND TOINT(fromJar.content) > 0 AND
(TOINT(toJar.volume) > TOINT(toJar.content))
RETURN ID(c) AS contextID, c.step, fromJar.name, fromJar.volume,
fromJar.content, toJar.name, toJar.volume, toJar.content;
However, the above fix is not very performant, as it is constantly performing (what should really be unnecessary) type conversions. It would be better if you changed those properties so that they have numeric values. If you do, then then the query will look like this:
MATCH (fromJar)-[:IN]->(c)<-[:IN]-(toJar)
WHERE c.status='ToDo' AND fromJar.content > 0 AND (toJar.volume > toJar.content)
RETURN ID(c) AS contextID, c.step, fromJar.name, fromJar.volume,
fromJar.content, toJar.name, toJar.volume, toJar.content;
And, to further improve performance, the c node in your query should be labeled, and there should be an index for that label and the status property.

org.hibernate.NonUniqueResultException: query did not return a unique result: 2?

I have below code in my DAO:
String sql = "SELECT COUNT(*) FROM CustomerData " +
"WHERE custId = :custId AND deptId = :deptId";
Query query = session.createQuery(sql);
query.setParameter("custId", custId);
query.setParameter("deptId", deptId);
long count = (long) query.uniqueResult(); // ERROR THROWN HERE
Hibernate throws below exception at the marked line:
org.hibernate.NonUniqueResultException: query did not return a unique result:
I am not sure whats happening as count(*) will always return only one row.
Also when i run this query on db directly, it return result as 1. So whats the issue?
It seems like your query returns more than one result check the database. In documentation of query.uniqueResult() you can read:
Throws: org.hibernate.NonUniqueResultException - if there is more
than one matching result
If you want to avoid this error and still use unique result request, you can use this kind of workaround query.setMaxResults(1).uniqueResult();
Hibernate
Optional findTopByClientIdAndStatusOrderByCreateTimeDesc(Integer clientId, Integer status);
"findTop"!! The only one result!
I don't think other answers explained the key part: why "COUNT(*)" returns more than one result?
I just encountered the same issue today, and what I found out is that if you have another class extending the target mapped class (here "CustomerData"), Hibernate will do this magic.
Hope this will save some time for other unfortunate guys.
Generally This exception is thrown from Oracle when query result (which is stored in an Object in your case), can not be cast to the desired object.
for example when result is a
List<T>
and you're putting the result into a single T object.
In case of casting to long error, besides it is recommended to use wrapper classes so that all of your columns act the same, I guess a problem in transaction or query itself would cause this issue.
It means that the query you wrote returns more than one element(result) while your code expects a single result.
Received this error while doing otherwise correct hibernate queries. The issue was that when having a class extend another hibernate was counting both. This error can be "fixed" by adding a method to your repository class.
By overriding the class count you can manually determine the way this is counted.
#Override
public Integer count(Page<MyObject> page) {
// manual counting method here
}
I was using JPQL and wanted to return Map. In my case, the reason was that I wanted to get Map<String, String>, but had to expect List<Map<String, String>> :)
Check your table, where one entity occurring multiple time's.
I had the same error, with this data :
id
amount
clientid
createdate
expiredate
428
100
427
19/11/2021
19/12/2021
464
100
459
22/11/2021
22/12/2021
464
100
459
22/11/2021
22/12/2021
You see here clientid occurring two times with 464.
I solved it by deleting one row :
id
amount
clientid
createdate
expiredate
428
100
427
19/11/2021
19/12/2021
464
100
459
22/11/2021
22/12/2021
I have found the core of the problem:
result of SELECT COUNT(*) can be a list, if there is a GROUP BY in the query,
and sometimes Hibernate rewrite your HQL and put a GROUP BY into it, just for fun.
Basically your query returns more than one result set.
In API Docs uniqueResult() method says that
Convenience method to return a single instance that matches
the query, or null if the query returns no results
uniqueResult() method yield only single resultset
Could this exception be thrown during an unfinished transaction, where your application is attempting to create an entity with a duplicate field to the identifier you are using to try find a single entity?
In this case the new (duplicate) entity will not be visible in the database as the transaction won't have, and will never be committed to the db. The exception will still be thrown however.
Thought this might help to someone, it happens because "When the number of data queries is greater than 1".reference
As what Ian Wang said, I suspect you are using a repository from spring. And a few days ago you just copy past a class and forgot to delete it when it is finally unused. Check that repository, and see if there is multiple same class of table you use. The count is not the count of rows, but the count of the table problem.
This means that orm technology is not preprogrammed to give you which results you are looking for because there are too many of the same results in the database. for example If there is more than one same value in my database and I want to get it back, you will encounter the error you get with the select query.
For me the error is caused by
spring.jpa.hibernate.ddl-auto=update
in application.properties file replacing it with
spring.jpa.hibernate.ddl-auto=create solved the issue, but it still depends on your needs to decide which configuration you need in your project, for more insights on the topic check this.
First you must test the query list size; here a example:
long count;
if (query.list().size() > 0)
count=(long) criteria.list().get(0);
else
count=0;
return count;

Retrieving a max date from DB2 in Java throwing wrong column type conversion exception

I have the following SQL that returns the max BILL_DATE based on some criteria. BILL_DATE is defined in the database as a DATE.
SELECT MAX(BILL_DATE)
FROM BILLTABLE
WHERE col1 = ? and
col2 = ?
But when I read the value from the resultSet.
bill.setBillDate(resultSet.getDate(1));
An exception is thrown:
Invalid data conversion: Wrong result column type for requested conversion. ERRORCODE=-4461, SQLSTATE=42815
I have also tried
bill.setBillDate(resultSet.getString(1));
But that doesn't return a date. It returns either 100, 200 or 300 which is obviously not correct.
Is there another way to do this? Am I doing something wrong?
Ash is right, how are you defining the date column?
Is it possible the column is timestamp? In that case try resultSet.getTimestamp(1))
I had two resultSets open in the function where I was reading in the BILL_DATE. I changed the code to the following and it works fine.
bill.setBillDate(resultSet1.getDate(1));

Categories

Resources