Exception with rownum in mybatis - mybatis

Following is my sql query that is used in mybatis mapper xml.
<select id="getData" fetchSize="30" resultType="java.util.HashMap" >
select * from table
where module='AB'
and rownum < 15
</select>
I am getting below exception while using rownum :
Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 130; columnNumber: 16; The content of elements must consist of well-formed character data or markup.
Below things I have tried:
ROWNUM<=15 AND <![CDATA[ ROWNUM <= 15 ]]>
But still it is not working.

Try this:
<select id="getData" fetchSize="30" resultType="java.util.HashMap" >
select * from table
where module='AB'
<![CDATA[ AND ROWNUM <= 15 ]]>
</select>
or ROWNUM <= 15 (with whitespaces after ROWNUM and before 15).

Are you sure you have tried the < or the <![CDATA[ ]]> on all the right places? (seems it's a large(r) file with perhaps multiple errors).
Since the code example you give is without the = and in the things you tried you add an =. And your error is on line 130 column 16 of your file, and we only see 5 lines and the < does not seem to be in column 16.
You could try to use: http://www.validome.org/xml/ and see if the entire configuration file is valid?
You can also read more about this on another question on Stack Overflow: https://stackoverflow.com/a/29136039/244748

Related

LIMIT in PostgresQL

I have query written in Oracle in MyBatis Mapper xml file like below:
<select id="getUserList" resultMap="userListResult" parameterType="org.user.UserNumber>
select * from user
WHERE (1=1)
<if test="num != null">AND rownum <= #{num}</if>
</select>
I need to make this Postgres Compliant. So, converted as below:
<select id="getUserList" resultMap="userListResult" parameterType="org.user.UserNumber>
select * from user
WHERE (1=1)
<if test="num != null">AND LIMIT #{num}</if>
</select>
But it is not working and throwing exception:
PSQLException: ERROR: syntax error at or near \"LIMIT\"\n
Can anyone help me please how to replace rownum here in Postgres?

Using "BETEEN" operator on a LONG-EMBEDDED-LIST property declared in OrientDB 2.2.17

I am using OrientDB 2.2.17 (enterprise edition) and I have a problem searching on the data:
I have a vertex called: EN70 with a property: EP70_4 from type 'embedded list = LONG'.
I added a record to this vertex with the value of [2,3,4] in this 'EP70_4' property.
The index on this property: Not_Unique (sbtree). [must support range queries]
SELECT * FROM EN70 WHERE (EP70_4 >= 1) is working and returns my record.
SELECT * FROM EN70 WHERE (EP70_4 <= 3) is working and returns my record.
The operator "Between" is not woking and throw exception:
SELECT * FROM EN70 WHERE (EP70_4 BETWEEN 1 AND 3 )
I tried to simulate a "between" operator:
SELECT * FROM EN70 WHERE ((EP70_4 >= 1) AND (EP70_4 <= 3)) IS NOT WORKING
but it is not working.
both sections 6 and 7 throws an exception:
Error on using index EN70.EP70_4 in query 'SELECT * FROM EN70 WHERE (EP70_4 BETWEEN 1 AND 3 )'. Probably you need to rebuild indexes. Now executing query using cluster scan
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long
at com.orientechnologies.common.serialization.types.OLongSerializer.preprocess(OLongSerializer.java:36)
at com.orientechnologies.orient.core.index.sbtree.local.OSBTree.iterateEntriesBetweenAscOrder(OSBTree.java:1474)
at com.orientechnologies.orient.core.index.sbtree.local.OSBTree.iterateEntriesBetween(OSBTree.java:771)
at com.orientechnologies.orient.core.index.engine.OSBTreeIndexEngine.iterateEntriesBetween(OSBTreeIndexEngine.java:185)
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.doIterateIndexEntriesBetween(OAbstractPaginatedStorage.java:2065)
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.iterateIndexEntriesBetween(OAbstractPaginatedStorage.java:2053)
at com.orientechnologies.orient.core.index.OIndexMultiValues.iterateEntriesBetween(OIndexMultiValues.java:275)
at com.orientechnologies.orient.core.index.OIndexAbstractDelegate.iterateEntriesBetween(OIndexAbstractDelegate.java:104)
at com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue.iterateEntriesBetween(OIndexTxAwareMultiValue.java:339)
at com.orientechnologies.orient.core.sql.operator.OQueryOperatorBetween.executeIndexQuery(OQueryOperatorBetween.java:131)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.searchForIndexes(OCommandExecutorSQLSelect.java:2184)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.searchInClasses(OCommandExecutorSQLSelect.java:1001)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLResultsetAbstract.assignTarget(OCommandExecutorSQLResultsetAbstract.java:209)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.assignTarget(OCommandExecutorSQLSelect.java:530)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.executeSearch(OCommandExecutorSQLSelect.java:512)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLSelect.execute(OCommandExecutorSQLSelect.java:488)
at com.orientechnologies.orient.core.sql.OCommandExecutorSQLDelegate.execute(OCommandExecutorSQLDelegate.java:74)
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.executeCommand(OAbstractPaginatedStorage.java:2624)
at com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage.command(OAbstractPaginatedStorage.java:2570)
at com.orientechnologies.orient.core.command.OCommandRequestTextAbstract.execute(OCommandRequestTextAbstract.java:69)
at com.orientechnologies.orient.server.network.protocol.http.command.post.OServerCommandPostCommand.execute(OServerCommandPostCommand.java:106)
at com.orientechnologies.orient.graph.server.command.OServerCommandPostCommandGraph.execute(OServerCommandPostCommandGraph.java:37)
at com.orientechnologies.orient.server.network.protocol.http.ONetworkProtocolHttpAbstract.service(ONetworkProtocolHttpAbstract.java:169)
at com.orientechnologies.orient.server.network.protocol.http.ONetworkProtocolHttpAbstract.execute(ONetworkProtocolHttpAbstract.java:622)
at com.orientechnologies.common.thread.OSoftThread.run(OSoftThread.java:77)
What can it be??? Please help me...
I'd say it's a bug, I strongly suggest you to report it here: https://github.com/orientechnologies/orientdb/issues

How to replace '?' in openjpa query with a real parameters?

Is there any way to get replaced '?' in printed out by openjpa query?
TRACE openjpa.Query - Executing query: Query: org.apache.openjpa.kernel.QueryImpl#246923;
TRACE openjpa.jdbc.SQL
SELECT * FROM TEST_TABLE WHERE ID = ? AND ROWNUM <= ?
[params=(long) 111, (long) 5001]
I verified different combinations with following entries:
< entry key="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE, Query=TRACE, JDBC=TRACE"/>
and
< entry key="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true" />
but it seems to be not working, according to expectation.
Is there any way to get replaced '?' in printed out by openjpa query?
I assume you want something like this : SELECT * FROM TEST_TABLE WHERE ID = 111 AND ROWNUM <= 5001? If so, no that is not possible. The reason for this is that OpenJPA will always favor setting parameters. Bummer if that isn't what you are looking for.

mybatis return result map with duplicate entry

I am using mybatis to retrieve data from DB, the data returned is containing duplicate entries.
Required result : Column Name , Value
Expected result is : column1 value A
But returned result is : COLUMN1 value A , column1 value A.
Hope able to clarify my doubt.
Can anybody tell me why it is happening?
<select id="getContentMap" resultType="map" parameterType="map">
select planId,location_qualifier from disclaimer_disclosure_content where
<choose>
<when test="plan_id != null">
plan_id = #{plan_id}
</when>
<when test="product_id != null">
product_id = #{product_id}
</when>
<otherwise>
issuer_id = #{issuer_id}
</otherwise>
</choose>
and effective_date >= #{effective_date}
and location_qualifier LIKE CONCAT('%' , #{location_qualifier} , '%')
</select>
The issue you are seeing is a bug in MyBatis 3 up until release 3.0.6: http://code.google.com/p/mybatis/issues/detail?id=303.
After that release you get the answer I outlined in my other answer (which was done with MyBatis 3.1.1).
You have four options:
Just ignore it and only grab the uppercase or lowercase entries
Upgrade to at least 3.0.6
Stop using map as resultType and move to a POJO domain object
Use the workaround below:
workaround for MyBatis < 3.0.6
Use full uppercase column aliases and they will only show up once (as uppercase) in your map:
<select id="getContentMap" resultType="map" parameterType="map">
select plan_id as PLAN_ID, location_qualifier as LOCATION_QUALIFIER from disclaimer_disclosure_content
where
<!-- SNIP: is the same as you had -->
</select>
This results in the output:
{PLAN_ID=2, LOCATION_QUALIFIER=Bar}
(or something similar depending on exactly how your select looks).
You will probably need to report more information, such as:
what database are you using?
what version of MyBatis 3 are you using (or are you still using iBATIS)?
what does your table structure look like?
In any case, I tried out a slightly simplified version of your query using MySQL 5.1 and MyBatis-3.1.1 and it worked fine - meaning that I only got back one entry of the column name in the result map. I provide my setup below so you can try to reproduce it or diagnose where your code may be wrong.
First, you have a error in your select statement. You have
SELECT planId
but then you have:
WHERE ... plan_id = #{plan_id}
so you probably meant SELECT plan_id in the SELECT clause.
Here is what worked for me.
My slightly simplified MyBatis select mapping is:
<select id="getContentMap" resultType="map" parameterType="map">
SELECT plan_id, location_qualifier FROM disclaimer_disclosure_content
WHERE
<choose>
<when test="plan_id != null">
plan_id = #{plan_id}
</when>
<otherwise>
product_id = #{product_id}
</otherwise>
</choose>
AND location_qualifier LIKE CONCAT('%' , #{location_qualifier} , '%')
</select>
Second, my MySQL table for this query:
mysql> select * from disclaimer_disclosure_content;
+---------+--------------------+------------+
| plan_id | location_qualifier | product_id |
+---------+--------------------+------------+
| 1 | Foo | 101 |
| 2 | Bar | 102 |
| 3 | Baz | 103 |
| 4 | Quux | 104 |
+---------+--------------------+------------+
4 rows in set (0.01 sec)
Third, my Java code to use the mapping:
#Test
public void testForSO() throws Exception {
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("plan_id", 2);
paramMap.put("location_qualifier", "Ba");
List<Map<String,Object>> lmap = session.selectList("getContentMap", paramMap);
assertNotNull(lmap);
Map<String,Object> m = lmap.get(0);
assertNotNull(m);
System.out.println(m.toString());
}
This passes and prints out:
{location_qualifier=Bar, plan_id=2}
I also tried it with
Map<String,Object> m = session.selectOne("getContentMap", paramMap);
and get the same expected result.

Dynamic pivot - how to obtain column titles parametrically?

I wish to write a Query for SAP B1 (t-sql) that will list all Income and Expenses Items by total and month by month.
I have successfully written a Query using PIVOT, but I do not want the column headings to be hardcoded like: Jan-11, Feb-11, Mar-11 ... Dec-11.
Rather I want the column headings to be parametrically generated, so that if I input:
--------------------------------------
Query - Selection Criteria
--------------------------------------
Posting Date greater or equal 01.09.10
Posting Date smaller or equal 31.08.11
[OK] [Cancel]
the Query will generate the following columns:
Sep-10, Oct-10, Nov-10, ..... Aug-11
I guess DYNAMIC PIVOT can do the trick.
So, I modified one SQL obtained from another forum to suit my purpose, but it does not work. The error message I get is Incorrect Syntax near 20100901.
Could anybody help me locate my error?
Note: In SAP B1, '[%1]' is an input variable
Here's my query:
/*Section 1*/
DECLARE #listCol VARCHAR(2000)
DECLARE #query VARCHAR(4000)
-------------------------------------
/*Section 2*/
SELECT #listCol =
STUFF(
( SELECT DISTINCT '],[' + CONVERT(VARCHAR, MONTH(T0.RefDate), 102)
FROM JDT1
FOR XML PATH(''))
, 1, 2, '') + ']'
------------------------------------
/*Section 3*/
SET #query = '
SELECT * FROM
(
SELECT
T0.Account,
T1.GroupMask,
T1.AcctName,
MONTH(T0.RefDate) as [Month],
(T0.Debit - T0.Credit) as [Amount]
FROM dbo.JDT1 T0
JOIN dbo.OACT T1 ON T0.Account = T1.AcctCode
WHERE
T1.GroupMask IN (4,5,6,7) AND
T0.[Refdate] >= '[%1]' AND
T0.[Refdate] <= '[%2]'
) S
PIVOT
(
Sum(Amount)
FOR [Month] IN ('+#listCol+')
) AS pvt
'
--------------------------------------------
/*Section 4*/
EXECUTE (#query)
I don't know SAP, but a couple of things spring to mind:
It looks like you want #listCol to contain a collection of numbers within square brackets, for example [07],[08],[09].... However, your code appears not to put a [ at the start of this string.
Try replacing the lines
T0.[Refdate] >= '[%1]' AND
T0.[Refdate] <= '[%2]'
with
T0.[Refdate] >= ''[%1]'' AND
T0.[Refdate] <= ''[%2]''
(I also added a space before the AND in the first of these two lines while I was editing your question.)