Stefan Langer wrote:
Warren wrote:
I am having problems with an n+1 sql map. I am getting the following error messages:

--- The error occurred while applying a result map. --- Check the CatItem.catItemsResults. --- Check the result mapping for the 'items' property. --- Cause: java.lang.NullPointerException

I have put the following test together:

Objects

TestCat object

private int catPK;
private String catName;
private List items;

TestItem object

private int itemPK;
private String itemName;

Database Tables

testitem table

itm_pk int4,
itm_cat_fk int4,
itm_name char(20),

testcat table

cat_pk int4,
cat_name char(20)

<resultMap id="catItemsResults" class="testCat" groupBy="catPK" >
<result property="catPK" column="cat_pk" javaType="int" jdbcType="INTEGER"/> <result property="catName" column="cat_name" javaType="string" jdbcType="CHAR"/> <result property="items" javaType="java.util.List" resultMap="CatItem.itemsResults" />
</resultMap>
  <resultMap id="itemsResults" class="testItem" >
<result property="itemName" column="itm_name" javaType="string" jdbcType="CHAR"/>
</resultMap>

<select id="getCatWithItems" resultMap="catItemsResults"
SELECT cat_pk, cat_name, itm_name FROM testcat LEFT JOIN testitem ON cat_pk = itm_cat_fk
</select>

I have looked at the FAQ, docs and mailing list archive and I can not figure out what I am doing wrong. I have tried it on a Postgres db with a postgres driver and a Sybase db with an ODBC driver. I still get the same results. I am running version 2.1.7 of Ibatis. What am I doing wrong?

Thanks,

Warren
Where does the nullpointer occur? In your code or in ibatis code?
When you run your query you will have a item in your items map even though the testCat does not contain any testItem. This item will have all null values. So if any of the methods in testItem throw a nullpointerexception when being fed with a null value that will probably be the case.

I just filed an enhancement request for Ibatis to prevent ibatis from creating such null objects. Feel free to vote or comment on it.
The issue is: IBATIS-375
The null pointer occurs in:

sqlMap.queryForList(statementName, parameterObject)

I should get a List of TestCat objects with a List of TestItems, shouldn't I? I couldn't quite follow your explanation of how TestCat and the items List are being populated. I think I understand that if a testCat does not have any testItems, the items List in testCat will still have a testItem in it with it's properties set to null, correct? But my query does not return any testCats without a testItem in it. My object setters are all very simple:

public void setCatName(String catName)
{
   this.catName = catName;
}

When I set the loggers to DEBUG I get the following log entries:

DEBUG [http-8080-Processor24] - Checked out connection 2100665 from pool.
DEBUG [http-8080-Processor24] - {conn-100186} Connection
DEBUG [http-8080-Processor24] - {pstm-100187} PreparedStatement: SELECT cat_pk, itm_pk, cat_name, itm_name FROM testcat LEFT JOIN testitem ON cat_pk = itm_cat_fk DEBUG [http-8080-Processor24] - {pstm-100187} Parameters: []
DEBUG [http-8080-Processor24] - {pstm-100187} Types: []
DEBUG [http-8080-Processor24] - {rset-100188} ResultSet
getBatchedItemDetails(Object batchedItems) EXCEPTION=com.ibatis.dao.client.DaoException: IBATIS PROBLEM Error executing query for list. Cause: com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred in com/clarks/spanky/persistence/sqlmapdao/sql/postgres/batchOrder-postgres.xml. --- The error occurred while applying a result map. --- Check the BatchOrder.catItemsResults. --- Check the result mapping for the 'items' property. --- Cause: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred in com/clarks/spanky/persistence/sqlmapdao/sql/postgres/batchOrder-postgres.xml. --- The error occurred while applying a result map. --- Check the BatchOrder.catItemsResults. --- Check the result mapping for the 'items' property. --- Cause: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
DEBUG [http-8080-Processor24] - Returned connection 2100665 to pool.

Is this telling me that the SELECT is not returning any records? I get the following results when I run the above logged SELECT directly on the db:

The SELECT query returns:

** cat_pk       cat_name        itm_name
1       Vitamins        Vitamin Item 3
1       Vitamins        Vitamin Item 1
1       Vitamins        Vitamin Item 2
2       Herbs   Herbs Item 3
2       Herbs   Herbs Item 2
2       Herbs   Herbs Item 1
2       Herbs   Herbs Item 4
3       Groceries       Grocery Item 2
3       Groceries       Grocery Item 3
3       Groceries       Grocery Item 1


This is on a Postgres db with a postgres driver. I have also changed the sqlMap to the following and I still get the same exception:

<resultMap id="catItemsResults" class="testCat" groupBy="catPK" >
<result property="catPK" column="cat_pk" javaType="int" jdbcType="NUMERIC" nullValue="0"/> <result property="catName" column="cat_name" javaType="string" jdbcType="CHAR" nullValue="NULL"/> <result property="items" javaType="java.util.List" resultMap="CatItem.itemsResults" />
</resultMap>

<resultMap id="itemsResults" class="testItem" >
<result property="itemPK" column="itm_pk" javaType="int" jdbcType="NUMERIC" nullValue="0"/> <result property="itemName" column="itm_name" javaType="string" jdbcType="CHAR" nullValue="NULL"/>
</resultMap>

<select id="getBatchedItemPromoPrices" resultMap="catItemsResults">
SELECT cat_pk, itm_pk, cat_name, itm_name FROM testcat LEFT JOIN testitem ON cat_pk = itm_cat_fk
</select>

Where is the NullPointerException coming from? I am not very experienced with Ibatis and do not know what is going on behind the scenes. This is all very frustrating.

Reply via email to