From 4857675e2d7be441bdca08a27dda9c22904d5dcb Mon Sep 17 00:00:00 2001 From: Allan Vital Date: Thu, 9 Nov 2017 00:29:10 -0200 Subject: [PATCH] BAEL-1319 Quick Guide on Data.sql and Schema.sql Files in Spring --- .../java/org/baeldung/sqlfiles/Country.java | 33 +++++++++++++++++++ spring-jpa/src/main/resources/data.sql | 5 +++ spring-jpa/src/main/resources/schema.sql | 5 +++ .../src/main/resources/sqlfiles.properties | 1 + 4 files changed, 44 insertions(+) create mode 100644 spring-jpa/src/main/java/org/baeldung/sqlfiles/Country.java create mode 100644 spring-jpa/src/main/resources/data.sql create mode 100644 spring-jpa/src/main/resources/schema.sql create mode 100644 spring-jpa/src/main/resources/sqlfiles.properties diff --git a/spring-jpa/src/main/java/org/baeldung/sqlfiles/Country.java b/spring-jpa/src/main/java/org/baeldung/sqlfiles/Country.java new file mode 100644 index 0000000000..0555c8186c --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/sqlfiles/Country.java @@ -0,0 +1,33 @@ +package org.baeldung.sqlfiles; + +import static javax.persistence.GenerationType.IDENTITY; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Country { + + @Id + @GeneratedValue(strategy = IDENTITY) + private Integer id; + + @Column(nullable = false) + private String name; + + 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; + } + +} diff --git a/spring-jpa/src/main/resources/data.sql b/spring-jpa/src/main/resources/data.sql new file mode 100644 index 0000000000..f36e034ce1 --- /dev/null +++ b/spring-jpa/src/main/resources/data.sql @@ -0,0 +1,5 @@ +INSERT INTO country (name) VALUES ('India'); +INSERT INTO country (name) VALUES ('Brazil'); +INSERT INTO country (name) VALUES ('USA'); +INSERT INTO country (name) VALUES ('Italy'); +COMMIT; diff --git a/spring-jpa/src/main/resources/schema.sql b/spring-jpa/src/main/resources/schema.sql new file mode 100644 index 0000000000..15d7788cd7 --- /dev/null +++ b/spring-jpa/src/main/resources/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE country ( + id INTEGER NOT NULL AUTO_INCREMENT, + name VARCHAR(128) NOT NULL, + PRIMARY KEY (id) +); diff --git a/spring-jpa/src/main/resources/sqlfiles.properties b/spring-jpa/src/main/resources/sqlfiles.properties new file mode 100644 index 0000000000..0bea6adad1 --- /dev/null +++ b/spring-jpa/src/main/resources/sqlfiles.properties @@ -0,0 +1 @@ +spring.jpa.hibernate.ddl-auto=none \ No newline at end of file