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;
}
CharitycodesponsorSum(in query) vsSponsorSum(in entity)