Spring JPA tips and tricks, Pt I: Setup

Lately I've been dealing with project which uses Spring JPA + Hibernate. Basics are relatively simple and well documented, but I've bumped into some requirements, which are not that straight-forward. In addition to the fact, that I just don't know the keywords to search for, turns out, that, as usual, I'm into quite unpopular ideas. I tend to think about them as of novel. So, I'll dedicate a couple of entries every now and then (as I figure things out) to document my findings. Maybe somebody will find them useful.
We'll take official Spring guide as a base for our further digging into JPA. It's a nice plain thing to start with, plus I can skip telling you the basics, which are covered in their tutorial.

I like to set up manually data for my playgrounds, to avoid uncertainty and abstraction during playing around. For that we have to manually add data source configuration, disable all sorts of DDL generation and insert some data manually. Let's get to it:

1. Add schema.sql to /resources with simple table cretation script:
CREATE TABLE customer (
    id           INTEGER AUTO_INCREMENT PRIMARY KEY,
    first_name   VARCHAR(64) NOT NULL,
    last_name    VARCHAR(64) NOT NULL
);

2. Add input data insertion script in /resources/data.sql:
INSERT INTO customer (id, first_name, last_name) VALUES
    (1, 'Jon', 'Doe'),
    (2, 'Jack', 'Dawson'),
    (3, 'Jack', 'Doe'),
    (4, 'Joanna', 'Doe'),
    (5, 'Mark', 'Pawson');

3. Configure in-memory database to be used by spring in /resources/application.properties:

spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=- 1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

By running our modified example, we should get additional entries in output, as defined in data.sql, with added entries from main application routine:

2017-11-30 15:17:10.596  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customers found with findAll():
2017-11-30 15:17:10.596  INFO 2060 --- [           main] com.shirtec.playground.Application       : -------------------------------
2017-11-30 15:17:10.604  INFO 2060 --- [           main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=1, firstName='Jon', lastName='Doe']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=2, firstName='Jack', lastName='Dawson']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=3, firstName='Jack', lastName='Doe']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=4, firstName='Joanna', lastName='Doe']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=5, firstName='Mark', lastName='Pawson']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=6, firstName='Jack', lastName='Bauer']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=7, firstName='Chloe', lastName='O'Brian']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=8, firstName='Kim', lastName='Bauer']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=9, firstName='David', lastName='Palmer']
2017-11-30 15:17:10.687  INFO 2060 --- [           main] com.shirtec.playground.Application       : Customer[id=10, firstName='Michelle', lastName='Dessler']

Don't worry about duplicate first or last names, they'll come in handy in the future exercises.

All of the source code is going to be pushed to GitHub. Repository master branch from here will get branched off for specific cases, each of which might or break code (generate compilation or runtime errors).

This initial post will be available under works/vanilla branch

You can read on in the next post where I attempt to deal with enums:
onwards, to Part II: Enums!

No comments:

Post a Comment