Reputation: 3680
I will get List of JSONs from my MobileApp and I am converting those to a DataModel and then trying to insert the list of dataModel as a bulk.
This is my Data Model or Entity Object.
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;
}
This is my Repository Interface
public interface CBRepository extends CrudRepository<CabBoardingModel, Long> {
List<CabBoardingModel> findAll();
List<CabBoardingModel> save(List<CabBoardingModel> cabBoardingModels);
Optional<CabBoardingModel> findById(Long id);
}
Below is the service tier that calls this save
method
@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;
}
}
Getting the below exception while accessing the save method
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]
Upvotes: 4
Views: 16350
Reputation: 157
You should add @Transactional annotation to your method, so spring will handle the transaction and commit your changes. This usually comes on the @Service class, but I see in your example that you do not have one, so put it on the controller (or add a service layer, I think it's better)
Upvotes: 0
Reputation: 44515
The saveAll
method in Spring Data is only available with Spring Data 2.x (the link you provided links always to the documentation of the latest version). However, with Spring Boot 1.5, it uses an older Spring Data version, where the following documentation applies: https://docs.spring.io/spring-data/data-commons/docs/1.13.10.RELEASE/api/
As you can see, here the method signature is
<S extends T> Iterable<S> save(Iterable<S> entities)
Upvotes: 2