0

I'm working to my first Spring JPA/Hibernate project. My DB is on postgresql but I have this error every time I start the app: Error creating bean with name 'tutorialController': Unsatisfied dependency expressed through field 'tutorialRepository': Error creating bean with name 'tutorialRepository' defined in com.example.postgresql.TutorialRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.postgresql.Tutorial. I have followed several tutorials on the web, but they all give me the same error. This is the code, I hope you can help me:

1.Tutorial.java

package com.example.postgresql;

import javax.persistence.*;

@Entity
@Table(name = "tutorials")
public class Tutorial {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "title")
private String title;

@Column(name = "description")
private String description;

@Column(name = "published")
private boolean published;

public Tutorial() {

}

public Tutorial(String title, String description, boolean published) {
    this.title = title;
    this.description = description;
    this.published = published;
}

public long getId() {
    return id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public boolean isPublished() {
    return published;
}

public void setPublished(boolean isPublished) {
    this.published = isPublished;
}

@Override
public String toString() {
    return "Tutorial [id=" + id + ", title=" + title + ", desc=" + description + ", published=" + published + "]";
}
 }
  1. TutorialRepository.java:

    package com.example.postgresql;
    
    import java.util.List;
    
    import org.springframework.data.jpa.repository.JpaRepository;
      import org.springframework.data.repository.CrudRepository;
      import org.springframework.stereotype.Repository;
    
     //This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
     //CRUD refers Create, Read, Update, Delete
    
     @Repository
     public interface TutorialRepository extends CrudRepository<Tutorial, Long> {
       List<Tutorial> findByPublished(boolean published);
    
            List<Tutorial> findByTitleContaining(String title);
          }
    

3.TutorialController.java:

package com.example.postgresql;


@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("/api")

public class TutorialController {
@Autowired
TutorialRepository tutorialRepository;

@GetMapping("/tutorials")
public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String title) {
    try {
        List<Tutorial> tutorials = new ArrayList<Tutorial>();

        if (title == null)
            tutorialRepository.findAll().forEach(tutorials::add);
        else
            tutorialRepository.findByTitleContaining(title).forEach(tutorials::add);

        if (tutorials.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }

        return new ResponseEntity<>(tutorials, HttpStatus.OK);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

@GetMapping("/tutorials/{id}")
public ResponseEntity<Tutorial> getTutorialById(@PathVariable("id") long id) {
    Optional<Tutorial> tutorialData = tutorialRepository.findById(id);

    if (tutorialData.isPresent()) {
        return new ResponseEntity<>(tutorialData.get(), HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
} ......ohter functions irrelevant to the problem....

4: PostgresApplication.java:

package com.example.postgresql;

@ComponentScan(basePackages = {"com.example.postgresql"})
@SpringBootApplication
public class PostgresqlApplication {
public static void main(String[] args) {
    SpringApplication.run(PostgresqlApplication.class, args);
}

}
Luigi V.
  • 79
  • 7

0 Answers0