@Entity annotation is not automatically generating table in h2 database

Viewed 3567

the @Entity annotation is not automatically creating table in h2 database after using the annotation . What could be wrong ? I have troubleshoot and tried other ways but it won't create automatically .

My model is :


@Entity
public class UserAccount {

    @Id
    @GeneratedValue
    private int id;
    private String userName;
    private String email;
    @Temporal(TemporalType.DATE)
    private Date dateCreated;
    private Set <Transaction> transactions = new HashSet<>();

my h2 configuration details is :

      spring.h2.console.enabled=true
      spring.datasource.platform=h2
      spring.datasource.url=jdbc:h2:mem:test
      spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
      spring.jpa.hibernate.ddl-auto=create

My pom.xml file is :

     <?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>com.Wallet</groupId>
    <artifactId>Wallet</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Wallet</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>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>

</project>

Please help I am stuck , what could be wrong with my code?

2 Answers
  • @Entity: Defines the class as entity for ORM and provided name can be used in ORM specific queries (JPQL, HSQL).

  • @Table: This will be mapped with single table in database. So if you write any native queries, you can use this table name. (This is an entry point for ORM)

Note that you can keep different name attribute for either of the annotation. The mapping and conversion from one to another is taken care of by the framework.

In order to request Spring to create tables on your behalf, you can specify some properties in the application.yml file (or properties file). See below:

spring:
   datasource: 
     url: jdbc:h2:file:./test_db;DB_CLOSE_ON_EXIT=FALSE;IFEXISTS=FALSE;DB_CLOSE_DELAY=-1
     driverClassName: org.h2.Driver
     username: sa
     password:
     validationQuery: SELECT 1
   jpa:
     database-platform: org.hibernate.dialect.H2Dialect
     hibernate:
       ddl-auto: create

// If you want to view the tables in console
   h2:
     console:
       enabled: true
       path: /h2-console

Apart from this there are multitudes of other properties which can be specified. For instance:

spring:
  jpa:
    hibernate:
      naming:
         physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
         implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
   properties:
     hibernate.format_sql: true
     generate-ddl: true
     show-sql: true
  • Take a look at the docs here and some example of setting up H2 with Spring Boot.

  • Also, see Database initialization in Spring Boot.

  • You can use Flyway for database migration as in production it is not recommended that you create your schema by these properties.

One of the reason could be, if your entity classes are in a completely different package than your main application package.

This could prevent your entities from being scanned. As there would be no scanned entities, no tables would be inserted to your database, meaning you wouldn't even see any obvious exceptions or error messages.

If that is the case, you shall use @EntityScan annotation in your main application. For example

@EntityScan("your.package.with.annotated.entities")
public class SomeClassInOtherPackage{
    // ...
}
Related