gpt4 book ai didi

SpringBoot JPA 存储库保存方法不起作用

转载 作者:行者123 更新时间:2023-12-03 16:05:56 28 4
gpt4 key购买 nike

我将从我的 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]

最佳答案

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)

关于SpringBoot JPA 存储库保存方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48813573/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com