One to Many association in Lazy Loading mode
See original GitHub issueI’m having some problems during the data retrieving of an entity (called Person) that contains a list of another entity (Telecom) in lazy loading mode using the XML MyBatis version.
MyBatis version
3.4.5
Database vendor and version
PostgreSQL 9.6
Test case or example project
Structure of the database: Tables: person, person_telecom (associative table between Person and Telecom), telecom
Database tables scripts:
CREATE TABLE person
(
id character varying(36) COLLATE pg_catalog."default" NOT NULL DEFAULT (uuid_generate_v1())::character varying,
CONSTRAINT person_pk PRIMARY KEY (id)
)
CREATE TABLE telecom
(
id character varying(36) COLLATE pg_catalog."default" NOT NULL DEFAULT (uuid_generate_v1())::character varying,
tlc_value character varying(50) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT telecom_pk PRIMARY KEY (id)
)
CREATE TABLE person_telecom
(
person_id character varying(36) COLLATE pg_catalog."default" NOT NULL,
telecom_id character varying(36) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT person_telecom_pk PRIMARY KEY (person_id, telecom_id),
CONSTRAINT person_telecom_personid FOREIGN KEY (person_id) REFERENCES person (id),
CONSTRAINT person_telecom_telecomid FOREIGN KEY (telecom_id) REFERENCES telecom (id)
)
Data example: Person: id:x
Telecom id:t1 value:123 id:t2 value:345
Person_Telecom person_id:x telecom_id:t1 person_id:x telecom_id:t2
Here is the person resultMap:
<resultMap id="personResultMap" type="Person">
<id column="p_id" jdbcType="VARCHAR" property="id" />
<collection property="telecom" column="t_telecom_id" ofType="Telecom" select="com.example.mapper.TelecomMapper.selectByPrimaryKey"/>
</resultMap>
<select id="selectByPrimaryKey" parameterType="String" resultMap="personResultMap">
SELECT
person.id as p_id, telecom.id as t_telecom_id
FROM person
LEFT JOIN person_telecom ON person_telecom.person_id = person.id
WHERE person.id = #{id,jdbcType=VARCHAR}
</select>
Here is the telecom resultMap with the selectByPrimaryKey method:
<resultMap id="telecomResultMap" type="Telecom">
<id column="tlc_id" jdbcType="VARCHAR" property="id" />
<result column="tlc_tlc_value" property="value" jdbcType="VARCHAR" />
</resultMap>
<select id="selectByPrimaryKey" parameterType="String" resultMap="telecomResultMap">
select
tlc.id, tlc.tlc_value
from telecom tlc
where tlc.id = #{id,jdbcType=VARCHAR}
</select>
The classes Person and Telecom are simple java beans; the class Person obviously contains an ArrayList of Telecom.
In our scenario there is a Person with two Telecom associated; using the code posted above the result is a Person with only one Telecom associated (the first).
Steps to reproduce
After DB creation and population:
PersonMapper.selectByPrimaryKey(personId);
Expected result
Java class Person instance:
Person x {
String id = x;
List<Telecom> telecoms = {(t1, 123), (t2, 345)}
}
Actual result
Person x {
String id = x;
List<Telecom> telecoms = {(t1, 123)}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
I have the same problem. Without nested query work fine but with nested query I have the same behavior
Okay, thanks for the support.