【问题标题】:Spring Data update query errorSpring Data 更新查询报错
【发布时间】:2017-09-20 10:02:20
【问题描述】:

我认为错误是类型不匹配异常,但我不知道为什么会得到它。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carController' defined in file [C:\Users\aleks\Desktop\Spring Boot With Rest\app\target\classes\carMarket\app\controllers\CarController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carServiceImpl' defined in file [C:\Users\aleks\Desktop\Spring Boot With Rest\app\target\classes\carMarket\app\services\implementations\CarServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract carMarket.app.orm.Car carMarket.app.repositories.CarRepository.updateCar(java.lang.String,java.lang.String,java.lang.Long,java.math.BigDecimal)!

这是我调用它的服务

@Transactional
@Override
public boolean editCar(long userId, long carId, CarBindingModel carBindingModel)
{
    Car car = this.carRepository.findById(carId);
    Car map = this.modelMapper.map(carBindingModel, Car.class);
    String make = carBindingModel.getMake();
    String model = carBindingModel.getModel();
    Long milesDriven = carBindingModel.getMilesDriven();
    BigDecimal price = carBindingModel.getPrice();
    Car car1 = this.carRepository.updateCar(make, model, milesDriven, price);
    return false;
}

这就是 ORM

@Entity(name = "cars")
public class Car
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotNull
    private String make;
    @NotNull
    private String model;
    @NotNull
    private BigDecimal price;
    @NotNull
    private Long milesDriven;
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @OneToOne(fetch = FetchType.EAGER)
    private User user;

当然,它有公共的 getter 和 setter 以及公共的空构造函数。 此错误仅在更新方法中出现,我对删除或查找方法没有任何问题..

这是它自己的查询

@Modifying
@Query(value = "UPDATE Car as s set s.make= :make, s.model= :model, s.milesDriven= :milesDriven, s.price= :price")
Car updateCar(@Param(value = "make") String make, @Param(value = "model") String model, @Param(value = "milesDriven") Long milesDriven, @Param(value = "price") BigDecimal price);

顺便说一句,这是编译时错误。

【问题讨论】:

  • 添加 updateCar 方法

标签: java spring jpa repository jpl


【解决方案1】:

所以我的问题是休耕 我有这个

@Entity(name = "cars")
public class Car

然后在 JPQL 上面的 repositories 方法中我有这个

@Query(value = "UPDATE Car as s set s.make= :make, s.model= :model, s.milesDriven= :milesDriven, s.price= :price")

所以它不应该是UPDATE Car,而是UPDATE cars

【讨论】:

    猜你喜欢
    • 2018-01-13
    • 2023-03-26
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2018-03-01
    • 2014-10-04
    相关资源
    最近更新 更多