org.springframework.dao.InvalidDataAccessApiUsageException:参数值与预期类型java.time.LocalDateTime不匹配 [英] org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value did not match expected type java.time.LocalDateTime
问题描述
我刚刚开始照顾别人开发的代码。
简而言之,如果我尝试使用字符串,布尔值或整数字段进行搜索,则可以正常工作。例如,当我使用state == SP搜索/过滤时,得到了结果,并且可以看到硅藻土字段已正确填充。在这种情况下,我还看到休眠状态没有抱怨无效数据:
当我使用 state == SP成功搜索时,从控制台复制了
...
TRACE 2450 --- [nio-9000-exec-2] ohtype .descriptor.sql.BasicExtractor:提取的值([created17_12 _]:[TIMESTAMP])-[2013-07-12T06:53:47]
...
不过,当我使用日期时间进行搜索/过滤时,我得到了此问题中概述的错误。例如:我搜索 createdDate == 2014-06-03T18:48:33并得到:
2018-03 -23 17:38:17.547错误2450-[nio-9000-exec-5] oaccC [。[。[/]。[dispatcherServlet]:Servlet [dispatcherServlet]的Servlet.service(),路径为[]引发异常[请求处理失败;嵌套的异常是org.springframework.dao.InvalidDataAccessApiUsageException:参数值[2014-06-03T18:48:33]与预期的类型[java.time.LocalDateTime(n / a)]不匹配;嵌套异常为java.lang.IllegalArgumentException:参数值[2014-06-03T18:48:33]与根本原因
$ b $的预期类型[java.time.LocalDateTime(n / a)]不匹配b java.lang.IllegalArgumentException:参数值[2014-06-03T18:48:33]与org.hibernate.jpa.spi处的预期类型[java.time.LocalDateTime(n / a)]
不匹配。 BaseQueryImpl.validateBinding(BaseQueryImpl.java:897)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.internal.QueryImpl.access $ 000(QueryImpl。 java:61)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.internal.QueryImpl $ ParameterRegistrationImpl.bindValue(QueryImpl.java:235)〜 [hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:638)〜[hibernate-entitymanager-5.0。 12.Final.jar:5.0.12.Final]
,位于org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:163)〜[hibe rnate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32)〜[hibernate-entitymanager-5.0.12 .Final.jar:5.0.12.Final]
在org.hibernate.jpa.criteria.compile.CriteriaCompiler $ 1 $ 1.bind(CriteriaCompiler.java:109)〜[hibernate-entitymanager-5.0.12.Final。 jar:5.0.12.Final]
at org.hibernate.jpa.criteria.CriteriaQueryImpl $ 1.buildCompiledQuery(CriteriaQueryImpl.java:366)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12。最终]
,位于org.hibernate.jpa.criteria.compile.CriteriaCompiler.compile(CriteriaCompiler.java:130)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
在org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:699)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
在sun.reflect.GeneratedMethodAccessor93 .invoke(未知来源)〜[na:na]
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodA ccessorImpl.java:43)〜[na:1.8.0_162]
在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_162]
在组织中。 springframework.orm.jpa.ExtendedEntityManagerCreator $ ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)〜[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
在com.sun.proxy。$ Proxy115.createQuery(未知来源)〜[na:na]
在sun.reflect.GeneratedMethodAccessor93.invoke(未知来源)〜[na:na]
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl。 java:43)〜[na:1.8.0_162]
在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_162]
在org.springframework。 orm.jpa.SharedEntityManagerCreator $ SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)〜[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
在com.sun.proxy。$ Proxy115上。 createQuery(未知来源)〜[na:na]
,位于org.springframework.data.jpa.repository.support.SimpleJpa Repository.getQuery(SimpleJpaRepository.java:656)〜[spring-data-jpa-1.11.6.RELEASE.jar:na]
在org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository .java:608)〜[spring-data-jpa-1.11.6.RELEASE.jar:na]
在org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:407) 〜[spring-data-jpa-1.11.6.RELEASE.jar:na]
在sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)〜[na:1.8.0_162]
在sun.reflect .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_162]
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_162]
在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_162]
在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport。 java:504)〜[spring-data-commons-1.13.6.RELEASE。 jar:na] org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)中的
〜[spring-data-commons-1.13.6.RELEASE.jar:na ]
,位于org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)〜[spring-data-commons-1.13.6.RELEASE.jar:na]
在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
在org.springframework。 data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)〜[spring-data-commons-1.13.6.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation .java:179)〜[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
在org.springframework.data.jpa.repository.support.Cru dMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)〜[spring-data-jpa-1.11.6.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation。 :179)〜[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)〜[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE] aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
,位于org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.10.RELEASE .jar:4.3.10.RELEASE]
,位于org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)〜[spring-data-commons-1.13.6.RELEASE。 jar:na]
,位于org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 〜[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)〜[spring-aop-4.3 .10.RELEASE.jar:4.3.10.RELEASE]
在com.sun.proxy。$ Proxy150.findAll(未知来源)〜[na:na]
在br.com.moving.service .ApiServiceImpl.list(ApiServiceImpl.java:48)
调用findAll时存储库引发错误:
@存储库
@Transactional
公共类ApiServiceImpl实现ApiService {
@PersistenceContext
私有EntityManager em;
@Override
public< T>页面< T>列表(Class T类别clazz,Class扩展JpaSpecificationExecutor T,clazz2,字符串搜索,
Pageable可分页,Class扩展Specification T s specClazz){
JpaSpecificationExecutor< T> ;仓库=新的JpaRepositoryFactory(em).getRepository(clazz2);
SpecificationBuilder< T> builder =新的SpecificationBuilder<>(specClazz);
Pattern pattern = Pattern.compile((\\w +。*?)(== |!= |< |< = |> |> = | = | ===)( (\\ * | \\w +)。*?););
Matcher matcher = pattern.matcher(search +;);
while(matcher.find()){
builder.with(matcher.group(1),matcher.group(2),matcher.group(3));
}
org.springframework.data.jpa.domain.Specification< T>规格= builder.build();
Page< T>页面= repository.findAll(spec,pageable);
返回页面;
}
...
ApiService接口
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
进口br.com.mycompany.specification.Specification;
公共接口ApiService {
< T>页面< T> list(Class T类别clazz,Class扩展JpaSpecificationExecutor T clcl2,字符串搜索,可分页的页面,Class扩展Specification T的customSpec);
< T> T get(Class T的clazz,Class的扩展JpaSpecificationExecutor T clcl2,字符串搜索,Class扩展的Specification T的customSpec);
}
LocalDateTimeSerializer:
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
公共类LocalDateTimeSerializer扩展了JsonSerializer< LocalDateTime>。 {
@Override
public void serialize(LocalDateTime dateTime,JsonGenerator generator,
com.fasterxml.jackson.databind.SerializerProvider provider)
引发IOException,com.fasterxml .jackson.core.JsonProcessingException {
if(dateTime == null){
generator.writeNull();
}否则{
generator.writeString(dateTime.format(DateTimeFormatter.ofPattern( yyyy-MM-dd’T’HH:mm:ss))));
}
}
}
型号:
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import br.com.mycompany.converter.BooleanNumberConverter;
import br.com.mycompany.converter.ReleaseCategoryTypeConverter;
import br.com.mycompany.converter.ReleaseStatusTypeConverter;
import br.com.mycompany.serializer.LocalDateTimeSerializer;
@Entity
@Table(name = releases)
@JsonInclude(Include.NON_NULL)
公共类发布实现可序列化{
$ b private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = id,nullable = false)
私有Long id;
@Column(name = state,长度= 2)
私有字符串状态;
@ JsonSerialize(使用= LocalDateTimeSerializer.class)
@Column(name = created_date,nullable = false,insertable = true,可更新= false)
private LocalDateTime createdDate = LocalDateTime.now();
//其他字段,构造函数,获取/设置不相关
数据库是Mysql。如果我通过Workbench在MySql中直接搜索,可以看到以下示例:
从版本
中选择created_date '0000-00-00 00:00:00'
'0000-00-00 00:00:00'
...
'2013-09-28 09:19:23 '
'2013-09-28 09:19:59'
通过锁定关闭格式在我看来LocalDateTimeSerializer.serialize是正确的。
任何线索都会受到高度赞赏。
* ***编辑
仔细阅读后
和
使用的方法完全没有区别:
1-从列中删除了@JsonSerialize和@Converter。我的意思是:
// @ JsonSerialize(using = LocalDateTimeSerializer.class)
// @ Convert(转换器= MovingLocalDateTimeConverter .class)
@Column(name = created_date,nullable = false,insableable = true,可更新= false)
private LocalDateTime createdDate;
2-添加了JsonSerialize。我的意思是:
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
公共类LocalDateTimeSerializer扩展了JsonSerializer< LocalDateTime> {
@Override
public void serialize(LocalDateTime dateTime,JsonGenerator generator,
com.fasterxml.jackson.databind.SerializerProvider provider)
引发IOException,com.fasterxml .jackson.core.JsonProcessingException {
if(dateTime == null){
generator.writeNull();
}否则{
generator.writeString(dateTime.format(DateTimeFormatter.ofPattern( yyyy-MM-dd’T’HH:mm:ss))));
}
}
}
和
@JsonSerialize(使用= LocalDateTimeSerializer.class)
@Column(name = created_date,nullable = false,可插入= true,可更新= false)
private LocalDateTime createdDate;
3-添加了转换器。我的意思是,
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter
公共类MovingLocalDateTimeConverter实现AttributeConverter< LocalDateTime,String> {
//私有DateTimeFormatter fmt = DateTimeFormatter.ofPattern( yyyy-MM-dd HH:mm:ss);
private DateTimeFormatter fmt = DateTimeFormatter.ofPattern( yyyy-MM-dd’T’HH:mm:ss);
@Override
public String convertToDatabaseColumn(LocalDateTime value){
return(value!= null)? value.format(fmt):null;
}
@Override
public LocalDateTime convertToEntityAttribute(String value){
return convertLocalDateTime(value);
}
private LocalDateTime convertLocalDateTime(String value){
try {
return(value!= null)? LocalDateTime.parse(value.substring(0,19),fmt):null;
} catch(DateTimeException e){
返回null;
}
}
}
和
@Convert(converter = MovingLocalDateTimeConverter.class)
@Column(name = created_date,可为空= false,可插入= true,可更新= false)
private LocalDateTime createdDate;
所有暂定性都不会因当前错误而改变。
我建议您添加以下依赖项。有关更多信息,请在此处
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-java8< / artifactId>
< version> $ {hibernate.version}< / version>
< / dependency>
I just started to take care of a code developed by other person. It was designed based on Specification pattern using Spring Data and Hibernate.
In few words, if I try search using string, boolean or integer fields it works correctly. For instance, when I search/filter with state==SP I got the result and I can see that the diatomite fields are correctly filled in. Also I see hibernate no complaining about invalid data in this case:
Copied form my console when I searched succesfully with "state==SP" ... TRACE 2450 --- [nio-9000-exec-2] o.h.type.descriptor.sql.BasicExtractor : extracted value ([created17_12_] : [TIMESTAMP]) - [2013-07-12T06:53:47] ...
Nevertheless, when I search/filter with datetime I get the error summarised in this question. For instance: I search "createdDate==2014-06-03T18:48:33" and I get:
2018-03-23 17:38:17.547 ERROR 2450 --- [nio-9000-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [2014-06-03T18:48:33] did not match expected type [java.time.LocalDateTime (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [2014-06-03T18:48:33] did not match expected type [java.time.LocalDateTime (n/a)]] with root cause
java.lang.IllegalArgumentException: Parameter value [2014-06-03T18:48:33] did not match expected type [java.time.LocalDateTime (n/a)]
at org.hibernate.jpa.spi.BaseQueryImpl.validateBinding(BaseQueryImpl.java:897) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.internal.QueryImpl.access$000(QueryImpl.java:61) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:235) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:638) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:163) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.criteria.compile.CriteriaCompiler$1$1.bind(CriteriaCompiler.java:109) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.criteria.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:366) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.criteria.compile.CriteriaCompiler.compile(CriteriaCompiler.java:130) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:699) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at sun.reflect.GeneratedMethodAccessor93.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at com.sun.proxy.$Proxy115.createQuery(Unknown Source) ~[na:na]
at sun.reflect.GeneratedMethodAccessor93.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at com.sun.proxy.$Proxy115.createQuery(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:656) ~[spring-data-jpa-1.11.6.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:608) ~[spring-data-jpa-1.11.6.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:407) ~[spring-data-jpa-1.11.6.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_162]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_162]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.11.6.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at com.sun.proxy.$Proxy150.findAll(Unknown Source) ~[na:na]
at br.com.moving.service.ApiServiceImpl.list(ApiServiceImpl.java:48)
The repository raising error when calling findAll:
@Repository
@Transactional
public class ApiServiceImpl implements ApiService {
@PersistenceContext
private EntityManager em;
@Override
public <T> Page<T> list(Class<T> clazz, Class<? extends JpaSpecificationExecutor<T>> clazz2, String search,
Pageable pageable, Class<? extends Specification<T>> specClazz) {
JpaSpecificationExecutor<T> repository = new JpaRepositoryFactory(em).getRepository(clazz2);
SpecificationBuilder<T> builder = new SpecificationBuilder<>(specClazz);
Pattern pattern = Pattern.compile("(\\w+.*?)(==|!=|<|<=|>|>=|=|===)((\\*|\\w+).*?);");
Matcher matcher = pattern.matcher(search + ";");
while (matcher.find()) {
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
}
org.springframework.data.jpa.domain.Specification<T> spec = builder.build();
Page<T> page = repository.findAll(spec, pageable);
return page;
}
...
ApiService interface
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import br.com.mycompany.specification.Specification;
public interface ApiService {
<T> Page<T> list(Class<T> clazz, Class<? extends JpaSpecificationExecutor<T>> clazz2, String search, Pageable pageable, Class<? extends Specification<T>> customSpec);
<T> T get(Class<T> clazz, Class<? extends JpaSpecificationExecutor<T>> clazz2, String search, Class<? extends Specification<T>> customSpec);
}
LocalDateTimeSerializer:
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime dateTime, JsonGenerator generator,
com.fasterxml.jackson.databind.SerializerProvider provider)
throws IOException, com.fasterxml.jackson.core.JsonProcessingException {
if (dateTime == null) {
generator.writeNull();
} else {
generator.writeString(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")));
}
}
}
Model:
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import br.com.mycompany.converter.BooleanNumberConverter;
import br.com.mycompany.converter.ReleaseCategoryTypeConverter;
import br.com.mycompany.converter.ReleaseStatusTypeConverter;
import br.com.mycompany.serializer.LocalDateTimeSerializer;
@Entity
@Table(name = "releases")
@JsonInclude(Include.NON_NULL)
public class Release implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "state", length = 2)
private String state;
@JsonSerialize(using = LocalDateTimeSerializer.class)
@Column(name = "created_date", nullable = false, insertable = true, updatable = false)
private LocalDateTime createdDate = LocalDateTime.now();
//other fields, constructors, gets/sets not relevant
The database is Mysql. If I search straigh in MySql via Workbench I can see this sample:
select created_date from releases
'0000-00-00 00:00:00'
'0000-00-00 00:00:00'
...
'2013-09-28 09:19:23'
'2013-09-28 09:19:59'
By locking close the format it seems to me that LocalDateTimeSerializer.serialize is correct.
Any clue will be highly appreciated.
**** edited
After read carefully Hibernate 4 with java.time.LocalDate and DATE() construct, I checked which jar versions I have and, as far as I can see, I didn't find any tip if there is a problem with my jars. I added below two print screen which reveals which jar versions I am using. Additionally, I am pasting bellow the three approaches used. All three resulted in same error explained above.
Libraries:
and
Approaches used with no difference at all:
1 - removed @JsonSerialize and @Converter from column. I mean:
//@JsonSerialize(using = LocalDateTimeSerializer.class)
//@Convert(converter = MovingLocalDateTimeConverter.class)
@Column(name = "created_date", nullable = false, insertable = true, updatable = false)
private LocalDateTime createdDate;
2 - added JsonSerialize. I mean:
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime dateTime, JsonGenerator generator,
com.fasterxml.jackson.databind.SerializerProvider provider)
throws IOException, com.fasterxml.jackson.core.JsonProcessingException {
if (dateTime == null) {
generator.writeNull();
} else {
generator.writeString(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")));
}
}
}
and
@JsonSerialize(using = LocalDateTimeSerializer.class)
@Column(name = "created_date", nullable = false, insertable = true, updatable = false)
private LocalDateTime createdDate;
3 - added converter. I mean,
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter
public class MovingLocalDateTimeConverter implements AttributeConverter<LocalDateTime, String> {
//private DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
@Override
public String convertToDatabaseColumn(LocalDateTime value) {
return (value != null) ? value.format(fmt) : null;
}
@Override
public LocalDateTime convertToEntityAttribute(String value) {
return convertLocalDateTime(value);
}
private LocalDateTime convertLocalDateTime(String value) {
try {
return (value != null) ? LocalDateTime.parse(value.substring(0, 19), fmt) : null;
} catch (DateTimeException e) {
return null;
}
}
}
and
@Convert(converter = MovingLocalDateTimeConverter.class)
@Column(name = "created_date", nullable = false, insertable = true, updatable = false)
private LocalDateTime createdDate;
All tentatives didn't change at all the current error.
I suggest you to add below dependency. For more info please refer here
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>${hibernate.version}</version>
</dependency>
这篇关于org.springframework.dao.InvalidDataAccessApiUsageException:参数值与预期类型java.time.LocalDateTime不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!