0

I am trying to create a very simple REST service using Spring Boot MySQL. But unable to get the around the error. I have done a lot of searches and most results told me that the below code is enough to build a bare-bones REST service that can perform CRUD operation.

I keep getting the error

***************************
APPLICATION FAILED TO START
***************************
Description:
Field repo in org.bluedolphin.spring.data.mysql.ActorController required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.

And once i add the line

@EnableJpaRepositories(basePackages="org.bluedolphin.spring.data.mysql", entityManagerFactoryRef="emf")

in the file SpringDataMysqlApplication, I start getting the following error

***************************
APPLICATION FAILED TO START
***************************
Description:
Field repo in org.bluedolphin.spring.data.mysql.ActorController required a bean named 'emf' that could not be found.
Action:
Consider defining a bean named 'emf' in your configuration.

The entire code is as below.

application.properties

#Server details
server.port=8180
# Replace with your connection string
spring.datasource.url=jdbc:mysql://localhost:3307/sakila
# Replace with your credentials
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver

SpringDataMysqlApplication

@SpringBootApplication
@EnableJpaRepositories(basePackages="org.bluedolphin.spring.data.mysql", entityManagerFactoryRef="emf")
public class SpringDataMysqlApplication {

    private static final Logger log = LoggerFactory.getLogger(SpringDataMysqlApplication.class);

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

Actor

@Entity
@Table(name = "actor")
public class Actor {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String first_name;
    private String last_name;
    private Date last_update;

    public Actor(int id, String first_name, String last_name, Date last_update) {
        super();
        this.id = id;
        this.first_name = first_name;
        this.last_name = last_name;
        this.last_update = last_update;
    }

    public Actor() {
        super();
    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public Date getLast_update() {
        return last_update;
    }

    public void setLast_update(Date last_update) {
        this.last_update = last_update;
    }

    @Override
    public String toString() {
        return "Actor [id=" + id + ", first_name=" + first_name + ", last_name=" + last_name + ", last_update="
                + last_update + "]";
    }
}

ActorRepository

public interface ActorRepository extends CrudRepository<Actor, Long> {

}

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<groupId>org.bluedolphin.spring.data.mysql</groupId>
    <artifactId>spring-data-mysql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>spring-data-mysql</name>
    <description>spring-data-mysql</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>
Sudeep Hazra
  • 118
  • 15

1 Answers1

1

the error message is self explanatory, you missed to define the entity manager factory bean. You can define it like below in spring config xml file.

<bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="DataPersistencePersistenceUnit" />
    <property name="dataSource" ref="yourDataSource" />
</bean>

If in your spring boot application you don't have any spring config file, then just use EnableJpaRepositories without explicitly giving a reference to entity manager.

@EnableJpaRepositories(basePackages="org.bluedolphin.spring.data.mysql")
Satish
  • 1,037
  • 1
  • 13
  • 20
  • I do not have a spring config file and prefer to do it using the annotations. Tried adding the `@EnableJpaRepositories` annotation as suggested but same error. :( – Sudeep Hazra Jan 29 '17 at 15:30
  • Have a look at [this thread](http://stackoverflow.com/questions/33074547/spring-boot-configure-entitymanager) – Satish Jan 30 '17 at 15:40