question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

One to Many association in Lazy Loading mode

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
marcocaminiticommented, Jan 25, 2018

I have the same problem. Without nested query work fine but with nested query I have the same behavior

0reactions
mzoccocommented, Jan 26, 2018

Okay, thanks for the support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can we use LAZY LOADING on One-to-many Relationship in ...
Yes, of-course we can use, lazy loading in @OneToMany relationship. Just do this @OneToMany(fetch = FetchType.LAZY) @MapsId private ...
Read more >
Eager/Lazy Loading In Hibernate - Baeldung
Hibernate applies lazy loading approach on entities and associations by providing a proxy implementation of classes. Hibernate intercepts calls ...
Read more >
5 ways to initialize lazy associations and when to use them
Lazy loading of associations between entities is a well established best practice in JPA. Its main goal is to retrieve only the requested...
Read more >
Guide to Lazy Loading in Hibernate - HowToDoInJava
@OneToMany and @ManyToMany associations are defaulted to LAZY loading; and · @OneToOne and @ManyToOne are defaulted to EAGER loading. This is ...
Read more >
Extra Lazy Associations - ORM - Doctrine
Associations are marked as Lazy by default, which means the whole collection object for an association is populated the first time its accessed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found