From a3ad9416244f132e9fad22b06911ec277615d3d5 Mon Sep 17 00:00:00 2001 From: Fibinger Ádám <adam.fibinger@wup.hu> Date: Tue, 26 Feb 2019 19:27:38 +0100 Subject: [PATCH] H2 basics with JPA --- src/main/resources/application.properties | 10 ++++++++++ src/main/java/hu/unr/fiber/cardapi/CardRepository.java | 6 ++++++ src/main/resources/data.sql | 4 ++++ pom.xml | 20 +++++++++++++++----- src/main/java/hu/unr/fiber/cardapi/Card.java | 11 +++++++++++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 11d2d6c..188990e 100644 --- a/pom.xml +++ b/pom.xml @@ -16,16 +16,26 @@ <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-devtools</artifactId> + <scope>runtime</scope> + </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>com.jayway.jsonpath</groupId> - <artifactId>json-path</artifactId> <scope>test</scope> </dependency> </dependencies> diff --git a/src/main/java/hu/unr/fiber/cardapi/Card.java b/src/main/java/hu/unr/fiber/cardapi/Card.java index 56d54bd..8440e38 100644 --- a/src/main/java/hu/unr/fiber/cardapi/Card.java +++ b/src/main/java/hu/unr/fiber/cardapi/Card.java @@ -1,15 +1,26 @@ package hu.unr.fiber.cardapi; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; import java.util.Objects; +@Entity public class Card { public static final long INVALID_ID = 0; + + @Id + @GeneratedValue private long id = INVALID_ID; private String name; //FIXME: @Pattern([0-9]+) private String number; private String cardHolder; + public Card(){ + super(); + } + public Card(long id) { this.id = id; } diff --git a/src/main/java/hu/unr/fiber/cardapi/CardRepository.java b/src/main/java/hu/unr/fiber/cardapi/CardRepository.java new file mode 100644 index 0000000..a5404d5 --- /dev/null +++ b/src/main/java/hu/unr/fiber/cardapi/CardRepository.java @@ -0,0 +1,6 @@ +package hu.unr.fiber.cardapi; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CardRepository extends JpaRepository<Card, Long> { +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..8e5e2af --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,10 @@ +# Enabling H2 Console +spring.h2.console.enabled=true + +#Turn Statistics on +spring.jpa.properties.hibernate.generate_statistics=true +logging.level.org.hibernate.stat=debug +# Show all queries +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true +logging.level.org.hibernate.type=trace \ No newline at end of file diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100644 index 0000000..0d72552 --- /dev/null +++ b/src/main/resources/data.sql @@ -0,0 +1,4 @@ +INSERT INTO CARD(ID, NAME, NUMBER) values(1,'Első kártya', '1') +INSERT INTO CARD(ID, NAME, NUMBER) values(2,'Második kártya', '2') +INSERT INTO CARD(ID, NAME, NUMBER) values(3,'Harmadik kártya', '4') +INSERT INTO CARD(ID, NAME, NUMBER) values(10,'Tízezer egyszázadik kártya', '10100') \ No newline at end of file -- Gitblit v1.8.0