Java: method saveAllAndFlush in interface org.springframework.data.jpa.repository.JpaRepository<T,ID> cannot be applied to given types

Viewed 23

My Entity

public class CarModelEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "model_id")
    private Integer modelId;
    @Column(name = "model_name")
    private String modelName;
    @Column(name = "is_parent")
    private Integer isParent;
    @Column(name = "parent_id")
    private Integer parentId;
    @Column(name = "created_by")
    private Integer createdBy;
    @Column(name = "created_at")
    private Timestamp createdAt;
    @Column(name = "updated_at")
    private Timestamp updatedAt;

}

My code :

CarModelEntity data = carModelRepo
                        .saveAllAndFlush(CarModelEntity.builder()
                                .isParent(1)
                                .modelName(value)
                                .build());

Syntax Error :

Error:(49, 25) java: method saveAllAndFlush in interface org.springframework.data.jpa.repository.JpaRepository<T,ID> cannot be applied to given types;
  required: java.lang.Iterable<S>
  found: com.*****.catprices.mobileapp.model.entity.CarModelEntity
  reason: cannot infer type-variable(s) S
    (argument mismatch; com.****.catprices.mobileapp.model.entity.CarModelEntity cannot be converted to java.lang.Iterable<S>)

My Repository :

public interface CarModelRepo extends JpaRepository<CarModelEntity, Integer> {
}
1 Answers

Reposted it as answer

saveAllAndFlush accept Iterable object while you give simple Object, you can add your CalModelEntity to an Object that implement Iterable such as ArrayList, HashMap or etc, and then pass the Object to saveAllAndFlush method. and also saveAllAndFlush return List of Object so you need a List to receive the return value from the method.

Here is some example code

List<CarModelEntity> paramCarModelEntityList = new ArrayList<CarModelEntity>();
        paramCarModelEntityList.add(CarModelEntity.builder()
                .isParent(1)
                .modelName(value)
                .build());
List<CarModelEntity> carModelEntityList = repository.saveAllAndFlush(paramCarModelEntityList);

Here are some reference links:

Iterable JpaRepository

Related