1

I'm a newbie in java spring and hibernate. i use maven to manage my project, i can not understand what problem i have. It's very strange. I has stucked here for a week. This is full error

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:run (default-cli) on project demo: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active). -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

This is my project tree: project tree Config.java file:

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.dao.DepartmentDAO;
import com.daoimpl.DepartmentDAOImpl;


@Configuration
public class Config {

    @Bean
    public DepartmentDAO departmentDAO(){
    //      DepartmentDAOImpl departmentDaoImpl = new DepartmentDAOImpl();
            return new DepartmentDAOImpl();
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>test.component</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>DemoHibernate</name>
<description>Demo project for Spring Boot</description>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
            <dependency>
         <groupId>org.eclipse.persistence</groupId>
         <artifactId>eclipselink</artifactId>
         <version>2.5.1</version>
    </dependency>
    <dependency>
         <groupId>org.eclipse.persistence</groupId>
         <artifactId>javax.persistence</artifactId>
         <version>2.0.0</version>
         <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     <dependency>
         <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <version>1.3.156</version>
     </dependency>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

MainController.java

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dao.DepartmentDAO;
import com.entities.Department;

//@Controller
@RestController
public class MainController {
    @Autowired
    private DepartmentDAO departmentDAO;

    @RequestMapping({"/","/home","/index"})
    public String home(){
    return "index";
}

@RequestMapping({"/deptList"})
 public String deptList() {
     departmentDAO.createDepartment("Dept Name", "Dept Location");

     List<Department> list = departmentDAO.listDepartment();
     for (Department dept : list) {
         System.out.println("Dept No " + dept.getDeptNo());
     }
     //      model.addAttribute("departments", list);
     return "deptList";
 }
}

DepartmentDAO.java

package com.dao;

import java.util.List;
//import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Repository;

import com.entities.*;

//@Repository
//@Component
public interface DepartmentDAO {
    public List<Department> listDepartment() ;
    public Integer getMaxDeptId();
    public void createDepartment(String name,String location);  
}

DepartmentDAOImpl.java

package com.daoimpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.dao.DepartmentDAO;
import com.entities.Department;


@Transactional
public class DepartmentDAOImpl implements DepartmentDAO{

private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

@SuppressWarnings("unchecked")
@Override
public List<Department> listDepartment() {
    Session session = this.sessionFactory.getCurrentSession();
    List<Department> list = session.createQuery("from Department").list();
    return list;
}

@Override
public Integer getMaxDeptId() {
    Session session = this.sessionFactory.getCurrentSession();
    String sql = "Select max(d.deptId) from Department d";
    Query query = session.createQuery(sql);
    Integer maxDeptId = (Integer)query.uniqueResult();
    if (maxDeptId == null){
        return 0;
    }
    return maxDeptId;
}

@Override
public void createDepartment(String name, String location) {
    Integer deptId = getMaxDeptId() + 1;
    Department dept = new Department();
    dept.setDeptId(deptId);
    dept.setDeptNo("D" + deptId);
    dept.setDeptName(name);
    dept.setLocation(location);
    Session session = this.sessionFactory.getCurrentSession();
    session.persist(dept);
}

}

Department.java

package com.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


@Entity
@Table(name = "DEPARTMENT", uniqueConstraints = {
@UniqueConstraint(columnNames = { "DEPT_NO" }) })
public class Department {
private int deptId;
private String deptNo;
private String deptName;
private String location;

public Department(){

}

public Department(Integer deptId, String deptName, String location){ 
    this.deptId = deptId;
    this.deptNo = "D" + this.deptId;
    this.deptName = deptName;
    this.location = location;
}

@Id
@Column(name = "DEPT_ID")
public int getDeptId() {
    return deptId;
}

public void setDeptId(int deptId) {
    this.deptId = deptId;
}

@Column(name = "DEPT_NO", length = 20, nullable = false)
public String getDeptNo() {
    return deptNo;
}

public void setDeptNo(String deptNo) {
    this.deptNo = deptNo;
}

@Column(name = "DEPT_NAME", nullable = false)
public String getDeptName() {
    return deptName;
}

public void setDeptName(String deptName) {
    this.deptName = deptName;
}

@Column(name = "LOCATION")
public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}   


}

Department.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 12, 2016 12:33:11 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.entities.Department" table="DEPARTMENT">
    <id name="deptId" type="int">
        <column name="DEPTID" />
        <generator class="assigned" />
    </id>
    <property name="deptNo" type="java.lang.String">
        <column name="DEPTNO" />
    </property>
    <property name="deptName" type="java.lang.String">
        <column name="DEPTNAME" />
    </property>
    <property name="location" type="java.lang.String">
        <column name="LOCATION" />
    </property>
</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">ruoitrau95</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testdkt</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
</session-factory>
</hibernate-configuration>

Sorry if this is a stupid question, i'm a newbie and i can not fix it for a week. Thanks for every support. :)

Ruồi Trâu
  • 91
  • 2
  • 10
  • Possible duplicate of [Spring Boot - Cannot determine embedded database driver class for database type NONE](https://stackoverflow.com/questions/24074749/spring-boot-cannot-determine-embedded-database-driver-class-for-database-type) – walkeros Dec 30 '18 at 15:10

2 Answers2

0

The error says it cannot determine the database driver.

Cannot determine embedded database driver class for database type NONE.

Try to set explicitly the connection URL:

spring.datasource.url=jdbc:mysql://localhost:3306/testdkt

I found this at https://www.journaldev.com/13830/spring-boot-cannot-determine-embedded-database-driver-class-for-database-type-none

Hope it will help

fhery021
  • 317
  • 2
  • 7
-1

Even I have faced same issue, but once i included below class in src/main/java package, I was able to proceed.

@SpringBootApplication
public class Main {
    public static void main(String[] args) {

        SpringApplication.run(Main.class, args);

    }
}
Daniel Sixl
  • 2,488
  • 2
  • 16
  • 29