Home > Mobile >  Unable to properly use h2 database in Spring framework
Unable to properly use h2 database in Spring framework

Time:12-22

I am new to Spring framework as well as H2 databases. My project is to create a CRUD operation backend to create/read/delete/update people. I was able to get all my functionalities to work and have tested it with postman, however when I access the database, my table is not being created nor is anything being added to it.

Below is my code:

People.java (where I am trying to create my People Table):

@Entity
@Table(name = "People")
public class People {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "people_id", length =20)
    private int peopleID;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;
    //setters and getters

application.properties:

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.h2.console.path=/h2
spring.datasource.generate-unique-name=false

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>3.0.0</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>net.javaguides.springboot</groupId>
   <artifactId>springboot-crud-hibernate-example</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>springboot-crud-hibernate-example</name>
   <description>spring boot crud operations example with hibernate</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-starter-validation</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>
      <dependency>
         <groupId>org.jetbrains</groupId>
         <artifactId>annotations</artifactId>
         <version>RELEASE</version>
         <scope>compile</scope>
      </dependency>
      <dependency>
         <groupId>org.jetbrains</groupId>
         <artifactId>annotations</artifactId>
         <version>RELEASE</version>
         <scope>compile</scope>
      </dependency>
   </dependencies>

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

</project

PLEASE any help would be EXTREMELY appreciated!!!!!

I tried changing the configuration multiple times but that would only result in the error "Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback." where I wont even have access to the database.

I also tried creating the table from within the database but it acts as a separate entity from my code and none of the functionalities works on it.

Whenever I use the create functionality on postman I get the correct response, but when I go to check my database to check if the person data has been added, nothing is there.

This is a snapshot of my database. Does not have People table created. [1]: H2 Console setup

Check Database H2 Console UI

  • Step 1 - Click on the table
  • Step 2 - RUN the query

CodePudding user response:

May be you have not used @transactional annotation over the methods present in dao which are updating , deleting, saving the data to your database because @transaction allow you to make physical changes

  • Related