11
votes

I am new to the Spring environment. I was trying to develop a basic MVC application using SpringBoot with Hibernate as ORM and MYSQL as database. I ran into lots of troubles setting-up the dependencies and the configurations. Currently, I was struck on the following error and I was not able to figure out how to get over it.

org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.

This is the set-up that I have in my application. It has no service layer and jsp pages to avoid the clutter

DATABASE:

MySql - A Database called Users is already present and it has a table called Users with a sample list of users

User.java(Model)

@Entity
@Table(name = "Users")
public class User {

    @Id
    @GeneratedValue
    public String id;

    public String username;

    public String firstname;

    public String lastname;

    public String password;
}

UserRepository:

@Repository
@Table(name = "Users")
public interface UserRepository extends JpaRepository<User, String> {
}

UserController:

@RestController
public class UserController {

    private UserRepository userRepository;

    @Autowired
    public UserController(UserRepository userRepository)
    {
        this.userRepository = userRepository;
    }

    @RequestMapping("user")
    public void getUser(@RequestParam("id") String id) {
         User user = userRepository.findOne(id);
    }

}  

Application.properties:

server.port: 9000

spring.datasource.url: jdbc:mysql://localhost/Users

spring.datasource.driverClassName: com.mysql.jdbc.Driver

spring.datasource.username: root

spring.datasource.password:

POM.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.3.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- HIBERNATE -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.0.Final</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- Spring ORM, works with Hibernate -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>

    <!-- MYSQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

</dependencies>

EDIT: Adding the main class

MAIN CLASS

@ComponentScan
@Configuration
@EnableAutoConfiguration
public class ApplicationStart {
    public static void main(String[] args)
    {
        SpringApplication.run(ApplicationStart.class, args);
    }
}

This is the current setup of my application. I don't even seem to know where to look for errors and the tutorials in the internet did not help my cause. So, any help on how to resolve the exception is much appreciated.

Please comment if more information is required.

Thanks-

2
Apparently it doesn't read your application.properties file. Make sure it is in the root of the class path (src/main/resources) or when on the file system next to your jar. In general you separate key/value pairs in a property file with = instead of : although the latter should work. I also don't really get your setup simply remove the exclude for hibernate and only include hibernate-entitymanager without a version as a dependency. Everything else will be taken care of.M. Deinum
@M.Deinum: If I remove that exclusion, I get the following exception- java.lang.IllegalArgumentException: Not an managed type: class models.User And, my application.properties is in the resources directory.Balasubramanian
That is more of an indication that there is something else wrong with your setup. Please add any additional configuration classes/files you have.M. Deinum
The EnableAutoConfiguration is trying to figure out which packages to scan for entities based on the package it is in. You might want to add a @EntityScan pointing to the package which contain your entity classes. Also note the ComponentScan will scan starting from the same package the ApplicationStart class is in. Maybe you can include your package structure in the post.M. Deinum
The problem was with the ApplicationStart not being in the same package as the controller, repository and the model. After clubbing them in the same package, things started working. I did not have to add the @EntityScan annotation as well. Thank you for the help :) Can you also add your last comment in your answer - it can help others to find it easily :)Balasubramanian

2 Answers

14
votes

Make sure that your application.properties is in one of the supported locations.

  1. A /config subdir of the current directory.
  2. The current directory
  3. A classpath /config package
  4. The class path root

The list is ordered by precedence (locations higher in the list override lower items).

Although separating your key/value pairs in a properties file with a : should work I suggest sticking to the more generally used = separator.

Your pom contains some unnecessary clutter which I suggest you move. You should only need the dependency on the mysql-connector-java everything else is clutter (the other dependencies are provided through the starter projects you depend on).

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- MYSQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
</dependencies>

These should be everything you need, versions and transitive dependencies are taken care of by the spring-boot-dependency pom.xml. (The grandparent of the starter-parent).

When using the @EnableAutoConfiguration annotation the class with the annotation will also be used to determine from which package to start scanning. In general you will put this annotation on your starter class. It is advisable to put this application class in a top level package (ie. your.package.application.StarterClass) all other packages should be sub package from that package. This way all classes will be automatically detected.

If that isn't possible you might need to add an additional @ComponentScan to specify a base package to start scanning from and a @EntityScan to specify the package(s) which contain your entities.

0
votes

I was also facing same issue but cause was very different, i tried every thing mentioned above in all answer taking 2 hour. But finally came to know that my file properties name 'Application.properties' is started with capital 'A'. it should be small 'A'. So please keep this in consideration also.