Reputation: 125
I'm a newbie in Mybatis and I have a problem with getting data with @Many.
I have two classes, objects of the second class are values of first-class objects Set<>;
Classes are:
public class InformationObjectMergedWithCopyrights {
private Long id;
private String titleLt;
private String accountingNumber;
private String inventoryNumber;
private Long referenceYear;
private Long duration;
private String propertyRights;
private Set<BavicCredit> creditsList = new HashSet<>();
getters/setters, constructor, toString...
}
public class BavicCredit {
private Long id;
private Long informationObjectId;
private String bavicLabel;
private String bavicRole;
getters/setters, constructor, toString...
}
I have sql provider, which gnerates query String:
public class InformationObjectSqlProvider {
public String selectByInventoryNumberConditionAccountingNumber(InventoryNumberReportParamsDao reportParamsDao) {
StringBuilder sql = new StringBuilder("SELECT " +
"io.ID AS id, " +
"io.TITLE_LT AS titleLt, " +
"io.ACCOUNTING_NUMBER AS accountingNumber, " +
"io.INVENTORY_NUMBER AS inventoryNumber, " +
"io.REFERENCE_YEAR AS referenceYear, " +
"io.DURATION AS duration, " +
"cvt.NAME AS propertyRights " +
"FROM SCHEMA_ONE.INFORMATION_OBJECT io " +
"INNER JOIN SCHEMA_ONE.COPYRIGHTS c ON c.INFORMATION_OBJECT_ID = io.ID " +
"INNER JOIN SCHEMA_TWO.CLASSIFIER_VALUES cv2 ON c.PROPERTY_RIGHTS = cv2.CODE " +
"INNER JOIN SCHEMA_TWO.CLASSIFIER_VALUE_TRANSLATIONS cvt ON cv2.ID = cvt.CLASSIFIER_VALUE_ID AND cvt.LANGUAGE_ID = 1 " +
"WHERE io.INVENTORY_NUMBER = #{reportParamsDao.inventoryNumberCode} ORDER BY accountingNumber ASC");
return sql.toString();
}
}
And finally, I have created a mapper:
@Mapper
public interface InformationObjectMapper {
InformationObjectSqlProvider provider = new InformationObjectSqlProvider();
@SelectProvider(type = InformationObjectSqlProvider.class, method = "selectByInventoryNumberConditionAccountingNumber")
@Results(value = {
@Result(property = "creditsList",
column = "SCHEMA_ONE.INF_OBJ_CLASIFFIER.INFORMATION_OBJECT_ID",
javaType = Set.class, many = @Many(select = "selectCredits"))
})
List<InformationObjectMergedWithCopyrights> selectByInventoryNumberConditionAccountingNumber(@Param("reportParamsDao") InventoryNumberReportParamsDao reportParamsDao);
@Select("SELECT * FROM SCHEMA_ONE.INF_OBJ_CLASSIFIER ioc WHERE ioc.INFORMATION_OBJECT_ID = #{informationObjetId} AND ioc.\"TYPE\" = 'CREDIT' ")
@Results(value = {
@Result(property = "bavicLabel", column = "BAVIC_LABEL"),
@Result(property = "bavicRole", column = "ROLE")
})
Set<BavicCredit> selectCredits(@Param("informationObjetId") Long informationObjetId);
}
My problem is that this code doesn't return Set of BavicCredit classes. Set is always empty, just like defined in the first class. Seems like the method selectCredits isn't called at all. The foreign key exists in the database. I believe, that I'm missing some small code or property mapping.
Java 11, Spring Boot 2.5.0, Mybatis mybatis-spring-boot-starter 2.2.0, database Oracle com.oracle.database.jdbc ojdbc8
Thanks in advance.
PS I have tried One to many relationship in MyBatis but still missing something
Upvotes: 0
Views: 493
Reputation: 3594
When using a nested select [1], the value of the column
attribute should be a column name in the result set of the first query.
In the result set of your first query, the parameter to the nested query is id
, so the @Result
should look as follows.
@Result(
property = "creditsList",
column = "id",
javaType = Set.class,
many = @Many(select = "selectCredits"))
[1] For the basics, please read this section of the doc.
Upvotes: 1