You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Steve Johnson <st...@equilibrium.com> on 2008/09/27 17:15:09 UTC

Problem with textbook spring/ibatis query

Greetings,

 

I'm getting a NullPointerException deep down in iBATIS when I issue a
two-level query to build a LIST of sub-objects within a parent object.
I attached the iBATIS source to my IDE, and I can see that txManager is
null in SqlMapExecutorDelegate.endTransaction().  It is the reference to
that null object that is causing the exception.

 

I was originally using iBATIS 2.3.0, since that's what's available in
the central Maven repo.  When I hit this problem, I downloaded and tried
2.3.4.  It produces the same behavior.  I'm using Spring 2.5.5,
including its iBATIS integration.

 

What I'm doing is almost right out of the Pro Spring book, and is also
very similar to what's in the iBATIS docs.  Here's what I have:

 

My two objects:

 

public class PropertyGroup {

    private Integer id;

    private String propertyGroupType;

    private String entityType;

    private Integer entityId;

 

    private List<Property> properties;

 

    ... getters/setters...

}

 

public class Property {

    private Integer id;

    private Integer propertyGroupId;

    private String name;

    private String value;

 

    ... getters/setters...

}

 

My sqlMap:

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"

        "http://ibatis.apache.org/dtd/sql-map-2.dtd">

 

<sqlMap namespace="PropertyGroup">

 

    <typeAlias alias="PropertyGroup"
type="com.equilibrium.apv.common.ibatis.PropertyGroup"/>

    <typeAlias alias="Property"
type="com.equilibrium.apv.common.ibatis.Property"/>

 

    <resultMap id="propertyGroup" class="PropertyGroup">

        <result property="id" column="id"/>

        <result property="propertyGroupType"
column="property_group_type"/>

        <result property="entityType" column="entity_type"/>

        <result property="entityId" column="entity_id"/>

        <result property="properties" column="id"
select="getPropertiesByPropertyGroup"/> 

    </resultMap>

    

    <resultMap id="property" class="Property">

        <result property="id" column="id"/>

        <result property="propertyGroupId" column="property_group_id"/>

        <result property="name" column="name"/>

        <result property="value" column="value"/>

    </resultMap>

 

    <select id="getPropertyGroupByEntityId" resultMap="propertyGroup"
parameterClass="map">

        select * from property_group where entity_type = #type:VARCHAR#
and entity_id = #id:INTEGER#

    </select>

 

    <select id="getPropertyGroupByEntityIdAndType"
resultMap="propertyGroup" parameterClass="map">

        select * from property_group where entity_type = #type:VARCHAR#
and 

                  entity_id = #id:INTEGER# and property_group_type =
#groupType:VARCHAR#

    </select>

 

    <select id="getPropertiesByPropertyGroup" resultMap="property"
parameterClass="int">

        select * from property where property_group_id = #id:INTEGER#

    </select>

 

</sqlMap>

 

And finally, my DAO code:

 

public class PropertyGroupDAO extends SqlMapClientDaoSupport {

 

    public List<PropertyGroup> getByEntityId(String entityType, Integer
entityId)

    {

        Map params = new HashMap();

        params.put("type", entityType);

        params.put("id", entityId.toString());

        return
getSqlMapClientTemplate().queryForList("getPropertyGroupByEntityId",
params);

    }

 

    public List<PropertyGroup> getByEntityIdAndGroupType(String
entityType, Integer entityId, String groupType)

    {

        Map params = new HashMap();

        params.put("type", entityType);

        params.put("id", entityId.toString());

        params.put("groupType", groupType);

        return
getSqlMapClientTemplate().queryForList("getPropertyGroupByEntityId",
params);

    }

 

    public List<Property> getPropertiesByGroupId(Integer id)

    {

        return
getSqlMapClientTemplate().queryForList("getPropertiesByPropertyGroup",
id);

    }

}

 

The first two queries in the DAO both fail with this
NullPointerException.  The third query, which only queries the
sub-table, succeeds.  If I take the sub-query, the "properties"
property, out of the primary "propertyGroup" resultMap, the first two
queries succeed as well.

 

If anyone can help me figure out what's wrong here, I'd be very
appreciative.  

 

TIA and Take Care,

 

Steve