Field repository in [package] required a bean named 'entityManagerFactory' that could not be found

18 Views Asked by At

I am working with Spring Boot to connect to MySQL database without using H2 (with in28minutes course). But I get error like this:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource can be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

No matter how many ways I try, it's still the same.

LearnMySqlDockerApplication.java :

package com.in28min.LearnMySQLDocker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication(exclude =  {DataSourceAutoConfiguration.class })
@ComponentScan("com.in28min.LearnMySQLDocker.courses")
@Configuration
@EnableJpaRepositories(basePackages = "com.in28min.LearnMySQLDocker.courses.repository")
public class LearnMySqlDockerApplication {

    public static void main(String[] args) {
        SpringApplication.run(LearnMySqlDockerApplication.class, args);
    }

}

CourseRepository.java :

package com.in28min.LearnMySQLDocker.courses.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import com.in28min.LearnMySQLDocker.courses.bean.Course;

public interface CourseRepository extends JpaRepository<Course, Long>{

}

CourseController.java :

package com.in28min.LearnMySQLDocker.courses.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.in28min.LearnMySQLDocker.courses.bean.Course;
import com.in28min.LearnMySQLDocker.courses.repository.CourseRepository;

@RestController
public class CourseController {
    @Autowired
    private CourseRepository repository;
    
    // http://localhost:8080/courses
    @GetMapping("/courses")
    public List<Course> getAllCourses() {       
        return repository.findAll();
    }
    
    // http://localhost:8080/courses/1
    @GetMapping("/courses/{id}")
    public Course getCourseDetails(@PathVariable long id) {
        return repository.findById(id).get();
    }
    
    /*
    POST http://localhost:8080/courses
    {
          "name": "Learn DevOps",
          "author": "in28minutes"
    }*/

    //POST - Create a new resource (/courses)
    @PostMapping("/courses")
    public void createCourse(@RequestBody Course course){
        repository.save(course);        
    }
    
    /*
    PUT - http://localhost:8080/courses/100001
    {
         "id": 100001,
         "name": "Learn Microservices 2",
         "author": "in28minutes"
        }
    */
    
    //PUT - Update/Replace a resource (/courses/1)
    @PutMapping("/courses/{id}")
    public void updateCourse(@PathVariable long id, @RequestBody Course course){
        repository.save(course);        
    }

    
    //DELETE - Delete a resource (/courses/1)
    @DeleteMapping("/courses/{id}")
    public void deleteCourse(@PathVariable long id){
        repository.deleteById(id);
    }
    

//  docker run --detach 
//  --env MYSQL_ROOT_PASSWORD=dummypassword 
//  --env MYSQL_USER=courses-user 
//  --env MYSQL_PASSWORD=dummycourses 
//  --env MYSQL_DATABASE=courses 
//  --name mysql
//  --publish 3306:3306 mysql:5.7
}

Course.java :

package com.in28min.LearnMySQLDocker.courses.bean;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

@Entity
public class Course {
    @Id
    @GeneratedValue
    private long id;
    
    //@Column(name="course_name")
    private String name;

    private String author;
    
    public Course() {
    }

    public Course(long id, String name, String author) {
        super();
        this.id = id;
        this.name = name;
        this.author = author;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    @Override
    public String toString() {
        return "Course [id=" + id + ", name=" + name + ", author=" + author + "]";
    }
}

applications.properties

spring.application.name=Learn-MySQL-Docker
#logging.level.org.springframework=DEBUG
#management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.include=health,metrics
#spring.datasource.url=jdbc:h2:mem:testdb

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/courses
spring.datasource.username=courses-user
spring.datasource.password=dummycourses
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect



#courses-user@localhost:3306

And in pom.xml, i've added dependencies: spring-boot-starter-data-jpa, mysql-connector-j, hibernate-entitymanager, hibernate-entitymanager, mysql-connector-java, spring-boot-starter-web

Can you guys help me? Thank you very much

I've try to use many solution but i stuck in 2 problem: "Field repository in com.in28min.LearnMySQLDocker.courses.controller.CourseController required a bean named 'entityManagerFactory' that could not be found." "Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."

0

There are 0 best solutions below