0

For a project I have to create a database using spring-boot, hibernate and mysql. I don't have an error per say, just the database does not get created without an error. I am not sure, however why and how I can fix it.

Logs I get when I run the app:


2019-10-13 20:24:02.652  INFO 20288 --- [           main] D.p.t.D.p.try1.DsProiectTry1Application  : Starting DsProiectTry1Application on DESKTOP-3KOFPIQ with PID 20288 (started by Diana in D:\AN 4\PROIECTE DS INCERCARI\DS-proiect-try1)
2019-10-13 20:24:02.652  INFO 20288 --- [           main] D.p.t.D.p.try1.DsProiectTry1Application  : No active profile set, falling back to default profiles: default
2019-10-13 20:24:03.539  INFO 20288 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-10-13 20:24:03.557  INFO 20288 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8ms. Found 0 repository interfaces.
2019-10-13 20:24:04.094  INFO 20288 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$2d7cd334] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-13 20:24:04.141  INFO 20288 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$acfd2066] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-13 20:24:04.676  INFO 20288 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-10-13 20:24:04.707  INFO 20288 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-13 20:24:04.707  INFO 20288 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.26]
2019-10-13 20:24:04.816  INFO 20288 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-10-13 20:24:04.832  INFO 20288 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2117 ms
2019-10-13 20:24:05.050  INFO 20288 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-10-13 20:24:05.253  INFO 20288 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-10-13 20:24:05.316  INFO 20288 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-10-13 20:24:05.378  INFO 20288 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.12.Final}
2019-10-13 20:24:05.378  INFO 20288 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-10-13 20:24:05.509  INFO 20288 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-10-13 20:24:05.616  INFO 20288 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2019-10-13 20:24:05.788  INFO 20288 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@ff21443'
2019-10-13 20:24:05.788  INFO 20288 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-10-13 20:24:06.366  INFO 20288 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-13 20:24:06.413  WARN 20288 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-10-13 20:24:06.559  WARN 20288 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2019-10-13 20:24:06.900  INFO 20288 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-13 20:24:06.900  INFO 20288 --- [           main] D.p.t.D.p.try1.DsProiectTry1Application  : Started DsProiectTry1Application in 4.611 seconds (JVM running for 5.055)

I have tried several changes on the application properties files and hibernate.cfg.xml( yet it still is not recognised) but none were helping/ made some changes .

My application properties files is :


# Connection url for the database "netgloo_blog"
spring.datasource.url = jdbc:mysql://localhost:3306/mydbds?useSSL=false&autoReconnect=true

# Username and password
spring.datasource.username = root
spring.datasource.password =

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = create-drop
spring.datasource.driverClassName=com.mysql.jdbc.Driver
server.port=8080

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect

My pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>DS.proiect.try1</groupId>
    <artifactId>DS-proiect-try1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>DS-proiect-try1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.2.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

    </dependencies>

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

</project>

my hibernate.cfg.xml file:

<?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>
        <!-- SQL Dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.org.hibernate.dialect.MySQLDialect</property>

        <!-- Database Connection Settings -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydbds</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>

        <!-- Mapping With Model Class Containing Annotations -->
        <mapping class=" DS.proiect.try1.DS.proiect.try1.Entities.Doctor" />
    </session-factory>
</hibernate-configuration>

My intelij project strcuture is :https://i.sstatic.net/b6wOt.jpg

Some of my tables from the entity package :


@Entity
@Table(name="Caregiver")
public class Caregiver {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "idCaregiver", unique=true, nullable = false)
    private int idCaregiver;

    @Column(name="caregiverName")
    private String caregiverName;

    @Column(name="caregiverSurname")
    private String caregiverSurname;

    @Column(name="caregiverGender")
    private String caregiverGender;

    @Column(name="caregiverAddress")
    private String caregiverAddress;

    @Column(name="caregiverBirthdate")
    private String caregiverBirthdate;

    @Column(name="caregiverEmail")
    private String caregiverEmail;

    @Column(name="caregiverPassword")
    private String caregiverPassword;

    @OneToMany(mappedBy = "Caregiver_idCaregiver", cascade = CascadeType.ALL,fetch= FetchType.LAZY)
    private Set<Patient> patientList;



@Entity
@Table(name = "Patient")
public class Patient {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "idPatient", unique=true, nullable = false)
    private int idPatient;

    @Column(name="patientName")
    private String patientName;

    @Column(name="patientSurname")
    private String patientSurname;

    @Column(name="patientGender")
    private String patientGender;

    @Column(name="patientBirthdate")
    private Date patientBirthdate;

    @Column(name="patientAddress")
    private String patientAddress;

    @Column(name="patientEmail")
    private String patientEmail;

    @Column(name="patientPassword")
    private String patientPassword;

    @ManyToOne
    @JoinColumn(name="Caregiver_idCaregiver")
    Caregiver Caregiver_idCaregiver;

and the relationship between them should be something like this:https://imgur.com/undefined

I should have the whole database created by spring-boot but it does not work like that and I have no idea why. Any help is appreciated, thank you!

1
  • Try adding the following property along with the @Jailson Evora's answer jdbc:mysql://localhost:3306/mydbds?useSSL=false&autoReconnect=true&createDatabaseIfNotExist=true .This will create the database if it is not present. Commented Oct 13, 2019 at 18:40

3 Answers 3

1

In application.properties change to spring.datasource.url = jdbc:mysql://localhost:3306/yourDB?useSSL=false and spring.jpa.hibernate.ddl-auto = create.

Use this dialect spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect and this dependency

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>

With SpringBoot you do not need anymore to use the hibernate.cfg.xml. SpringBoot will do this job for us avoiding boilerplate code. Just use application properties to set your app...

To create your DB with SpringBoot bear in mind that you need just three setting step:

1 - MySql Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)

spring.datasource.url = jdbc:mysql://localhost:3306/YOURBD?useSSL=false
spring.datasource.username = root
spring.datasource.password = root

2 - Hibernate Properties

The SQL dialect makes Hibernate generate better SQL for the chosen database

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

And the last one

3 - Hibernate ddl auto (create, create-drop, validate, update)

spring.jpa.hibernate.ddl-auto = update or create

and do not forget the dependency..

Sign up to request clarification or add additional context in comments.

1 Comment

I have that dependency. I have done the changes and nothing happens. No errors but no DB either.
0

You need to pass all hibernate property valus in application.properties file

Comments

0

Spring boot does not create the database. Spring boot connect the database and create and update tables as per your java entity class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.