0

Im new in spring boot. I tried creating table in H2 using @Entity, but it doesn't appear in H2 console. Im using Eclipse for developing SpringBoot Application. application.properties file:

server.port = 7070
spring.mvc.view.prefix = /views/
spring.mvc.view.suffix = .jsp

#h2
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

Movie.java file: Also used @Id annotation

package com.controller.model;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Movie {
    @Id
    private int movieId;
    private String movieName;
    private String movieCollection;
    
    
    public Movie() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    
    public Movie(int movieId, String movieName, String movieCollection) {
        super();
        this.movieId = movieId;
        this.movieName = movieName;
        this.movieCollection = movieCollection;
    }


    public int getMovieId() {
        return movieId;
    }
    public void setMovieId(int movieId) {
        this.movieId = movieId;
    }
    public String getMovieName() {
        return movieName;
    }
    public void setMovieName(String movieName) {
        this.movieName = movieName;
    }
    public String getMovieCollection() {
        return movieCollection;
    }
    public void setMovieCollection(String movieCollection) {
        this.movieCollection = movieCollection;
    }
    @Override
    public String toString() {
        return "Movie [movieId=" + movieId + ", movieName=" + movieName + ", movieCollection=" + movieCollection + "]";
    }
    
    

}

pom.xml file:

<?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>3.2.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>MiniProject</groupId>
    <artifactId>MovieWeb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MovieWeb</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
    
    <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>8.5.30</version>
</dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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.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>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

MovieWebApplication.java file: class with main method

package com.library;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MovieWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(MovieWebApplication.class, args);
    }

}

Please help if you got any solution.

1 Answers1

0

There are two solutions:

You may add further annotations to the Movie entity class.

@Entity
@Table(name = "movie")
public class Movie {
    @Id
    private int movieId;
    @Column(name = "movie_name")
    private String movieName;
    @Column(name = "movie_collection")
    private String movieCollection;
    
..
}
  • Optional - You can add some sequence generator to the Id field to generate a specific sequence. See this post for more details
  • Optional - In the @Column() for columns, you can add further constraints such as nullable = false, length, etc. depending on your requirements.

OR

You may add @EntityScan("com.example.model") to your main class. This will scan entities in the specified package and create them for you.

Finally, you may need to add this property spring.jpa.hibernate.ddl-auto in your application.properties if you are not using migration frameworks such as Liquibase, Flyway, etc. See this post for more details.

SwathiP
  • 315
  • 3
  • 5
  • Thanks @SwathiP, While going through @EntityScan(), I came to know I misspelt package name for Movie class(com.controller.model instead of com.library.model). Thank you for your response. It rlly worked for me after getting worried for about 2 hours :) – Prajal Saxena Jul 28 '23 at 20:14
  • Glad, it worked out! – SwathiP Jul 28 '23 at 20:23