简体   繁体   中英

Problem creating table in H2 database with SpringBoot

My greetings for respectful users I'm newbie with spring boot and i want just create simple table in H2 database this is the model class

@Entity
public class Human 

{

    @Id
    private long id;


    private String firstname;

    private String lastname;

        private String email;

        public long getId() {
            return id;
        }




        public void setId(int id) {
            this.id = id;
        }
        public String getFirstname() {
            return firstname;
        }
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
        public String getLastname() {
            return lastname;
        }
        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
}

and this is pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>demo-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-1</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-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

and application.properties:

spring.h2.console.enabled= true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:file:~/user
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

until here the app works well, the only problem that the table doesn't show in H2 console (I don't know if it's really created or not but when I use H2 console the table was created instantly)

So please what's the problem could be? Any help is appreciated

There are a couple of things that you need to check and verify to ensure this works.

  • Make sure to define the repository layer correctly.Also ensure you have provided all the correct annotations.
  • If you have create queries for the tables make sure that you put these queries in a file name schema.sql .Also, ensure that the file is put under the directory src/main/resources/ folder.But when providing the schema.sql make sure to disable the spring.jpa.hibernate.ddl-auto as both won't work at the same time as intented.

In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql, but you cannot do both. Make sure to disable spring.jpa.hibernate.ddl-auto if you use schema.sql.

  • If you have any data updation queries to be executed put them inside a file names data.sql .Also, ensure that the file is put under the directory src/main/resources/ folder.This will be executed after the ddl-auto creates the tables.

Repository layer like:

@Repository
public interface HumanRepository extends JpaRepository<Human, Integer> {

}

As @sovannarith cheav said you need to provide the property in application.properties or application.yml like:

spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

The spring.jpa.hibernate.ddl-auto property can have different values depending upon your use-case.Values for this property can be:

validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema at the end of the session.

If this didn't work perhaps springboot is not identifying H2, so try setting the following:

spring.datasource.platform=h2
spring.datasource.initialization-mode=always

Official Doc

The solution here is just adding annotation for main class

@EntityScan("com.example.model")

So here Spring will scan the package com.example.model and connet with H2 database then create the table

The table human of origins Human entity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM