Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
<resultMap id="SandboxMap" type="Sandbox">
<id property="firstName" column="FIRSTNAME"/>
<result property="age" column="AGE"/>
<collection property="split" javaType="list" ofType="Split" column="AGE" select="selectSplit">
<id property="sandboxId" column="SANDBOX_ID"/>
</collection>
</resultMap>
<select id="selectSandbox" resultMap="SandboxMap">
SELECT * FROM SANDBOX
WHERE AGE=#{age}
</select>
<select id="selectSplit" parameterType="int" resultType="com.scibor.Split">
SELECT * FROM SPLIT
WHERE Split.SANDBOX_ID=#{age}
</select>
And I have in my Database some entries such that there is a Sandbox with Age 14 and there are a few Splits whose SANDBOX_ID is 14. There are no imposed constraints on any of the columns because it was not specified in the documentation that I had to do so, i.e. these are all simple data types, NUMBER
and VARCHAR
no notion of PRIMARY KEY
, NOT NULL
etc.
Within my code I then invoke:
Sandbox sandbox = session.selectOne("Sandbox.selectSandbox", 14);
This returns a Sandbox
object whose Name
and Age
fields are properly set, however, the array associated with splits
it's just an array of nulls. If I was to guarantee uniqueness and change collection
to association
, it would be the same thing, split
would just be null
.
I've been staring at this for some time and I have tried all sorts of values for column
in collection
tag, as well as various variants of the selectSplit
entry, but none of them work, they always return a null value for split
. More annoyingly, no exceptions are thrown so it seems that all of the SQL queries being generated are in fact valid (most likely the last #{age}
is itself null
).
Can someone please explain how I can retrieve an instance of Sandbox
with a List<Split>
populated using MyBatis? The documentation is helpful but I have followed the examples, I think exactly, yet I am still not getting the expected result.
It turns out that the difficulty was in this line:
<select id="selectSplit" parameterType="int" resultType="com.scibor.Split">
One of the things I had taken for granted was that the property names in Split do not match up with the names of the columns for the associated table. So instead of using a resultType
, I had to create the following:
<resultMap id="SplitMap" type="Split">
<id column="SANDBOX_ID" property="sandboxId"/>
</resultMap>
This resolved the issue.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.