Unable To use java.time.LocalDate in JPA entity with MySql

自古美人都是妖i 提交于 2021-02-19 04:47:07

问题


I am trying to use LocalDate in my entities and this usage has plenty of documentation around the net. Which is why I am baffled it doesn't work.

Here is my error: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xD7\x0C\x03x'

So, it obviously needs a converter. I have tried the following links:

This one says to add the spring converters to the scan path for the EntityManager. I directly tried his example (get all kinds of errors) as well as adapting it to my code (below code does indeed execute):

@Autowired
private void configPersistence(LocalContainerEntityManagerFactoryBean factoryBean) {
    factoryBean.setPackagesToScan(Worker.class.getPackage().toString(),"org.springframework.data.jpa.convert.threeten");
}

But the problem persists.

I've been here and here, which all say something similar.

Simply adding the hibernate-java8 dependency doesn't work as it suggests.

   <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

I have gone so far as to independently instantiate the converter bean and no dice. It must be something else.

UPDATE

If I literally C&P the Spring code and add it as a bean, it works!? This is not the right way to do it.

@Bean
public AttributeConverter<LocalDate,Date> getConverter() {
    return new LocalDateConverter();
}

@Converter(autoApply = true)
public static class LocalDateConverter implements AttributeConverter<LocalDate, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDate date) {
        return Jsr310Converters.LocalDateToDateConverter.INSTANCE.convert(date);
    }

    @Override
    public LocalDate convertToEntityAttribute(Date date) {
        return Jsr310Converters.DateToLocalDateConverter.INSTANCE.convert(date);
    }
}

Transitive dependencies from spring-data

+- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  \- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile

End update

Here is my entity:

@Entity
public class Worker {
    private Long id;
    private String givenName;
    private String familyName;
    private LocalDate dob;
    private String nationalId;
    private byte[] photo;

    public Worker() {
        this.id = Math.abs(new Random().nextLong());
    }

    @Id
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "GivenName")
    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String givenName) {
        this.givenName = givenName;
    }

    @Basic
    @Column(name = "FamilyName")
    public String getFamilyName() {
        return familyName;
    }

    public void setFamilyName(String familyName) {
        this.familyName = familyName;
    }

    @Basic
    @Column(name = "DOB")
    public LocalDate getDob() {
        return dob;
    }

    public void setDob(LocalDate dob) {
        this.dob = dob;
    }

    @Basic
    @Column(name = "NationalID")
    public String getNationalId() {
        return nationalId;
    }

    public void setNationalId(String nationalId) {
        this.nationalId = nationalId;
    }

    @Basic
    @Column(name = "photo")
    public byte[] getPhoto() {
        return photo;
    }

    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }
}

回答1:


You need Hibernate 5.0+ along with the hibernate-java8 dependency for it to work. You can check the 5.0 changelog here which should show the addition of the Java 8 support. Also some official blog entry saying the same here.

Please check the migration guide from 4.3 to 5.0 for additional information.



来源:https://stackoverflow.com/questions/36363913/unable-to-use-java-time-localdate-in-jpa-entity-with-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!