You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MyBatis, Unable to create objects by constructor with collection as argument
I have immuatble classes, I am struct with the below error while instantiating objects.
Tables
CREATE TABLE employee (
id varchar NOT NULL,
"name" varchar NOT NULL
)
CREATE TABLE muzimil.address (
id varchar NOT NULL,
street varchar NOT NULL,
city varchar NOT NULL,
emp_id varchar NULL
)
Java Classes
@Data
@Builder
@AllArgsConstructor
public class Employee {
private final String id;
private final String name;
private final List<Address> addresses;
}
@Data
@Builder
@AllArgsConstructor
public class Address {
private final String id;
private final String street;
private final String city;
private final String employeeId;
}
MyBatis: Mapper
<select id="getById" resultMap="employeeResultMap">
select
emp.id as employeeId,
emp.name as name,
aa.id as addressId,
aa.street as street,
aa.city as city
from employee emp
join address aa
on emp.id=aa.emp_id
where emp.id = #{id};
</select>
<resultMap id="employeeResultMap" type="Employee">
<constructor>
<idArg column="employeeId" javaType="String" name="id"/>
<arg column="name" javaType="String" name="name"/>
<arg resultMap="addressMap" name="addresses"/>
</constructor>
</resultMap>
<resultMap id="addressMap" type="java.util.List">
<association property="addresses" javaType="Address">
<collection property="address" javaType="java.util.ArrayList" ofType="Address">
<constructor>
<idArg column="addressId" javaType="String" name="id"/>
<arg column="street" javaType="String" name="street"/>
<arg column="city" javaType="String" name="city"/>
<arg column="employeeId" javaType="String" name="employeeId"/>
</constructor>
</collection>
</association>
</resultMap>
Error:
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.UnsupportedOperationException
### The error may exist in file [/Users/mchavak/Documents/muzimil/POC/postgres/ibatis/build/resources/main/mybatis/mapper/BrandMapper.xml]
### The error may involve com.muzimi.postgres.ibatis.EmployeeMapper.getById
### The error occurred while handling results
### SQL: select emp.id as employeeId, emp.name as name, aa.id as addressId, aa.street as street, aa.city as city from employee emp join address aa on emp.id=aa.emp_id where emp.id = ?;
### Cause: java.lang.UnsupportedOperationException
The text was updated successfully, but these errors were encountered:
There may be several approaches.
I'm not sure if it meets your requirement, but the easiest I can think of is to create a mutable version of Employee class and convert it to immutable in your service layer or in ResultHandler.
MyBatis, Unable to create objects by constructor with collection as argument
I have immuatble classes, I am struct with the below error while instantiating objects.
Tables
Java Classes
MyBatis: Mapper
Error:
The text was updated successfully, but these errors were encountered: