Model:
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "SliderRes")
public class SliderModel {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;
@Column(name = "obrazek")
@Lob
private String img;
@Column(name = "tytul")
private String title;
@Column(name = "opis")
private String desc;
@Column(name = "tlo")
private String bg;
}
Service Class:
import intj.frontend.sellkon.model.SliderModel;
import intj.frontend.sellkon.repository.SliderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class SliderService {
@Autowired
private SliderRepository sliderRepository;
public List<SliderModel> getOpis(){
List<SliderModel> lstMovie = new ArrayList<SliderModel>();
lstMovie = sliderRepository.getOpis();
return lstMovie;
}
}
Repository Interace:
import intj.frontend.sellkon.model.SliderModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface SliderRepository extends JpaRepository<SliderModel,Long> {
@Query("SELECT opis FROM slider_res s")
List<SliderModel> getOpis();
}
Controller:
import intj.frontend.sellkon.Service.SliderService;
import intj.frontend.sellkon.model.SliderModel;
import intj.frontend.sellkon.repository.SliderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@CrossOrigin("*")
@RestController
@RequestMapping("/v1")
public class SliderController {
@Autowired
private SliderService sliderService;
@GetMapping
public List<SliderModel> getOpis(){
List<SliderModel> ls = sliderService.getOpis();
return ls;
}
}
data from mysql after executing the query
opis
desc
When i start app i have error: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sellkonApplication': Unsatisfied dependency expressed through field 'sliderRepository';
CRUD methods like findAll() are working properly. I am just starting my adventure with Java spirng, and this problem is difficult for me. Can someone tell what the problem is?
CodePudding user response:
Can you put here full stacktrace of error that you get?
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sellkonApplication': Unsatisfied dependency expressed through field 'sliderRepository';
It's only the top beginning of stacktrace.
CodePudding user response:
Actually the error is on the query.
A way to fix it would be to inform that it is a native query and that it is the value. Something like this.
I tested here, and it worked as expected.
@Query(value = "SELECT opis FROM slider_res s", nativeQuery = true)
List<String> getOpis();
or like this if you want to retrieve evertything from the table
@Query(value = "SELECT * FROM slider_res", nativeQuery = true)
List<SliderModel> getAll();
I prefer doing it like native queries, because if you are familiar with using SQL directly it will be more "readable".