我将从我的MobileApp获取JSON列表,我将这些转换为DataModel,然后尝试将dataModel列表作为批量插入。
这是我的数据模型或实体对象。
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.time.OffsetDateTime;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "cab_boarding")
public class CabBoardingModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Column(name = "gpid")
String gpid;
@Column(name = "latitude")
Long latitude;
@Column(name = "longitude")
Long longitude;
@Column(name = "vehicle_number")
String vehicleNumber;
@Column(name = "boarding_time")
OffsetDateTime boardingTime;
}
这是我的存储库接口
public interface CBRepository extends CrudRepository<CabBoardingModel, Long> {
List<CabBoardingModel> findAll();
List<CabBoardingModel> save(List<CabBoardingModel> cabBoardingModels);
Optional<CabBoardingModel> findById(Long id);
}
以下是调用此save
方法
@Service
public class CBService {
private CBRepository cbRepository;
@Autowired
public CBService(CBRepository cbRepository) {
this.cbRepository = cbRepository;
}
public List<CabBoardingModel> create(List<CabBoardingModel> cabBoardingModelList) {
List<CabBoardingModel> cbCreatedList = cbRepository.save(cabBoardingModelList);
return cbCreatedList;
}
}
访问保存方法时获取以下异常
2018-02-15 23:49:03.407 ERROR [smpoc-service-cabboarding,1b921dc7bf735306,1b921dc7bf735306,false] 2988 --- [nio-8080-exec-1] f.c.b.a.w.e.h.ControllerExceptionHandler : Unexpected exception
org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [java.util.ArrayList]: Could not find field for property during fallback access!
at org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper.getPropertyValue(DirectFieldAccessFallbackBeanWrapper.java:56) ~[spring-data-commons-1.13.10.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.getId(JpaMetamodelEntityInformation.java:149) ~[spring-data-jpa-1.11.10.RELEASE.jar:na]
at org.springframework.data.repository.core.support.AbstractEntityInformation.isNew(AbstractEntityInformation.java:51) ~[spring-data-commons-1.13.10.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.isNew(JpaMetamodelEntityInformation.java:227) ~[spring-data-jpa-1.11.10.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:507) ~[spring-data-jpa-1.11.10.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:513) ~[spring-data-commons-1.13.10.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:498) ~[spring-data-commons-1.13.10.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:475) ~[spring-data-commons-1.13.10.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56) ~[spring-data-commons-1.13.10.RELEASE.jar:na]
答案 0 :(得分:2)
Spring Data中的saveAll
方法仅适用于Spring Data 2.x(您提供的链接始终链接到最新版本的文档)。但是,使用Spring Boot 1.5时,它使用较旧的Spring Data版本,其中适用以下文档:https://docs.spring.io/spring-data/data-commons/docs/1.13.10.RELEASE/api/
如您所见,这里的方法签名是
<S extends T> Iterable<S> save(Iterable<S> entities)