0

I am a beginner on spring-boot so i am trying out all the basic features. recently i started to build a small project but I am unable to connect to the spring-boot H2 embedded database. As per my knowledge, the table should be automatically available in the h2 - console. but for some reason i am not getting the table from the pojo.

TestController.java

package com.example.theboot.testPhase.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.example.theboot.testPhase.Repo.ProfRepositories;

@RestController
public class TestController {

@Autowired
ProfRepositories repo;
@GetMapping(path="/showme/{id}")
public String getShowMe(@PathVariable("id") int ids) {      
    int c = (int) repo.count();
    return "Count " + c;
}

@GetMapping(path="/showme")
public String getMe() {

    return "showmeWorking";
}
}

ProfRepositories.java

package com.example.theboot.testPhase.Repo;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.theboot.testPhase.vo.Profiles;

@Repository
public interface ProfRepositories extends CrudRepository<Profiles, String>{
}

Profiles.java

package com.example.theboot.testPhase.vo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table
public class Profiles {

@Id
@Column (name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
String ID;
@Column (name = "NAME")
String name;

@Override
public String toString() {
    return "Profiles [ID=" + ID + ", name=" + name + "]";
}

}

TestPhaseApplication.java

package com.example.theboot.testPhase;

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

@SpringBootApplication
public class TestPhaseApplication {

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

}

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     
http://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.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.theboot</groupId>
<artifactId>testPhase</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testPhase</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-rest</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>

</dependencies>

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

application.properties

spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.h2.console.path=/h2
spring.database.url=jdbc:h2:mem:A12

My sincere apologies for the poor editing. I am new here and i am finding this pretty difficult to handle. Thanks in advance browser h2 console project structure

  • @EnableJpaRepositories , try adding it over TestPhaseApplication main class . If you share your github repo code , we can look further – Kumar Pallav Jan 24 '19 at 11:10

1 Answers1

0

I recommend the documentation around sql databases and this other question.

Mostly adding the following line to your application.properties should resolve the problem.

spring.jpa.hibernate.ddl-auto = create

Spring Boot internally defaults this parameter value to create-drop if no schema manager has been detected, otherwise none for all other cases.

Alok
  • 36
  • 3