1
votes

I am in the process of developing a Spring Boot application and came across this error when starting the server. I am not sure if I am incorrectly defining any annotations or missing any dependencies. Any help would be appreciated.

Main class:

@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration

public class DemoApplication {

    public static void main(String[] args) {
        System.out.println("Hello world");
        SpringApplication.run(DemoApplication.class, args);
    }
}

UserService class:

@Service
public class UserService implements IUserService {

    @Autowired
    private UserDAO userDAO;

    @Override
    public List<UserDTO> getAllUsers() {

        List<User> entities = userDAO.getUsers();
        List<UserDTO> users = new ArrayList<UserDTO>();//Will never use directly User Object, will be using Tranferable objects

        Iterator<User> iterator = entities.iterator();

        while(iterator.hasNext()) {
            User user = iterator.next();
            users.add(ApiDTOBuilder.userToUserDTO(user));//We are building UserDTO object.
        }
        return users;
    }

    @Override
    public UserDTO getUserByUsername(String username) {
        User user = userDAO.getUser(username);
        return ApiDTOBuilder.userToUserDTO(user);
    }

    @Override
    public void createUser(UserDTO user) {
        userDAO.createUser(ApiDTOBuilder.userDTOToUser(user));
    }

    @Override
    public void updateUser(UserDTO user) {
        userDAO.updateUser(ApiDTOBuilder.userDTOToUser(user));

    }

    @Override
    public void deleteUser(String username) {
        userDAO.deleteUser(username);
    }
}

UserDAO class:

@Repository public class UserDAO implements IUserDAO {

        @PersistenceContext
        EntityManager em;//JPA EntityManager is used to access a database in a particular application. It is used to manage persistent entity instances, to find entities by their primary key identity(As here is Username), and to query over all entities.

        @Override
        @Transactional
        public User getUser(String username) {
            return em.find(User.class, username); //Here entitymanager can find the username as it has the capability to find an entity by unique identifies
        }


        @Override
        @Transactional
        public List<User> getUsers() {
            List<User> resultList = em.createQuery("FROM user_tbl", User.class).getResultList();
            return resultList;
        }

        @Override
        @Transactional
        public void createUser(User user) {
            em.persist(user);//Make an instance managed and persistent.
        }

        @Override
        @Transactional
        public void updateUser(User user) {
            em.merge(user);//Merge the state of the given entity into the current persistence context.
        }

        @Override
        @Transactional
        public void deleteUser(String username) {
            User user = this.getUser(username);
            em.remove(user);//Remove the entity instance.
        }
}

Build.gradle:

buildscript {
    ext {
        springBootVersion = '2.0.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final')
    compile('javax.xml.bind:jaxb-api:2.2.11')
    compile('org.springframework.boot:spring-boot-starter-test:2.0.2.RELEASE')
    compile('com.sun.xml.bind:jaxb-core:2.2.11')
    compile('com.sun.xml.bind:jaxb-impl:2.2.11')
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Error message:

***************************
APPLICATION FAILED TO START
***************************
Description:

Field userDAO in com.example.demo.service.UserService required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.
Action:

Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.

I saw all the relevant answers and tried to implement those suggestions like adding dependencies, adding notations in the main class, etc. But it shows the same error. Please someone help me out. Thank you in advance.

P.S: Github link of my application: https://github.com/heliOpenxCell/demo

1
Why do you have this line in application.properties: spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration - Simon Martinelli
That's the problem. Also, start using Spring data. It's a lot easier than creating custom DAOs with sql like queries. You should never have to inject entity manager anywhere when you are using spring boot. - horatius
Additionally there is a lot of unnecessary configuration on the Main Class and in the WebConfiguration Class. Plus there are superfluous gradle dependencies. Why don't you keep the things like Spring Initializer is creating it? - Simon Martinelli
In spring data, you never have to explicitly configure EntityManager. If you are doing that, you are using the whole library wrong. Also use the Repository pattern. DAO pattern is old and well past it's sell by date for relational databases. - horatius
What if I want to create persistent context ? How can I create and pass into the Tasklet ? - Jeff Cook

1 Answers

-3
votes

i got the same your problem, just add this to your maven or gradle, it work for me!

Maven:

<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jdbc</artifactId>
  <version>9.0.10</version>
</dependency>

Gradle:

compile group: 'org.apache.tomcat', name: 'tomcat-jdbc', version: '9.0.10'