QueryDsl:将“Date”转换为“LocalDate”

时间:2018-06-04 10:27:30

标签: java querydsl localdate

我在4.1.3版本中使用QueryDsl。

My Entity包含java.util.Date类型的字段,结果类包含java.time.LocalDate类型的字段。

我想用数据库中的值填充结果对象,但是下面的代码给出了一个错误: java.lang.IllegalArgumentException:java.util.Date与java.time.LocalDate不兼容

如何在这些类型之间进行转换?

实体

import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "myEntity")
public class EntityType {

    @Id
    private Long id;

    private Date birthday;

    public Date getBirthday() {
        return this.birthday;
    }

    public void setBirthday(final Date birthday) {
        this.birthday = birthday;
    }
}

结果类

import java.time.LocalDate;  
public class ResultType {

    private LocalDate birthday;

    public LocalDate getBirthday() {
        return this.birthday;
    }

    public void setBirthday(final LocalDate birthday) {
        this.birthday = birthday;
    }
}

请求

import java.sql.Connection;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAQueryBase;
import com.querydsl.jpa.impl.JPAQuery;

@SpringJUnitConfig
public class LocalDateTest extends AbstractTest {

    @Test
    public void testLocalDate() {
        final JPAQuery<ResultType> query = new JPAQuery<>(super.entityManager);
        final QEntityType e = QEntityType.entityType;
        final JPAQueryBase<ResultType, ?> queryBase = query.from(e);

        queryBase.select(Projections.bean(ResultType.class, e.birthday));

        queryBase.fetch();
    }
}

0 个答案:

没有答案