How do I use or config Mybatis default LongTypeHandler - mybatis

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

Related

org.xml.sax.SAXParseException : Content is not allowed in prolog

This is my project demo, I want to use MyBatis xml configuration. But I got a SAXParseException error, like this
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:260)
at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:126)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.<init>(XMLMapperBuilder.java:80)
at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:519)
... 56 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:994)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:258)
... 59 more
this is my mapper xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="com.ryanqy.mapper.UserMapper" xmlns="http://mybatis.org/schema/mybatis-mapper">
<resultMap id="userResultMap" type="com.ryanqy.entity.UserEntity">
<result column="username" property="username"/>
<result column="password" property="password"/>
</resultMap>
<select id="findUserByUserName" resultMap="userResultMap">
select * from uuc_user where username = #{userName}
</select>
</mapper>
Problem is with your mybatis config:
mybatis:
configuration:
map-underscore-to-camel-case: true
type-aliases-package: com.ryanqy.entity
mapper-locations: mapper
change mapper-locations to:
mapper-locations: mapper/UserMapper.xml
#OR
mapper-locations: mapper/*.xml
If you change, you receive error with missing DOCTYPE in your mapper XML, so add something like this
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

MyBatis issue: Unrecognized jdbcType

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

Included SQL fragment doesn't substitute parameter

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>

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>

How can i resolve this MULE_ERROR-109

I am performing a transformation and getting the following error:
ERROR 2013-10-02 12:38:19,763 [[vistaesb].VistaESBFlow1.stage1.04] org.mule.exception.DefaultMessagingExceptionStrategy:
Message : Failed to transform from "json" to "personal_information"
Code : MULE_ERROR-109
Exception stack is:
1. Unrecognized field "phone_number" (Class personal_information), not marked as ignorable
at [Source: java.io.InputStreamReader#ac7e4af; line: 2, column: 21] (through reference chain: personal_information["phone_number"]) (org.codehaus.jackson.map.exc.UnrecognizedPropertyException)
org.codehaus.jackson.map.exc.UnrecognizedPropertyException:53 (null)
2. Failed to transform from "json" to "personal_information" (org.mule.api.transformer.TransformerException)
org.mule.module.json.transformers.JsonToObject:136 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
MY configuration is simple enough:
<flow name="VistaESBFlow1" doc:name="VistaESBFlow1">
<jdbc-ee:inbound-endpoint queryKey="personal_information" responseTimeout="1000" encoding="UTF-8" mimeType="text/plain" queryTimeout="-1" pollingFrequency="10000" connector-ref="applyVista_dev" doc:name="Data Entry Point">
</jdbc-ee:inbound-endpoint>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<data-mapper:transform config-ref="new_mapping_grf" doc:name="DataMapper"/>
<json:json-to-object-transformer doc:name="JSON to Object" encoding="utf8" returnClass="personal_information" mimeType="text/plain"/>
<file:outbound-endpoint path="C:\Users\abrowning\Desktop\test" responseTimeout="10000" doc:name="File" encoding="utf8" mimeType="text/plain"/>
</flow>
There is a link to a similar problem here, 109 Error, but i don't think this has to do with my endpoint.
I'm guessing a 109 is a bush-league error, so nay help is appreciated.
The source of my issue was that I had a data mismatch in my get/set methods and after having written a lot of PHP over the last year, I over looked that.