ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeff Butler" <jeffgbut...@gmail.com>
Subject Re: N Plus 1 Mapping Always getting a null pointer
Date Wed, 19 Apr 2006 03:36:56 GMT
I'm glad to hear it works!

For the future you should know that having a getter that returns one data
type and a setter that takes a different data type is breaking the JavaBeans
specification and definitely causes problems in iBATIS and other systems.
The same is true for overloaded setters.  Keep the POJOs simple!

Jeff Butler


On 4/18/06, Eric Bauld <baulde@cpsc.ucalgary.ca> wrote:
>
> Using simple getters and setters fixed it. The class cast was happening
> in the getVotes method as I returned an Vote[]. I never would have
> figured that was the cause without help. I did not realize iBATIS was
> using the get method the way it was.
> That explains why the classcast was happening.
>
> Thanks a bunch.
>
> - Eric
>
> Jeff Butler wrote:
> > Hmmm...You can't cast a List to an array - so you must have meant
> > something else - were you calling the toArray() method and saving the
> > result as an array internally?
> >
> > In my testing I've observed that iBATIS will call the setVotes method
> > once with null, once again with an empty ArrayList.  Then it will
> > repeatedly call getVotes for all the remaining votes rows.  Note that
> > iBATIS does not compose the entire List and send it to the setVotes
> > method at one time.  The initial list will be empty, then the getVotes
> > method will be called repeatedly to add a Vote object one at a time.
> >
> > You best hope for success is to have a private List of votes, and
> > simple getters and setters.  Trying to do anything wierd in the
> > setters and getters will cause unpredictable results - like you're
> seeing.
> >
> > If you are still having trouble after making simple getters and
> > setters, then please send your BasicStakeholder class along with your
> > reply.
> >
> > Jeff Butler
> >
> >
> >
> >
> > On 4/18/06, *Eric Bauld* <baulde@cpsc.ucalgary.ca
> > <mailto:baulde@cpsc.ucalgary.ca>> wrote:
> >
> >     setVotes was casting to an array. But when I added a if(votes ==
> >     null){return;} for testing or removed all code in the method.
> >     and left it as
> >     setVotes(List votes){}
> >
> >     I would then get this ClassCastException(included below) if the
> setter
> >     method for setVotes has code in it or not.
> >
> >
> >     Its as if my resultMap for BasicVote is failing. As it would build
> >     the
> >     stakeholder object when I removed the
> >     <result property="votes" resultMap="Stakeholder.votes"/>
> >     from the stakeMap resultMap. I cannot see a problem in my resultMap,
> I
> >     have looked over it many times.
> >
> >     I am already using the BasicVote object in vote.xml(included
> >     below)  and
> >     I can get votes from the DB just fine.
> >     But its not working within stakeholder.xml
> >
> >     --------------Exception trace Follows:
> >     com.ibatis.common.jdbc.exception.NestedSQLException :
> >     --- The error occurred in config/maps/stakeholder.xml.
> >     --- The error occurred while applying a result map.
> >     --- Check the Stakeholder.stakeMap.
> >     --- The error happened while setting a property on the result
> object.
> >     --- Cause: java.lang.ClassCastException
> >     Caused by: java.lang.ClassCastException
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback
> (GeneralStatement.java:188)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList
> (GeneralStatement.java:123)
> >        at
> >     com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(
> SqlMapExecutorDelegate.java:610)
> >        at
> >     com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(
> SqlMapExecutorDelegate.java:584)
> >        at
> >     com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(
> SqlMapSessionImpl.java:101)
> >        at
> >     com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForList(
> SqlMapClientImpl.java:78)
> >        at
> >     rp.broker.StakeholderBroker.getStakeholders(StakeholderBroker.java
> :81)
> >        at
> >     test.rp.broker.TestStakeholderBroker.testGetStakeholders
> >     (TestStakeholderBroker.java:37)
> >        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >        at
> >     sun.reflect.NativeMethodAccessorImpl.invoke(
> NativeMethodAccessorImpl.java:39)
> >        at
> >     sun.reflect.DelegatingMethodAccessorImpl.invoke
> >     (DelegatingMethodAccessorImpl.java:25)
> >        at java.lang.reflect.Method.invoke(Method.java:324)
> >        at junit.framework.TestCase.runTest(TestCase.java:154)
> >        at junit.framework.TestCase.runBare(TestCase.java:127)
> >        at junit.framework.TestResult$1.protect(TestResult.java:106)
> >        at junit.framework.TestResult.runProtected(TestResult.java:124)
> >        at junit.framework.TestResult.run(TestResult.java:109)
> >        at junit.framework.TestCase.run (TestCase.java:118)
> >        at junit.framework.TestSuite.runTest(TestSuite.java:208)
> >        at junit.framework.TestSuite.run(TestSuite.java:203)
> >        at
> >     org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> RemoteTestRunner.java
> >     :478)
> >        at
> >     org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
> RemoteTestRunner.java:344)
> >        at
> >     org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
> RemoteTestRunner.java:196)
> >     Caused by: java.lang.ClassCastException
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setNestedResultMappingValue
> (BasicResultMap.java:383)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.applyNestedResultMap
> (BasicResultMap.java
> >     :369)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues
> (BasicResultMap.java:355)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject
> (RowHandlerCallback.java
> >     :63)
> >        at
> >     com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(
> SqlExecutor.java:395)
> >        at
> >     com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(
> SqlExecutor.java:185)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteQuery
> >     (GeneralStatement.java:205)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback
> (GeneralStatement.java:173)
> >        ... 22 more
> >
> >     Caused by:
> >     java.lang.ClassCastException
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setNestedResultMappingValue
> (BasicResultMap.java:383)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.applyNestedResultMap
> (BasicResultMap.java
> >     :369)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues
> (BasicResultMap.java:355)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject
> (RowHandlerCallback.java
> >     :63)
> >        at
> >     com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(
> SqlExecutor.java:395)
> >        at
> >     com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(
> SqlExecutor.java:185)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteQuery
> >     (GeneralStatement.java:205)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback
> (GeneralStatement.java:173)
> >        at
> >
> com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList
> >     (GeneralStatement.java:123)
> >        at
> >     com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(
> SqlMapExecutorDelegate.java:610)
> >        at
> >     com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(
> SqlMapExecutorDelegate.java
> >     :584)
> >        at
> >     com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(
> SqlMapSessionImpl.java:101)
> >        at
> >     com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForList(
> SqlMapClientImpl.java:78)
> >        at
> >     rp.broker.StakeholderBroker.getStakeholders
> >     (StakeholderBroker.java:81)
> >        at
> >     test.rp.broker.TestStakeholderBroker.testGetStakeholders(
> TestStakeholderBroker.java:37)
> >        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >        at
> >     sun.reflect.NativeMethodAccessorImpl.invoke
> >     (NativeMethodAccessorImpl.java:39)
> >        at
> >     sun.reflect.DelegatingMethodAccessorImpl.invoke(
> DelegatingMethodAccessorImpl.java:25)
> >        at java.lang.reflect.Method.invoke(Method.java:324)
> >        at junit.framework.TestCase.runTest (TestCase.java:154)
> >        at junit.framework.TestCase.runBare(TestCase.java:127)
> >        at junit.framework.TestResult$1.protect(TestResult.java:106)
> >        at junit.framework.TestResult.runProtected(TestResult.java:124)
> >        at junit.framework.TestResult.run(TestResult.java:109)
> >        at junit.framework.TestCase.run(TestCase.java:118)
> >        at junit.framework.TestSuite.runTest(TestSuite.java:208)
> >        at junit.framework.TestSuite.run(TestSuite.java :203)
> >        at
> >     org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> RemoteTestRunner.java:478)
> >        at
> >     org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
> RemoteTestRunner.java:344)
> >        at
> >     org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
> >     (RemoteTestRunner.java:196)
> >
> >
> >     ---------Vote.xml-----------
> >     <?xml version="1.0" encoding="UTF-8" ?>
> >
> >     <!DOCTYPE sqlMap
> >     PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
> >     "http://www.ibatis.com/dtd/sql-map-2.dtd">
> >
> >     <sqlMap namespace="Vote">
> >
> >        <select id="getVotes" resultClass=" rp.object.baseImpl.BasicVote
> ">
> >            SELECT user_id AS userId,
> >                instance_id AS instanceId,
> >                requirement_id AS requirementId,
> >                criteria_id AS criteriaId,
> >                value AS voteValue
> >            FROM vote
> >            WHERE user_id = #userId#
> >            AND instance_id = #instanceId#
> >        </select>
> >
> >        <insert id="insertVotes"
> >     parameterClass="rp.object.baseImpl.BasicVote">
> >            INSERT INTO vote
> >     (user_id,instance_id,requirement_id,criteria_id,value)
> >            VALUES
> >     (#userId#,#instanceId#,#requirementId#,#criteriaId#,#voteValue#)
> >            ON DUPLICATE KEY UPDATE value=#voteValue#;
> >        </insert>
> >     </sqlMap>
> >
> >
>
>

Mime
View raw message