Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a MVC app using Spring Boot + Hibernate + MySql

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-

like image 328
Balasubramanian Avatar asked Jul 01 '14 16:07

Balasubramanian


People also ask

Can we use Hibernate with spring MVC?

We can simply integrate hibernate application with spring application. In hibernate framework, we provide all the database information hibernate. cfg. xml file.

Can we use spring MVC and Spring Boot together?

Yes, you can use Spring MVC with Spring Boot. To create and run a Spring MVC web application in spring boot, you need to add the spring-boot-starter dependency in your pom. xml file.


1 Answers

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.

like image 57
M. Deinum Avatar answered Sep 24 '22 12:09

M. Deinum