My mapper interface looks like this.
public interface CommonMapper {
long selectSequenceNumber(String sequenceName);
}
And I prepared sql mapper looks like this.
<sql id="sequenceNumber">
<choose>
<when test="_databaseId == 'derby'">
VALUES NEXT VALUE FOR #{sequenceName}
</when>
<otherwise>
SELECT #{sequenceName}.NEXTVAL FROM DUAL
</otherwise>
</choose>
</sql>
<select id="selectSequenceNumber" resultType="_long">
<!--<include refid="....mapper.CommonMapper.sequenceNumber"/>-->
<include refid="sequenceNumber"/>
</select>
When I test mapper #{sequenceName} part is not substituted with given.
14:36:09.492 [main] DEBUG ....selectSequenceNumber - ==> Preparing: VALUES NEXT VALUE FOR ?
14:36:09.724 [main] ERROR ....PersistenceTests - failed to apply function for SqlSession
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "?" at line 1, column 23.
### The error may exist in .../CommonMapper.xml
### The error may involve ....CommonMapper.selectSequenceNumber
### The error occurred while executing a query
### SQL: VALUES NEXT VALUE FOR ?
### Cause: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "?" at line 1, column 23.
Is this normal? How can I fix this?
You need to use string substitution ${sequenceName} instead of setting the parameter with #{} for direct string placement in your queries (such as for giving object names, dynamic sql etc...)
<sql id="sequenceNumber">
<choose>
<when test="_databaseId == 'derby'">
VALUES NEXT VALUE FOR ${sequenceName}
</when>
<otherwise>
SELECT ${sequenceName}.NEXTVAL FROM DUAL
</otherwise>
</choose>
</sql>
<select id="selectSequenceNumber" resultType="_long">
<!--<include refid="....mapper.CommonMapper.sequenceNumber"/>-->
<include refid="sequenceNumber"/>
</select>
Related
I have a issues as below while comparing date in MyBatis as below
Caused by: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
Both of java 'applyDate' and Postgres 'org_info.apply_date' type are Date
Here is my configuration:
<sql id="searchCriteriaSql">
<where>enter code here
<if test="applyDate != null && applyDate != '' ">
<![CDATA[org_info.apply_date <= #{applyDate}]]>
</if>
<if test="ogrNm != null && ogrNm != '' ">
<bind name="ogrNmKey"
value="'%' + ogrNm + '%'" />
AND ( upper(org_info.org_nm) LIKE upper(#{ogrNmKey}))
</if>
</where>
</sql>
The root cause comes from
<![CDATA[org_info.apply_date <= #{applyDate}]]>
Please help to give answer
I'm getting following error while calling the oracle function...
org.springframework.jdbc.UncategorizedSQLException:
### Error querying database. Cause: java.sql.SQLException: Non supported SQL92 token at position: 1:
### The error may exist in file [C:\Users\pkr\rpt\bin\dbmappers\MyMapper.xml]
### The error may involve com.pkr.tpy.rpt.dao.MyDao.resetRecs-Inline
### The error occurred while setting parameters
### SQL: { ? = call actions.reset_recs(?, ?, ?, ?) }
### Cause: java.sql.SQLException: Non supported SQL92 token at position: 1:
; uncategorized SQLException for SQL []; SQL state [99999]; error code [17034]; Non supported SQL92 token at position: 1: ; nested exception is java.sql.SQLException: Non supported SQL92 token at position: 1:
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
at com.sun.proxy.$Proxy37.selectOne(Unknown Source)
Here is my mapper.xml
<select id="resetRecs" resultType="String" statementType="CALLABLE">
{
#{retVal,mode=OUT,jdbcType=VARCHAR} = call actions.reset_recs(#{xCode, jdbcType=DECIMAL,mode=IN},
#{yCode,jdbcType=DECIMAL,mode=IN},
#{xyzNum,jdbcType=DECIMAL,mode=IN},
#{src,jdbcType=VARCHAR,mode=IN})
}
</select>
DAO Interface:
String resetRecs(#Param("xCode") int xCode,
#Param("yCode") int yCode,
#Param("xyzNum") int xyzNum, #Param("xyzSrc") String xyzSource,
#Param("retVal") String retVal);
I'm using Mybatis 3.4.0, any help would be greatly appreciated.
I was able to fix this when I updated my mapper call statement in one line.
<select id="resetRecs" resultType="String" statementType="CALLABLE">
{#{retVal,mode=OUT,jdbcType=VARCHAR} = call actions.reset_recs(#{xCode, jdbcType=DECIMAL,mode=IN},#yCode,jdbcType=DECIMAL,mode=IN},#xyzNum,jdbcType=DECIMAL,mode=IN},#{src,jdbcType=VARCHAR,mode=IN})}
While trying to convert my app from using MyBatis [it was iBatis before], I've been getting the following error, in spite of trying to make so many different kinds of changes to the code: What am I doing wrong? Any help would be really really really great!!!
Error message:
`Caused by: org.springframework.jdbc.UncategorizedSQLException: ### Error querying database. Cause: com.ibm.db2.jcc.am.SqlException: [jcc][10271][10296][3.58.82] Unrecognized JDBC type: -10. ERRORCODE=-4228, SQLSTATE=null
`
`### The error may exist in Path-to-XML-File.XML`
`### The error may involve namespace.resultMap-name`
`### The error occurred while executing a query`
`### SQL: {call name-of-stored-proc(?)}`
`### Cause: com.ibm.db2.jcc.am.SqlException: [jcc][10271][10296][3.58.82] Unrecognized JDBC type: -10. ERRORCODE=-4228, SQLSTATE=null`
`; uncategorized SQLException for SQL []; SQL state [null]; error code [-4228]; [jcc][10271][10296][3.58.82] Unrecognized JDBC type: -10. ERRORCODE=-4228, SQLSTATE=null; nested exception is com.ibm.db2.jcc.am.SqlException: [jcc][10271][10296][3.58.82] Unrecognized JDBC type: -10. ERRORCODE=-4228, SQLSTATE=null`
XML File: `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Namespace-name">
<resultMap id="retrieveReports-results" type="Folder-structure-to-Java-file" autoMapping="true">
<result property="name" column="REPORT_NAME"/>
<result property="location" column="REPORT_DIRECTORY"/>
<result property="key" column="REPORT_KEY"/>
<result property="executable" column="EXECUTABLE"/>
<result property="commandLine" column="COMMAND_LINE"/>
<result property="commandLineFormat" column="COMMAND_LINE_FORMAT"/>
</resultMap>
<select id="retrieveReports" resultType="java.util.Map" statementType="CALLABLE">
{call prc_sel_reports(#{reports,jdbcType=CURSOR,javaType=java.sql.ResultSet,mode=OUT,resultMap=retrieveReports-results})}
</select>
</mapper>`
Java code:
try {
List < Report > reports = super.getSqlSession().selectList("retrieveReports");
if(log.isDebugEnabled()){
log.debug("Retrieved " + reports.size() + " reports, in method: retrieveReports()");
}
return reports;
// Attempt to catch different kinds of exceptions.
} catch (Exception e) {
throw new RuntimeException("Exception caught while trying to retrieve reports", e);
}
You have to specify a parameterType in the select statement, either map or custom class. An instance has to be passed on statement invocation.
resultType is not used, then irrelevant.
Procedure actually writes in a field of the "input" parameter passed to mybatis statement, and the statement does not return anything.
Mapper interface would be: void retrieveReports(Map<String, Object> params);
And your call:
Map<String, Object> params = new HashMap<String, Object>();
session.selectList("retrieveReports", params);
List<Report> reports = (List<Report>)params.get("reports");
Param may also be a custom type with property private List<Report> reports;.
I tried following if clause in MyBatis and got following exception please help me to identify the issue here..
public class Student{
private Integer studId;
private String name;
private String email;
private Date dob;
}
Mapping
<select id="searchStudent" parameterType="hashmap" resultMap="StudentResult">
<![CDATA[
SELECT * FROM STUDENTS
WHERE 1 = 1
<if test="studId != null">
AND STUD_ID= #{studId}
</if>
<if test="name != null">
AND NAME like #{name}
</if>
]]>
</select>
Exception I get:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test="studId != null">
AND STUD_ID= 1
</if>
<if test="name != null">
' at line 4
### The error may exist in StudentMapper.xml
### The error may involve com.maventest.mytest.StudentMapper.searchStudent-Inline
### The error occurred while setting parameters
### SQL: SELECT * FROM STUDENTS WHERE 1 = 1 <if test="studId != null"> AND STUD_ID= ? </if> <if test="name != null"> AND NAME like ? </if>
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'test="studId != null">
AND STUD_ID= 1
</if>
<if test="name != null">
' at line 4
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
Remove <![CDATA[ and ]]> as this escapes all the query and mybatis doesn't process it at all so if is passed verbatim as part of the query to database.
<select id="searchStudent" parameterType="hashmap" resultMap="StudentResult">
SELECT * FROM STUDENTS
WHERE 1 = 1
<if test="studId != null">
AND STUD_ID= #{studId}
</if>
<if test="name != null">
AND NAME like #{name}
</if>
</select>
CDATA sections are used to escape blocks of text containing characters that would otherwise be regarded as markup [ORACLE definition].
Sometimes, we need it, specially when we have a where condition with such markups : <, > , <> , etc.
<![CDATA[ SELECT * FROM STUDENTS WHERE 1 = 1 ]]>
<if test="studId != null">
<![CDATA[ AND STUD_ID= #{studId} ]]>
</if>
<if test="name != null">
**<![CDATA[ AND COD_PER <> 'ASSU' ]]>**
</if>
Add it just on the line with the markup problem also works.
I have a strange error in a MyBatis mapper where I can check if a parameter is null but I can't check the value of the same parameter;
Here is an excerpt from my mapper:
<parameterMap id="OUTOCriteria" type="Map">
<parameter property="name" javaType="String" />
<parameter property="parentOU" javaType="Long" />
<parameter property="type" javaType="Integer" />
<parameter property="statusTypeCurrent" javaType="Integer" />
</parameterMap>
Then later I do this check:
<if test="parentOU != null">
<if test="parentOU.compareTo(new Long(1))">
AND OU_STRC_ID ${parentOU.operator.sql} #{parentOU.value}
</if>
</if>
The first check doesn't throw any error, but the nested check gives me this error:
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression '!parentOU.compareTo(new Long(1))'. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "compareTo" failed for object parentOUEQUAL1 [java.lang.NoSuchMethodException: compareTo(java.lang.Long)] ### The error may exist in com/clearstream/iam/query/sqlmaps/OU.xml ### The error may involve ou.iamouDefaultQuery ### The error occurred while executing a query ### Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression '!parentOU.compareTo(new Long(1))'. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "compareTo" failed for object parentOUEQUAL1 [java.lang.NoSuchMethodException: compareTo(java.lang.Long)]
What would be the correct syntax to test the value of my Long parameter?
Try something like this:
<if test="parentOU != null and parentOU.longValue() == 1">
AND OU_STRC_ID ${parentOU.operator.sql} #{parentOU.value}
</if>