Пишу занчит web-приложение на Spring, нужна работа с бд. В конкретном случае нужно из БД вытащить класс сущности, найденный по полю body. Вроде IDEA в коде ошибок не находит, но при запуске ошибка:
2020-11-18 22:39:48.404 WARN 8548 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'expressionRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.Optional com.web.springcalculator.repository.ExpressionRepository.findByBody(java.lang.String)!
Код репозитория, Entity и сервиса приложен ниже.
package com.web.springcalculator.repository;
import com.web.springcalculator.expressions.ExpressionEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import java.util.Optional;
public interface ExpressionRepository extends CrudRepository<ExpressionEntity, Long> {
@Query("FROM ExpressionEntity WHERE ExpressionEntity.body = body")
public Optional<ExpressionEntity> findByBody(String body);
}
package com.web.springcalculator.expressions;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
@Entity
@Table(name = "EXPRESSIONS")
@NoArgsConstructor
@AllArgsConstructor
public class ExpressionEntity {
@Id
@GeneratedValue
@Getter
private Long id;
@Getter
@Column(name = "body", nullable = false)
@Setter
private String body;
@Getter
@Column(name = "answer", nullable = false)
@Setter
private double answer;
}
package com.web.springcalculator;
import com.web.springcalculator.dto.ExpressionDTO;
import com.web.springcalculator.expressions.ExpressionEntity;
import com.web.springcalculator.repository.ExpressionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
@RequiredArgsConstructor
public class ExpressionService {
private final ExpressionRepository repository;
public Optional<ExpressionDTO> findById(final long id){
return repository.findById(id).map(ExpressionDTO::convert);
}
public Optional<ExpressionDTO> findByBody(final String body){
return repository.findByBody(body).map(ExpressionDTO::convert);
}
public void save(final ExpressionEntity entity){
repository.save(entity);
}
}