MyBatis issue: Unrecognized jdbcType - mybatis

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;.

Related

How do I use or config Mybatis default LongTypeHandler

sql xml define
When I use Mybatis LongTypeHandler in resultMap, I got error bellow, I don't know why. Does anyone could help on this?
Thanks in advance.
<resultMap id="detail_siteTypeConfig" type="java.util.HashMap">
<result typeHandler="org.apache.ibatis.type.LongTypeHandler" javaType="java.lang.Long" property="siteID" column="siteid"/>
<result typeHandler="org.apache.ibatis.type.LongTypeHandler" javaType="java.lang.Long" property="siteType" column="sitetype"/>
<result typeHandler="com.webex.webapp.common.util.mybatis.StringEmptyTypeHandler" javaType="java.lang.String" property="itemName" column="itemname"/>
<result typeHandler="com.webex.webapp.common.util.mybatis.StringEmptyTypeHandler" javaType="java.lang.String" property="itemValue" column="itemvalue"/>
</resultMap>
<select id="xxxxxx" resultMap="detail_siteTypeConfig">
select siteid, sitetype, itemname, itemvalue from tablexxxx where siteid=#{siteId} and sitetype=#{siteType}
</select>
Error log
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'conf/dao/basic/SessionType_Audit.xml'. Cause: org.
apache.ibatis.type.TypeException: Unable to find a usable constructor for class org.apache.ibatis.type.LongTypeHandler
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:123)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:95)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:377)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)
... 22 more
Caused by: org.apache.ibatis.type.TypeException: Unable to find a usable constructor for class org.apache.ibatis.type.LongTypeHandler
at org.apache.ibatis.type.TypeHandlerRegistry.getInstance(TypeHandlerRegistry.java:457)
at org.apache.ibatis.builder.BaseBuilder.resolveTypeHandler(BaseBuilder.java:143)
at org.apache.ibatis.builder.MapperBuilderAssistant.buildResultMapping(MapperBuilderAssistant.java:427)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:393)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:280)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:254)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:246)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:119)
... 25 more
Caused by: java.lang.ClassCastException: org.apache.ibatis.type.LongTypeHandler cannot be cast to org.apache.ibatis.type.TypeHandler
at org.apache.ibatis.type.TypeHandlerRegistry.getInstance(TypeHandlerRegistry.java:455)
... 32 more

EclipseLink JPA 2.1 - Exception thrown when calling JPA native query and try to map the result to POJO

I am using EclipseLink 2.5.2 and is using the following orm.xml to map the result of native query to POJOs.
<named-native-query name="RelSumView.findFmkItem">
<query>
select item_file_name
, last_release_id
, last_release_version
from tableA
</query>
</named-native-query>
<sql-result-set-mapping name="FmkItemDtoMapping">
<constructor-result target-class="xxx.model.common.biz.dto.FmkItemDTO">
<column name="item_file_name" class="java.lang.String" />
<column name="last_release_id" class="java.lang.Integer" />
<column name="last_release_version" class="java.lang.String" />
</constructor-result>
</sql-result-set-mapping>
But in below DAO class:
public List<FmkItemDTO> getFmkReleaseItemsByEnvLabelId(int envLabelId) {
return getEntityManager().createNamedQuery("RelSumView.findFmkItem", "FmkItemDtoMapping").getResultList();
}
The following exception is thrown:
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-6042] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: A session name must be specified for
non-object-level queries. See the setSessionName(String) method.
Query: ResultSetMappingQuery(sql="RelSumView.findFmkItem")
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:480)
at xxx.model.common.dao.FmkReleaseDAO.getFmkReleaseItemsByEnvLabelId(FmkReleaseDAO.java:28)
I have no idea of what this exception is and on how to set the session name to the query.

Spring Mybatis Oracle function call

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})}

mybatis PersistenceException Mapper error

I don't know what is wrong.
Something is bad with mapper but everything looks good.
File UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.javacodegeeks.snippets.enterprise.UserMapper">
<select id="addUser" parameterType="int" resultType="com.javacodegeeks.snippets.enterprise.User">
insert into * from users where id = #{id}
</select>
<select id="getUser" parameterType="int" resultType="com.javacodegeeks.snippets.enterprise.User">
select * from com.javacodegeeks.snippets.enterprise.User where id = #{id}
</select>
</mapper>
I get this Error:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 70; Document root element "mapper", must match DOCTYPE root "null".
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:51)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:35)
at Main.do1(Main.java:105)
at Main.main(Main.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 70; Document root element "mapper", must match DOCTYPE root "null".
You can use following doc type. However I find some discrepancy in the code you have copied. Are you sure of INSERT and SELECT statements. I hope its copy mistake
<?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="com.javacodegeeks.snippets.enterprise.UserMapper">
<select id="addUser" parameterType="int" resultType="com.javacodegeeks.snippets.enterprise.User">
insert into * from users where id = #{id}
</select>
<select id="getUser" parameterType="int" resultType="com.javacodegeeks.snippets.enterprise.User">
select * from com.javacodegeeks.snippets.enterprise.User where id = #{id}
</select>
</mapper>

Spring MVC - nested exception is java.lang.RuntimeException: org.postgresql.util.PSQLException: ERROR: relation "userid" does not exist

I am making a simple spring MVC application that takes data from a view and sends it to a PostgreSQL database on my machine. I have been following tutorials and am not completely familiar with the bean configuration style of handling connection settings in the Data Access Objects. The error that returns when I attempt to post to the database is as follows:
HTTP Status 500 - Request processing failed; nested exception is java.lang.RuntimeException: org.postgresql.util.PSQLException: ERROR: relation "userid" does not exist
"userid" is my simple postgre table I'm using for testing.
My spring bean configuration file is this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="username" value="postgres" />
<property name="password" value="kittens" />
</bean>
This is the DAO handling the connection to the DB:
package com.ARSmvcTest.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.ARSmvcTest.dao.arsDAO;
import com.ARSmvcTest.models.ARS;
public class JDBCarsDAO implements arsDAO {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insert(ARS ars){
String sql = "INSERT INTO UserID " +
"(ID_, First_, Last_, Email_, Pwd_) VALUES (?, ?, ?, ?, ?)";
Connection conn = null;
try {
conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, ars.getID_());
ps.setString(2, ars.getFirst_());
ps.setString(3, ars.getLast_());
ps.setString(5, ars.getPwd_());
ps.setString(4, ars.getEmail_());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {}
}
}
}
// FIND ARS BY ID WILL GO HERE EVENTUALLY
}
The spring bean is passing data to the connection successfully as evidenced by this screenshot below:
http://i.imgur.com/Hb7O5Qi.png (apologies, new user and can't embed images)
Despite the dataSource object receiving connection properties from my above spring bean, I notice that the ConnectionProperties field is still null.
Is this what is causing my exception on the Insert attempt?
Lastly I will include a screenshot of the Exception and stack being displayed in browser at the moment of failure:
http://i.imgur.com/prj1HtY.png (apologies, can't embed images)
this exception is directly triggered from the database(-driver).
It tells you that the table named "userid" is not existing.
Please check:
the table name on database and your insert-statement
if the table is read and writabled for the user you use for your connection; you need to have grated the correct right to be able to see and write to the table
I hope this will help you as this seems to be the typical error for this kind of exception.