0

I have a shipping information object that matches the table it is stored in in MySQL with the exception that the "Address" is an object (in the object) however it is an AddressId in the DB Table. My current result map looks like this:

<resultMap type="ShippingInfo" id="shippingInfoResult">
    <result column="orderItemId" property="orderItemId" />
    <result column="shipingDate" property="shippingDate" javaType="java.sql.Date"  />
    <result column="numberShipped" property="numberShipped" />
    <result column="carrier" property="carrier" />
    <result column="tracking" property="tracking" />
    <result column="shippingType" property="shippingType" />
    <association property="address" column="addressId" javaType="com.Foo.Bar.address.Address" select="com.Foo.Bar.address.AddressMapper.read" />
</resultMap>

I have been trying to figure out how to get MyBatis to select the address based on the addressId that is returned in the query but have as of yet been unsuccessful. It is currently setting the value of address to null rather than an empty Address object which I would expect it to do.

Is it possible to do this or do I need to format my query differently to include more data? Do I need to do something else different so that it will at least set the address field to an empty address rather than null?

Thank you for any assistance.

UPDATE I want to clarify, if there is an address that matches the addressId it pulls said address without issue and populates it. The issue I am running into is when the id is set to 0 and the query returns zero results. It appears to be setting the address to an empty address object for one instance and the rest are all set to null.

Ginto Hewoo
  • 73
  • 1
  • 1
  • 8

1 Answers1

0

See if it helps if you use this setting:

<setting name="callSettersOnNulls" value="true"/>
Ian Lim
  • 4,164
  • 3
  • 29
  • 43
  • Does adding this setting require a mapper as well? I am getting this when I add this to mybatis-config: `Failed to parse config resource: class path resource [mybatis/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: The setting callSettersOnNulls is not known. Make sure you spelled it correctly (case sensitive).` – Ginto Hewoo Oct 31 '13 at 18:06
  • May I know which mybatis version are u using? – Ian Lim Nov 01 '13 at 02:02
  • You may take a look at a sample mybatis-config.xml here https://code.google.com/p/mybatis/source/browse/trunk/src/test/java/org/apache/ibatis/submitted/call_setters_on_nulls/mybatis-config.xml?r=5459 – Ian Lim Nov 01 '13 at 07:07
  • I am using mybatis 3.1.1 I have looked at that link you posted, as that is what prompted my asking if it needed a mapper file, if it does, what exactly is that mapper file for? Do I need a mapper for all possible null values? Is the name for the mapper file used in that example required or just a name chosen for clarity? – Ginto Hewoo Nov 01 '13 at 16:50
  • Follow up after working on this for quite a while. The data structure is as follows: A single Order Object has one to many orderItems in it, each of which has a shippingInformation object which has an address in it. What appears to be happening is that at the deepest level, if the address is not set, it has an AddressId of 0, the query that is used to get said address is executed once and found to be an empty result set, the first orderItem -> shippingInfo -> address gets set to an empty address object, however each successive address object with an ID of 0 is set to null. – Ginto Hewoo Nov 01 '13 at 19:43
  • Ideally I would like to set an "if result set empty, set to empty object" parameter, but I have not been able to find examples of where this is done. – Ginto Hewoo Nov 01 '13 at 19:45