Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在忽略JPA AttributeConverter_Java_Hibernate_Jpa_Java 8 - Fatal编程技术网

Java 正在忽略JPA AttributeConverter

Java 正在忽略JPA AttributeConverter,java,hibernate,jpa,java-8,Java,Hibernate,Jpa,Java 8,我正在尝试使用Hibernate 4.3.0使用AttributeConverter将新的Java 8 ZonedDateTime值存储在MySQL数据库(DATETIME字段)中 当我尝试执行更新时,会出现以下错误: ...Data truncation: Incorrect datetime value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x0D\x06\x00\x00

我正在尝试使用Hibernate 4.3.0使用AttributeConverter将新的Java 8 ZonedDateTime值存储在MySQL数据库(DATETIME字段)中

当我尝试执行更新时,会出现以下错误:

...Data truncation: Incorrect datetime value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x0D\x06\x00\x00\x07\xE0\x02\x11\x0B&\xEE\xE0\x08\x' for column...
我读过很多关于使用转换器方法的答案,所以我写了一个:

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Date;
import java.time.LocalDate;
import java.time.ZonedDateTime;

@Converter(autoApply = true)
public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime, Date> {
    @Override
    public Date convertToDatabaseColumn(ZonedDateTime zonedDateTime) {
        if (zonedDateTime == null) return null;

        return java.sql.Date.valueOf(zonedDateTime.toLocalDate());
    }

    @Override
    public ZonedDateTime convertToEntityAttribute(Date date) {
        if (date == null) return null;

        LocalDate localDate = date.toLocalDate();
        return ZonedDateTime.from(localDate);
    }
}
import javax.persistence.AttributeConverter;
导入javax.persistence.Converter;
导入java.sql.Date;
导入java.time.LocalDate;
导入java.time.ZonedDateTime;
@转换器(自动应用=真)
公共类ZonedDateTimeConverter实现AttributeConverter{
@凌驾
公共日期转换为DatabaseColumn(ZonedDateTime ZonedDateTime){
if(zonedDateTime==null)返回null;
返回java.sql.Date.valueOf(zonedDateTime.toLocalDate());
}
@凌驾
公共分区DateTime convertToEntityAttribute(日期){
如果(日期==null)返回null;
LocalDate LocalDate=date.toLocalDate();
返回ZonedDateTime.from(localDate);
}
}
。。。但它从未被呼叫过

我甚至添加了
jpaProperties.put(“hibernate.archive.autodetection”,“class,hbm”)到我的JPA属性,但没有运气。ZonedDateTimeConverter类与我的实体在同一个包中,因此应该对其进行扫描

我在这里遗漏了什么?

阅读JPA 2.1规范:

“除以下各项外,支持所有基本类型的转换:Id属性(包括 嵌入ID和派生标识的属性)、版本属性、关系属性和 在XML描述符中显式注释为枚举或时态或指定为枚举或时态的属性。”


您的ZonedDateTime字段是否有可能在您的实体中用@Id或@Temporal注释?

好的,这是我的错误。我的应用程序有一部分由Hibernate管理,但有一部分不是。我遇到问题的对象不是由Hibernate管理的,因此没有调用JPA是有道理的。

您是否也尝试过用注释
@Convert(converter=ZonedDateTimeConverter.class)
对要转换的属性进行注释?是的,我尝试过使用/不使用这些注释和“自动应用”设置为真/假;所有的组合都给出了相同的结果。只是对你的转换器的一般评论。。。您说希望持久保存在DATETIME字段中,但您只是使用java.sql.DATE来存储日期。您可以转换为java.util.Date,这就是alsoI将
@Temporal
替换为
@Convert
的时间,但正如我刚才在自己的回答中所说的,这是我自己的错。该实体不是由Hibernate(或任何其他JPA提供商)管理的。谢谢,修复了我的问题!我的一个字段上有一个
@Convert
@Temporal
,其中转换器被忽略,并且在检索值时出现错误。移除
@Temporal
后,它就像一个符咒。