Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7552721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:51:15+00:00 2026-05-30T10:51:15+00:00

Say I have two domain objects and a mapper interface. class Person { int

  • 0

Say I have two domain objects and a mapper interface.

class Person {
    int id;
    List<Problem> problems = new ArrayList<Problem>();
}

class Problem {
    int id;
    Person person;
}

interface PersonMapper {
    public List<Person> selectAllPersons();
}

And two database tables.

create table person (
    id integer not null generated always as identity constraint person_pk primary key,
)

create table problem (
    id integer not null generated always as identity constraint problem_pk primary key,
    person_id integer not null constraint problem_person_fk references person
)

I can create a mapping file that gets the data I want.

<resultMap id="personMap" type="Person">
    <id column="person_id" property="id" />
    <collection column="problem_person_id" property="problems"
                javaType="ArrayList" ofType="Problem" resultMap="problemMap" />
</resultMap>

<resultMap id="problemMap" type="Problem">
    <id column="problem_id" property="id" />
    <!-- Adding an association here will cause a circular dependency -->
    <!-- The circular dependency results in a StackOverflowException -->
</resultMap>

<select id="selectAllPersons" resultMap="personMap">
    select
        person.id as person_id,
        problem.id as problem_id
    from person left outer join problem on person.id = problem.person_id
</select>

However, since MyBatis doesn’t do bi-directional mapping, none of the Problem objects in the returned collections will have their Person reference set correctly.

According to this issue, it sounds like I should be able to update my mapper interface and add a custom result handler that can be supplied by the calling class.

interface PersonMapper {
    public List<Person> selectAllPersons(ResultHandler handler);
}

class PersonResultHandler implements ResultHandler {
    @Override
    public void handleResult(ResultContext context) {
        System.out.println(context.getResultObject());
    }
}

class PersonDAO {
    // Get SqlSession sqlSession
    sqlSession.getMapper(PersonMapper.class).selectAllPersons(new PersonResultHandler());
}

However, the handleResult method of my ResultHandler never gets called. I’ve seen this example, but the extra fluff class in there makes it very difficult to understand. Can anyone give me a simple example of using a custom ResultHandler with mapper interfaces? I’m using MyBatis 3.0.5+.

I’ve also read through the MyBatis mailing list and there are several suggestions of using caching and lazy loading to solve circular dependencies, but I can’t find any examples of how to do it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:51:16+00:00Added an answer on May 30, 2026 at 10:51 am

    You should replace your method declaration to:

    interface PersonMapper {
        public void selectAllPersons(ResultHandler handler);
    }
    

    And populate List<Person> inside your PersonResultHandler

    class PersonResultHandler implements ResultHandler {
    
        List<Person> persons = new ArrayList<Person>();
    
        @Override
        public void handleResult(ResultContext context) {
            Object result = context.getResultObject();
            if (result instanceof Person) {
                Person person = (Person) result;
                for (Problem problem : person.getProblems()) {
                    problem.setPerson(person);
                }
                persons.add(person);
            }
        }
    
        public List<Person> getPersons() {
            return persons;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have two collections of integers: IEnumerable<int> col1=new List<int> {2,3,3,5,7,11,11,11,13}; IEnumerable<int> col2=new
Say I have two lists: var list1 = new int[] {1, 2, 3}; var
If I have two domain names: altcognito.com and say I've got the other following
Say we have two aggregate roots in a domain model: Group and User. Now,
I actually have two websites on the same domain. say, for example http://www.abc.com/flashsite &
I have a problem with uninitialized proxies in nhibernate The Domain Model Let's say
I have a domain class which has two dates in it and I want
Say I have two domain names: www.somesite.com and www.anothersite.com and both go to www.somesite.com
Say I have two strings, String s1 = AbBaCca; String s2 = bac; I
Say I have two tables I want to join. Categories: id name ---------- 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.