1

Have problem with this code

public interface CharityRepository extends CrudRepository<Charity,Long> {

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

Throw exception

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminController' defined in file [D:\IdeaProjects\maraphonskills(3)\target\classes\com\maraphon\maraphonskills\controllers\AdminController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'charityServiceImpl' defined in file [D:\IdeaProjects\maraphonskills(3)\target\classes\com\maraphon\maraphonskills\service\CharityServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'charityRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Short com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()!

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'charityServiceImpl' defined in file [D:\IdeaProjects\maraphonskills(3)\target\classes\com\maraphon\maraphonskills\service\CharityServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'charityRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Short com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()!

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'charityRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Short com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()!

Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.lang.Short com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()!

Caused by: java.lang.NullPointerException: null

But this code works fine

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

@Modifying doesn't help.What could be the problem?

Charity Code

 @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<>();
    }

Registration code

    @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;
}
6
  • Please post Charity code Commented Nov 24, 2018 at 21:07
  • I edited my question Commented Nov 24, 2018 at 21:14
  • Mind the capital letter at the beginning of sponsorSum (in query) vs SponsorSum (in entity) Commented Nov 24, 2018 at 21:16
  • OOOOOOOH My.... Didn't notice this.Thanks for the help! Commented Nov 24, 2018 at 21:20
  • The error message says: "Validation failed for query for method public abstract java.lang.Short com.maraphon.maraphonskills.repository.CharityRepository.totalCharitySum()". So no, it doesn't work with Short. Commented Nov 24, 2018 at 21:20

1 Answer 1

1

Tips: Java is case sensitive.

The issue is between the use of lowercase sponsorSum, in your CharityRepository interface:

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

And uppercase one in your Charity class:

private Short SponsorSum;

Use the same case in both, and it will solve your issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.