Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3771

Spring Boot Error: Consider defining a bean named 'entityManagerFactory' in your configuration

$
0
0

I am trying to learn and build a basic Spring Boot application using REST API, Java, and Oracle SQL. I'm stuck with this error. I've tried doing suggestions from internet (clean and install, jpa dependency), but I still could not fix this. I am so frustrated; please help me, I want to learn. Many thanks!

Here's the error message in console log:

Field userRepository in com.example.userApi.controller.UserController required a bean named 'entityManagerFactory' that could not be found.

The injection point has the following annotations:- @org.springframework.beans.factory.annotation.Autowired(required=true)

Action: Consider defining a bean named 'entityManagerFactory' in your configuration.

Here is my code for reference.

UserApiApplication.java

package com.example.userApi;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.domain.EntityScan;import org.springframework.context.annotation.ComponentScan;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;@SpringBootApplication@EnableJpaRepositories(basePackages = "com.example.userApi.repository")public class UserApiApplication {    public static void main(String[] args) {        SpringApplication.run(UserApiApplication.class, args);    }}

UserController.java

package com.example.userApi.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import com.example.userApi.model.User;import com.example.userApi.repository.UserRepository;import java.util.List;@RestController@RequestMapping("/api/users")public class UserController {    @Autowired    private UserRepository userRepository;    // Create User    @PostMapping    public User createUser(@RequestBody User user) {        return userRepository.save(user);    }    // Get All Users    @GetMapping    public List<User> getAllUsers() {        return userRepository.findAll();    }    // Get User by ID    @GetMapping("/{id}")    public User getUserById(@PathVariable Long id) {        return userRepository.findById(id).orElseThrow(() -> new RuntimeException("User not found"));    }    // Update User    @PutMapping("/{id}")    public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {        User user = userRepository.findById(id).orElseThrow(() -> new RuntimeException("User not found"));        user.setFirstName(userDetails.getFirstName());        user.setLastName(userDetails.getLastName());        user.setAge(userDetails.getAge());        user.setOccupation(userDetails.getOccupation());        return userRepository.save(user);    }    // Delete User    @DeleteMapping("/{id}")    public String deleteUser(@PathVariable Long id) {        User user = userRepository.findById(id).orElseThrow(() -> new RuntimeException("User not found"));        userRepository.delete(user);        return "User deleted successfully";    }}

User.java

package com.example.userApi.model;import javax.persistence.*;@Entitypublic class User {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    @Column(nullable = false)    private String firstName;    @Column(nullable = false)    private String lastName;    @Column    private int age;    @Column    private String occupation;    // Getters and Setters    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getFirstName() {        return firstName;    }    public void setFirstName(String firstName) {        this.firstName = firstName;    }    public String getLastName() {        return lastName;    }    public void setLastName(String lastName) {        this.lastName = lastName;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getOccupation() {        return occupation;    }    public void setOccupation(String occupation) {        this.occupation = occupation;    }}

UserRepository.java

package com.example.userApi.repository;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.stereotype.Repository;import com.example.userApi.model.User;@Repositorypublic interface UserRepository extends JpaRepository<User, Long> {}

application.properties. This might also help

server.port=5052# Oracle Database Configurationspring.datasource.url=jdbc:oracle:thin:@localhost:1521:XEspring.datasource.username=restspring.datasource.password=restspring.datasource.driver-class-name=oracle.jdbc.OracleDriver# Hibernate Configurationspring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialectspring.jpa.hibernate.ddl-auto=update

pom.xml

<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>com.example</groupId><artifactId>userApi</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>userApi</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.7.5</version></dependency><!-- Spring Boot Starter Data JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><version>2.7.5</version></dependency><!-- Oracle JDBC Driver --><dependency><groupId>com.oracle.database.jdbc</groupId><artifactId>ojdbc8</artifactId><version>19.16.0.0</version><scope>runtime</scope></dependency><dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.2.3.Final</version> </dependency><dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency></dependencies></project>

Viewing all articles
Browse latest Browse all 3771

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>