0

I was trying to use a spring boot initializer using intelij IDEA. I used dependancies like spring-web, spring data-JPA and H2 sql. Afrter implementing the necessary steps I was able to get the H2 console running and sucessfull database connection. But, the entity I created is not apearing in the database. [h2_console after running my java application]

I tried the following steps:

  1. create my project by adding the dependancies I mentioned above.(The main application class)

     ```java
    
     package com.example.demo;
    
     import org.springframework.boot.SpringApplication;
     import org.springframework.boot.autoconfigure.SpringBootApplication;
    
     @SpringBootApplication
     public class Demo5Application {
    
         public static void main(String[] args) {
             SpringApplication.run(Demo5Application.class, args);
         }
    
     }
    
     ```
    
  2. create the packege for defining my entity nemed "entity".[package_entity]

  3. Then I created a class called "Book" to define my entity properties.[entity_properties]

     ```java
     package com.example.demo.entity;
    
     import javax.persistence.*;
    
     @Entity
     public class Book {
    
         @Id
         @GeneratedValue(strategy = GenerationType.AUTO)
         private Integer id;
         private String name;
         private String category;
         private String sector;
         private String type;
    
         public Integer getId() {
         return id;
     }
    
     public void setId(Integer id) {
         this.id = id;
     }
    
     public String getName() {
         return name;
     }
    
     public void setName(String name) {
         this.name = name;
     }
    
     public String getCategory() {
         return category;
     }
    
     public void setCategory(String category) {
         this.category = category;
     }
    
     public String getSector() {
         return sector;
     }
    
     public void setSector(String sector) {
         this.sector = sector;
     }
    
     public String getType() {
         return type;
     }
    
     public void setType(String type) {
         this.type = type;
       }
      }
      ```
    
  4. provide them their getters and setters.(as shown above)

  5. Use the proper annotaions for each parts.(@Entity, @ID, @GeneratedValue) 6- Then, I go to the application.properties to add more information about my H2 database.[application_properties]

     spring.datasource.driver-class-name=org.h2.Driver
     spring.datasource.url=jdbc:h2:mem:sarita
     spring.datasource.username=SA
     spring.datasource.password=
    
     spring.h2.console.enabled=true
     spring.h2.console.path=/h2
    
     spring.jpa.show-sql=true
     spring.jpa.hibernate.ddl-auto=create  
    

After all those steps I run my application. The connection is showing my success.[successfull_h2_db_connection]

But, when I hit connect it will get inside. However, there is no table created inside as I expected!!![no_table_created]

In all the tutorials I saw they were able to see this table in their H2 console. What mistake did I made for my failure?

Thanks!!!

  • Hello, please paste your code as formatted snippets. 1. Main application class with package definition 2. Entity class with package definition 3. application.properties – Mar-Z Apr 12 '23 at 15:44
  • We certainly need the above mentioned to help further. When looking at those tutorials, take a look at the setting they have used, in particular any that may hint at dealing with schema creation, DDL etc as this is commonly missed. Spring/JPA won't create schemas/Tables in the DB unless you configure it to do so. Otherwise, check your class paths etc so that the entity is found when it loads the persistence unit and connects to the database, and the logs, as they may hint at what is or isn't happening. – Chris Apr 12 '23 at 16:55
  • Let me guess. you are using the regular H2 database which is an **in-memory** dtabase. Which means as soon as your application is down, the database is gone. It also means you cannot connect to it from an external console as it is only internal to the JVM. If that is not the case there is too little information here to really dig into. – M. Deinum Apr 12 '23 at 18:38
  • Ok. Thanks for the reply. I edited my question based on your reply. This is my first time askin a question on stack overflow. I am looking forward to see your answer. Regards!!! – Adonias Andarge Apr 13 '23 at 06:07

2 Answers2

0

As other has mentioned, you didn't mentioned hibernate/jpa to create your database tables. You have only created a connection to your in-memory database.

you need to define your jpa actions (like using jpa template: https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa) to have a crud (create, read, update, delete). However, you need to create your database structure (unless you tell jpa to create your database schema).

A common library that is supported under spring is liquibase(https://www.baeldung.com/liquibase-refactor-schema-of-java-app). it will create your schema every time you run your app.

Another option is to have your h2 pointing to a file persisted database. you may use some responses from How to store H2 database file into project directory.

0

Add data.sql and schema.sql under src/resources folder in your project. schema.sql should have create table sql queries

data.sql should have the insert queries.

This should help jpa to create and populate data in your in memory db.

alea
  • 980
  • 1
  • 13
  • 18