0

My service code here

public HashMap<String, Object> syncEmployees(Long updatedAt, String userId) {
    HashMap<String, Object> outputMap = new LinkedHashMap<String, Object>();
    **List<String>** outputEmployee = employeeDao.getEmployeeSyncDetails(updatedAt);

        System.out.println("\n\n Size : "+outputEmployee.size()+"\nOutput : "+outputEmployee);

    outputMap.put("employee", outputEmployee);
    return outputMap;
}

Dao code which is return type is list of string List(String)

**List<String>** getEmployeeSyncDetails(@Param("updatedAt") long updatedAt);

.XML mapper and query where method returns list of xyzModel

<resultMap id="xyzmap" type="xyzModel" >
        <result property="userId" column="user_id" />
        <result property="employeeCode" column="employee_code" />
        <result property="designationId" column="designation_id" />

    </resultMap>


<select id="getxyzDetails" resultMap="xyzmap">
    SELECT   
             user_id, ua.employee_code, designation_id
    FROM users
    WHERE  updated_at &gt; #{updatedAt} 
    ORDER BY  updated_at ASC
</select>

And output is like Size : 3 Output : [com.webapp.models.xyzModel@1567524c, com.webapp.models.xyzModel@7744c2cd, com.webapp.models.xyzModel@43515de7]

My question is how its is working? How xyzmodel is getting populated in list of String actually my problem is XML mapper return xyzModel but if I write any other model like UserModel or String or any other model it is not showing any error or execute perfectly with proper output. If we process this list then only he send error java.lang.ClassCastException

Sameer Kazi
  • 17,129
  • 2
  • 34
  • 46
  • 2
    Looks to me like you haven't overridden the `toString()` method of `xyzModel`. Each element of your `List` is the result of calling the `toString()` implementation provided by `Object` on your `xyzModel` instances. The list doesn't actually contain those instances. – JonK Feb 22 '16 at 11:13
  • actually my problem is XML mapper return xyzModel but if I write any other model like User or String or nay other its not showing any error or execute perfectly with proper output. If we process this list then only he send error – Sameer Kazi Feb 22 '16 at 12:20
  • My concern is not related to override toSting method – Sameer Kazi Feb 22 '16 at 12:29

1 Answers1

1

In runtime, there are no types for generics.

If you would write the code for getEmployeeSyncDetails yourself, your compiler would warn you about this.

Read here about type erasure in java: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

Yoav Gur
  • 1,366
  • 9
  • 15
  • Why is type erasure relevant here? This seems to be a case of the author misinterpreting what the contents of a `List` object actually are. – JonK Feb 22 '16 at 11:47
  • perhaps I misunderstood the question, but according to my understanding, he asks how come although the method actually returns a list of xyzModel, it can be stored in a variable of type List. – Yoav Gur Feb 22 '16 at 11:58
  • And that's where the misunderstanding is. The list *does* contain Strings. You can't put an object that isn't a String into a List that specifies its type to be String. The code may say `list.add(new xyzModel());`, but the compiler will change it to `list.add(new xyzModel().toString());`. If you haven't provided an implementation for `toString()` you get the default implementation from `Object`, which is the class name followed by the @ symbol followed by the hex representation of that object's hash code. It's this that you can see in the List – JonK Feb 22 '16 at 12:11
  • actually my problem is XML mapper return xyzModel but if I write any other model like User or String or nay other its not showing any error or execute perfectly with proper output. If we process this list then only he send error – Sameer Kazi Feb 22 '16 at 12:20
  • You didn't mention an exception in your original question. Sharing the stacktrace would help a great deal. – Yoav Gur Feb 22 '16 at 12:47
  • Your comment on the original question is correct @JonK but your above explanation of what the compiler does is incorrect. The list DOES contain xyzModel classes as you can see from the output of the list. Type erasure is the actual issue. The way that mybatis works is that the resultmaps are calculated and results bound according to them on the runtime. Hence you lose the compile type generics check. If you change the dao of one of your current mappers to List as the OP has and run it you can verify this behavior. Or ask me I can happily provide one – h3adache Apr 26 '17 at 15:57