JPA @Query java.lang.IllegalArgumentException:

时间:2018-11-24 20:58:50

标签: java spring hibernate jpa

此代码有问题

public interface CharityRepository extends CrudRepository<Charity,Long> {

  @Query("select sum(sponsorSum) from Charity")
  Integer totalCharitySum();
}

抛出异常

  

org.springframework.beans.factory.UnsatisfiedDependencyException:在文件[D:\ IdeaProjects \ maraphonskills(3)\ target \ classes \ com \ maraphon \ maraphonskills \ controllers \ AdminController.class中定义名称为'adminController'的bean创建错误]:通过构造函数参数0表示不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.UnsatisfiedDependencyException:在文件[D:\ IdeaProjects \ maraphonskills(3)\ target \ classes \ com \ maraphon \ maraphonskills \ service \ CharityServiceImpl.class中创建名称为'charityServiceImpl'的bean时出错]:通过构造函数参数0表示不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'charityRepository'的bean时出错:调用init方法失败;嵌套的异常是java.lang.IllegalArgumentException:方法公共抽象java.lang.Short com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()的查询验证失败!

     

原因:   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建文件中定义的名称为'charityServiceImpl'的bean时出错   [D:\ IdeaProjects \ maraphonskills(3)\ target \ classes \ com \ maraphon \ maraphonskills \ service \ CharityServiceImpl.class]:   通过构造函数参数0表示的不满足的依赖关系;   嵌套异常为   org.springframework.beans.factory.BeanCreationException:错误   创建名称为'charityRepository'的bean:初始化方法的调用   失败嵌套的异常是java.lang.IllegalArgumentException:   验证方法公共抽象java.lang.Short的查询失败   com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()!

     

由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为'charityRepository'的bean时出错:调用init方法失败;嵌套的异常是java.lang.IllegalArgumentException:方法公共抽象java.lang.Short com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()的查询验证失败!

     

原因:java.lang.IllegalArgumentException:方法公共抽象java.lang.Short com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()的查询验证失败!

     

原因:java.lang.NullPointerException:空

但是此代码可以正常工作

public interface CharityRepository extends CrudRepository<Charity,Long> {
  @Query("select sum(sponsorTarget) from Registration")
  Short totalSum();
}

@修改无济于事,这可能是什么问题?

慈善机构代码

 @Entity
@EqualsAndHashCode(exclude = "registrations")
@Data
public class Charity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @Lob
    private String description;
    private String fileName;
    private Short SponsorSum;

    @OneToMany(mappedBy = "charity")
    public Set<Registration> registrations = new HashSet<>();
    }

注册代码

    @Entity
@EqualsAndHashCode(exclude = "sponsorShips")
@Data
public class Registration {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    public Runner runner;
    private String registrationDateTime;
    @ManyToOne
    public Charity charity;
    @ManyToOne
    public RaceKitOption raceKitOption;
    @ManyToOne
    public RegistrationStatus registrationStatus;

    private Short sponsorTarget;
    @OneToMany(mappedBy = "registration")
    public Set<SponsorShip> sponsorShips = new HashSet<>();
   // public RegistrationEvent registrationEvent;
}

1 个答案:

答案 0 :(得分:0)

提示: Java区分大小写。

问题出在 CharityRepository 界面中使用小写的SponsorSum之间:

@Query("select sum(sponsorSum) from Charity")

慈善类中的大写字母:

private Short SponsorSum;

在两种情况下都使用相同的情况,它将解决您的问题。