spring-data-jpa Java:无法将接口org.springframework.data.jpa.repository中的方法saveAllAndFlush应用于给定的类型

xpcnnkqh  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(632)

我的实体

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;

}

我的代码:

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

语法错误:

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>)

我的存储库

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

wbgh16ku1#

作为答案转发
saveAllAndFlush接受Iterable对象当你给予简单的Object时,你可以把你的CalModelEntity添加到一个实现Iterable的Object中,比如ArrayList,HashMap等等,然后把Object传递给saveAllAndFlush方法。并且saveAllAndFlush还返回Object的List,所以你需要一个List来接收来自方法的返回值。
下面是一些示例代码

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

以下是一些参考链接:
IterableJpaRepository显示器

相关问题