From 1137565f01217de89de86cb1a54d54682765acb6 Mon Sep 17 00:00:00 2001 From: Bipin kumar Date: Wed, 19 Apr 2023 20:25:45 +0530 Subject: [PATCH] JAVA-19787 : Adding example for h2 default sql schema and data initialization (#13863) --- .../dataloading/ApplicationDataLoading.java | 20 ++++++++ .../com/baeldung/dataloading/model/User.java | 49 +++++++++++++++++++ .../application-datasource.properties | 11 +++++ 3 files changed, 80 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/dataloading/ApplicationDataLoading.java create mode 100644 persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/dataloading/model/User.java create mode 100644 persistence-modules/spring-boot-persistence/src/main/resources/application-datasource.properties diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/dataloading/ApplicationDataLoading.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/dataloading/ApplicationDataLoading.java new file mode 100644 index 0000000000..619f0e0411 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/dataloading/ApplicationDataLoading.java @@ -0,0 +1,20 @@ +package com.baeldung.dataloading; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.AbstractEnvironment; + +import com.baeldung.boot.Application; + +@SpringBootApplication(scanBasePackages = { "com.baeldung.dataloading" }) +public class ApplicationDataLoading { + private static ApplicationContext applicationContext; + + public static void main(String[] args) { + System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "datasource"); + + applicationContext = SpringApplication.run(Application.class, args); + } + +} diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/dataloading/model/User.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/dataloading/model/User.java new file mode 100644 index 0000000000..e0927f3646 --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/dataloading/model/User.java @@ -0,0 +1,49 @@ +package com.baeldung.dataloading.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "users") +public class User { + + @Id + @GeneratedValue + private Integer id; + private String name; + private Integer status; + + public User() { + } + + public User(String name, Integer status) { + this.name = name; + this.status = status; + } + + 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 Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } +} diff --git a/persistence-modules/spring-boot-persistence/src/main/resources/application-datasource.properties b/persistence-modules/spring-boot-persistence/src/main/resources/application-datasource.properties new file mode 100644 index 0000000000..11f409719f --- /dev/null +++ b/persistence-modules/spring-boot-persistence/src/main/resources/application-datasource.properties @@ -0,0 +1,11 @@ +spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 +spring.datasource.username=sa +spring.datasource.password= +spring.jpa.hibernate.ddl-auto=none +# Enabling H2 Console +spring.h2.console.enabled=true +# Uppercase Table Names +spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl +hibernate.globally_quoted_identifiers=true +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect