y_yo43
@y_yo43

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

MybatisでLocalTime型のレコードコンポーネントを持つRecordクラスへ値をマッピングする方法

Q&A

Closed

解決したいこと

TimeSectionクラスを単位として、時間の情報を取得しようとしています。
TimeSectionクラスはプロジェクトコード、ID、range(時間範囲)のフィールドを持っています。
Mybatisを利用して時間の範囲をrangeフィールドへマッピングしたいのですが、TimeSectionMapper.xmlのselectを実行すると下記のように、rangeのみnullになってしまいます。

結果
projectCode:xxx
timeSectionId:1
range:null

DBには値が入っていることは確認できました。
MybatisでLocalTime型のレコードコンポーネントを持つRecordクラスへ上手く値をマッピングするにはどうしたらいいでしょうか?
以下コードの一部です。

TimeRange.java
import java.time.Duration;
import java.time.LocalTime;

public record TimeRange(LocalTime start, LocalTime end) {

    public Duration duration(){
        Duration between = Duration.between(start, end);
        Duration day = Duration.ofDays(1);
        if(between.isZero()) return day;
        if(between.isNegative()) return day.plus(between);
        return between;
    }
    
}
TimeSection.java
import com.demo.domain.model.project.project.ProjectCode;

/**
 * 時間区分の集約クラス
 */
public class TimeSection {

    ProjectCode projectCode;
    TimeSectionId timeSectionId;
    TimeRange range;

    public TimeSection(ProjectCode projectCode, TimeSectionId timeSectionId, TimeRange range) {
        this.projectCode = projectCode;
        this.timeSectionId = timeSectionId;
        this.range = range;
    }

    @Deprecated
    public TimeSection(){}
    
    public ProjectCode projectCode(){
        return this.projectCode;
    }
    
    public TimeSectionId timeSectionId(){
        return this.timeSectionId;
    }

    public TimeRange range(){
        return this.range;
    }

}
TimeSectionMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.demo.infrastructure.datasource.timesection.TimeSectionMapper">

    <select id="selectAll" resultType="com.demo.domain.model.contact.timesection.TimeSection">
        SELECT
            project_code as "projectCode.value",
            time_section_id as "timeSectionId.value",
            start_time as "range.start",
            end_time as "range.end"
        FROM time_section_list
        WHERE project_code = #{value}
    </select>
    
    <select id="selectByCodeAndId" resultType="com.demo.domain.model.contact.timesection.TimeSection">
        SELECT
            project_code as "projectCode.value",
            time_section_id as "timeSectionId.value",
            start_time as "range.start",
            end_time as "range.end"
        FROM time_section_list
        WHERE project_code = #{projectCode.value}
        AND time_section_id = #{timeSectionId.value}
    </select>
    
</mapper>
detail.log
2024-05-17T15:31:19.952+09:00 DEBUG 46246 --- [http-nio-8080-exec-5] org.mybatis.spring.SqlSessionUtils       : Creating a new SqlSession
2024-05-17T15:31:19.953+09:00 DEBUG 46246 --- [http-nio-8080-exec-5] org.mybatis.spring.SqlSessionUtils       : SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1f2cdd47] was not registered for synchronization because synchronization is not active
2024-05-17T15:31:19.954+09:00 DEBUG 46246 --- [http-nio-8080-exec-5] o.m.s.t.SpringManagedTransaction         : JDBC Connection [HikariProxyConnection@2114130743 wrapping conn0: url=jdbc:h2:mem:test user=test] will not be managed by Spring
2024-05-17T15:31:19.954+09:00 DEBUG 46246 --- [http-nio-8080-exec-5] c.e.m.i.d.e.t.T.selectAll                : ==>  Preparing: SELECT project_code as "projectCode.value", time_section_id as "timeSectionId.value", start_time as "range.start", end_time as "range.end" FROM time_section_list WHERE project_code = ?
2024-05-17T15:31:19.956+09:00 DEBUG 46246 --- [http-nio-8080-exec-5] c.e.m.i.d.e.t.T.selectAll                : ==> Parameters: xxx(String)
2024-05-17T15:31:19.962+09:00 DEBUG 46246 --- [http-nio-8080-exec-5] c.e.m.i.d.e.t.T.selectAll                : <==      Total: 2
2024-05-17T15:31:19.963+09:00 DEBUG 46246 --- [http-nio-8080-exec-5] org.mybatis.spring.SqlSessionUtils       : Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1f2cdd47]
2024-05-17T15:31:21.779+09:00 DEBUG 46246 --- [Catalina-utility-2] o.apache.catalina.session.ManagerBase    : Start expire sessions StandardManager at 1715927481770 sessioncount 0
2024-05-17T15:31:21.780+09:00 DEBUG 46246 --- [Catalina-utility-2] o.apache.catalina.session.ManagerBase    : End expire sessions StandardManager processingTime 10 expired sessions: 0
0

1Answer

Your answer might help someone💌