Skip to content

Commit 2887739

Browse files
committed
Spring boot hibernate with H2
1 parent b5e9017 commit 2887739

File tree

7 files changed

+245
-0
lines changed

7 files changed

+245
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.in28minutes.springboot.rest.example</groupId>
7+
<artifactId>spring-boot-2-jpa-with-hibernate-and-h2</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-2-jpa-with-hibernate-and-h2</name>
12+
<description>Spring Boot 2, Hibernate, JPA and H2 - Example Project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.3.1.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-devtools</artifactId>
41+
<scope>runtime</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.h2database</groupId>
45+
<artifactId>h2</artifactId>
46+
<scope>runtime</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-maven-plugin</artifactId>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
64+
<repositories>
65+
<repository>
66+
<id>spring-snapshots</id>
67+
<name>Spring Snapshots</name>
68+
<url>https://repo.spring.io/snapshot</url>
69+
<snapshots>
70+
<enabled>true</enabled>
71+
</snapshots>
72+
</repository>
73+
<repository>
74+
<id>spring-milestones</id>
75+
<name>Spring Milestones</name>
76+
<url>https://repo.spring.io/milestone</url>
77+
<snapshots>
78+
<enabled>false</enabled>
79+
</snapshots>
80+
</repository>
81+
</repositories>
82+
83+
<pluginRepositories>
84+
<pluginRepository>
85+
<id>spring-snapshots</id>
86+
<name>Spring Snapshots</name>
87+
<url>https://repo.spring.io/snapshot</url>
88+
<snapshots>
89+
<enabled>true</enabled>
90+
</snapshots>
91+
</pluginRepository>
92+
<pluginRepository>
93+
<id>spring-milestones</id>
94+
<name>Spring Milestones</name>
95+
<url>https://repo.spring.io/milestone</url>
96+
<snapshots>
97+
<enabled>false</enabled>
98+
</snapshots>
99+
</pluginRepository>
100+
</pluginRepositories>
101+
102+
103+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.alanbinu.springboot.jpa.hibernate.h2.example;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
10+
import com.alanbinu.springboot.jpa.hibernate.h2.example.student.Student;
11+
import com.alanbinu.springboot.jpa.hibernate.h2.example.student.StudentRepository;
12+
13+
@SpringBootApplication
14+
public class SpringBoot2JPAWithHibernateAndH2Application implements CommandLineRunner {
15+
16+
private Logger logger = LoggerFactory.getLogger(this.getClass());
17+
18+
@Autowired
19+
StudentRepository repository;
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(SpringBoot2JPAWithHibernateAndH2Application.class, args);
23+
}
24+
25+
@Override
26+
public void run(String... args) throws Exception {
27+
28+
logger.info("Student id 10001 -> {}", repository.findById(10001L));
29+
30+
logger.info("Inserting -> {}", repository.save(new Student("John", "A1234657")));
31+
32+
logger.info("Update 10003 -> {}", repository.save(new Student(10001L, "Name-Updated", "New-Passport")));
33+
34+
repository.deleteById(10002L);
35+
36+
logger.info("All users -> {}", repository.findAll());
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.alanbinu.springboot.jpa.hibernate.h2.example.student;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Student {
9+
@Id
10+
@GeneratedValue
11+
private Long id;
12+
private String name;
13+
private String passportNumber;
14+
15+
public Student() {
16+
super();
17+
}
18+
19+
public Student(Long id, String name, String passportNumber) {
20+
super();
21+
this.id = id;
22+
this.name = name;
23+
this.passportNumber = passportNumber;
24+
}
25+
26+
public Student(String name, String passportNumber) {
27+
super();
28+
this.name = name;
29+
this.passportNumber = passportNumber;
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String name) {
45+
this.name = name;
46+
}
47+
48+
public String getPassportNumber() {
49+
return passportNumber;
50+
}
51+
52+
public void setPassportNumber(String passportNumber) {
53+
this.passportNumber = passportNumber;
54+
}
55+
56+
@Override
57+
public String toString() {
58+
return String.format("Student [id=%s, name=%s, passportNumber=%s]", id, name, passportNumber);
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.alanbinu.springboot.jpa.hibernate.h2.example.student;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface StudentRepository extends JpaRepository<Student, Long>{
8+
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Enabling H2 Console
2+
spring.h2.console.enabled=true
3+
#Turn Statistics on
4+
spring.jpa.properties.hibernate.generate_statistics=true
5+
logging.level.org.hibernate.stat=debug
6+
# Show all queries
7+
spring.jpa.show-sql=true
8+
spring.jpa.properties.hibernate.format_sql=true
9+
logging.level.org.hibernate.type=trace
10+
11+
12+
spring.datasource.url=jdbc:h2:mem:testdb
13+
spring.data.jpa.repositories.bootstrap-mode=default
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
insert into student
2+
values(10001,'Ranga', 'E1234567');
3+
4+
insert into student
5+
values(10002,'Ravi', 'A1234568');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.alanbinu.springboot.jpa.hibernate.h2.example;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class SpringBoot2JPAWithHibernateAndH2ApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)