Pages

22 December 2011

iBatis with Spring: "JavaBeansDataExchange could not instantiate result class" Exception

The below exception trace occurs when trying to select records from database to create an instance of an entity or collection of entities. As a result of not providing default constructor on the entity class.

I gave a sample entity implementation that has a constructor with string parameter and commented out default constructor. After running iBatis with Spring's framework, iBatis cannot find the default constructor and print the example stack trace. This is also pointing the basics of entity beans in Java EE, "Entity Beans must have a public default constructor".

TRACE
[2011-12-22 10:28:23,863] 00000522 DEBUG java.sql.Connection - {conn-100087} Preparing Statement:    SELECT * FROM DEMO.SAMPLE_TABLE   WHERE ID = ?  
[2011-12-22 10:28:23,863] 00000522 DEBUG java.sql.PreparedStatement - {pstm-100088} Executing Statement:    SELECT * FROM DEMO.SAMPLE_TABLE   WHERE ID = ? 
[2011-12-22 10:28:23,863] 00000522 DEBUG java.sql.PreparedStatement - {pstm-100088} Parameters: [123]
[2011-12-22 10:28:23,863] 00000522 DEBUG java.sql.PreparedStatement - {pstm-100088} Types: [java.lang.String]
[2011-12-22 10:28:23,865] 00000522 DEBUG java.sql.ResultSet - {rset-100089} ResultSet
[2011-12-22 10:28:25,812] 00000522 INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
[2011-12-22 10:28:26,865] 00000522 INFO  org.springframework.jdbc.support.SQLErrorCodesFactory - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
[2011-12-22 10:28:27,248] 00000522 ERROR org.springframework.jdbc.support.SQLStateSQLExceptionTranslator - SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];   
--- The error occurred in Sample-sqlMap.xml.  
--- The error occurred while applying a result map.  
--- Check the SampleSqlMap.SampleEntityRM.  
--- The error occured while instantiating the result object  
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: com.blogspot.oguzhan.strings.SampleEntity; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in Sample-sqlMap.xml.  
--- The error occurred while applying a result map.  
--- Check the SampleSqlMap.SampleEntityRM.  
--- The error occured while instantiating the result object  
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: com.blogspot.oguzhan.strings.SampleEntity  
org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];   
--- The error occurred in Sample-sqlMap.xml.  
--- The error occurred while applying a result map.  
--- Check the SampleSqlMap.SampleEntityRM.  
--- The error occured while instantiating the result object  
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: com.blogspot.oguzhan.strings.SampleEntity; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in Sample-sqlMap.xml.  
--- The error occurred while applying a result map.  
--- Check the SampleSqlMap.SampleEntityRM.  
--- The error occured while instantiating the result object  
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: com.blogspot.oguzhan.strings.SampleEntity
 at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:124)
 at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:322)
 at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:212)
 at org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult(SqlMapClientTemplate.java:249)
 at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:296)
 ...
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in Sample-sqlMap.xml.  
--- The error occurred while applying a result map.  
--- Check the SampleSqlMap.SampleEntityRM.  
--- The error occured while instantiating the result object  
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: com.blogspot.oguzhan.strings.SampleEntity
 at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:204)
 at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:139)
 at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:567)
 at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:541)
 at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
 at org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:298)
 at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
 ... 71 more
Caused by: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: com.blogspot.oguzhan.strings.SampleEntity
 at com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:108)
 at com.ibatis.sqlmap.engine.mapping.result.ResultMap.setResultObjectValues(ResultMap.java:371)
 at com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)
 at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:385)
 at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:300)
 at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:189)
 at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteQuery(MappedStatement.java:221)
 at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:189)
 ... 77 more
Caused by: java.lang.InstantiationException: com.blogspot.oguzhan.strings.SampleEntity
 at java.lang.Class.newInstanceImpl(Native Method)
 at java.lang.Class.newInstance(Class.java:1328)
 at com.ibatis.common.resources.Resources.instantiate(Resources.java:302)
 at com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectInternally(ResultObjectFactoryUtil.java:123)
 at com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactoryUtil.createObjectThroughFactory(ResultObjectFactoryUtil.java:91)
 at com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:106)
 ... 84 more

EXAMPLE ENTITY CLASS
import java.io.Serializable;

public class SampleEntity implements Serializable{

 private static final long serialVersionUID = 1L;
 
 private String id;
 // default constructor must exist 
 //public SampleEntity() { }
 
 public SampleEntity(String id) {
  super();
  this.id = id;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }
 
 @Override
 public String toString() {
  return "id=" + id;
 }
}

4 comments:

  1. Thanks a lot mo re` bhai........

    I had no default constructor declared....
    but after creating a default constructor, everything works fine.........

    u save my time.......
    may god save ur time.......

    ReplyDelete
  2. Thanks a lot too Oguzhan Acargil!I was with the same mistake. No default constructor.

    So, after found your post and declared default constructor in my Class, all works fine.

    ReplyDelete
  3. That was quite helpful .. thanks a lot ..

    ReplyDelete
  4. Sudhir Mohapatro

    Exlent yar

    ReplyDelete

Thx for reading! Comments are appreciated...