A
A
Alexander Nikolaev2020-11-19 11:19:23
Java
Alexander Nikolaev, 2020-11-19 11:19:23

What to do: compilation error related to Spring Data repository (Bean not created)?

I am writing a web application in Spring, I need to work with the database. In a specific case, you need to pull out the entity class found by the body field from the database. It seems that IDEA does not find errors in the code, but at startup the error is:

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)!


The repository, Entity and service code is attached below.

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2020-11-19
@Dr3g0nra1der

IllegalArgumentException: Validation failed for query for method findByBody(String body)

Here is a short error log. Method validation error.
I can assume there are 2 problems:
Here, your code:
public interface ExpressionRepository extends CrudRepository<ExpressionEntity, Long> {

    @Query("FROM ExpressionEntity WHERE ExpressionEntity.body = body")
    public Optional<ExpressionEntity> findByBody(String body);

}

It should most likely be like this:
SELECT e FROM ExpressionEntity e WHERE e.body = ?1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question