Search code examples
javaibatis

iBatis - load list of objects


I am quite new to iBatis (started today) and I am trying to query my database for a list of objects (returning single objects and values work just fine).

I am following THIS tutorial (with little modification - I have different names of my classes). Could you please tell me what am I doing wrong in this case?

This is my mapping for getting list of users:

    <!-- Retrieve all users -->
    <select id="getAll" resultClass="wa.myslima.refman.entities.User">
      SELECT * FROM USER
   </select>

When calling

rd = Resources.getResourceAsReader("SqlMapConfig.xml");

SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

users = (List<User>) smc.queryForList("User.getAll", null);

I get the following exception:

--- The error occurred in User.xml.  
--- The error occurred while applying a result map.  
--- Check the User.getAll-AutoResultMap.  
--- The error occured while instantiating the result object  
--- Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate result class.  Cause: java.lang.InstantiationException: wa.myslima.refman.entities.User

In some other tutorials, they use some additional mapping so I also tried this approach without success:

<resultMap id="UserResult" class="wa.myslima.entities.User">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="email" column="email"/>
  </resultMap>

  <select id="getAll" resultMap="UserResult">
    select * from USER
  </select>

Thanks for any tips. It is probably some silly mistake like typo but I still cannot see it through...

EDIT: User.java

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 1456239434165308496L;

    private int id;
    private String name;
    private String password;
    private String email;

    public User(String name, String password, String email) {
        this.name = name;
        this.password = password;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }
}

Solution

  • You will need a default constructor in your User class. So define it like:

    public User() {}
    

    This constructor will be used by Ibatis to create new User.