Before we go ahead we want to do some changes to the current state of the service in order to get a good and cleaner starting point.
When you build a service step by step, it is easier for testing to have a in-memory database and use some demo data created with the migration script. Otherwise we would need to create additional migration scripts for each change we make, which does not make sense and would be a lot a work . Therefore we change application.yml, so that H2 in-memory is used:
logging:
level:
org:
springframework: INFO <!--(1)-->
com:
sap:
olingo:
jpa: TRACE
odata:
jpa:
punit_name: Trippin
root_packages: com.example.trippin
server:
port: 9010
spring:
datasource:
driver-class-name: org.h2.Driver
password: ''
url: jdbc:h2:mem:testdb;MODE=PostgreSQL
username: sa
flyway:
password: ''
schemas:
- Trippin
url: jdbc:h2:mem:testdb;MODE=PostgreSQL
user: sa
servlet:
multipart:
enabled: falseNext we rename class EntityTemplate to Person and add a list of attributes, which let this entity look more like a person:
import java.util.Collection;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity(name = "Person")
@Table(schema = "\"Trippin\"", name = "\"Person\"")
public class Person {
@Id
@Column(name = "\"UserName\"")
private String userName;
@Column(name = "\"FirstName\"", nullable = false)
private String firstName;
@Column(name = "\"LastName\"", length = 26)
private String lastName;
@Column(name = "\"MiddleName\"")
private String middleName;
@Column(name = "\"Age\"")
private Short age;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JoinColumn(name = "\"UserName\"", insertable = false, updatable = false)
private Collection<Trip> trips;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Collection<Trip> getTrips() {
return trips;
}
public void setTrips(Collection<Trip> trips) {
this.trips = trips;
}
@Override
public int hashCode() {
return Objects.hash(userName);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
return Objects.equals(userName, other.userName);
}
}Next we delete ValueObjectTemplateKey.
The service becomes consistent after we renamed ValueObjectTemplate to Trip, clean it up and also add some new attributes:
import java.time.ZonedDateTime;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import com.sap.olingo.jpa.metadata.core.edm.annotation.EdmIgnore;
@Entity(name = "Trip")
@Table(schema = "\"Trippin\"", name = "\"Trip\"")
public class Trip {
@Id
@Column(name = "\"TripId\"")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TripId")
@SequenceGenerator(name = "TripId", sequenceName = "\"Trippin\".\"TripId\"", allocationSize = 1)
private Integer tripId;
@EdmIgnore
@Column(name = "\"UserName\"")
private String userName;
@Column(name = "\"ShareId\"")
private UUID shareId;
@Column(name = "\"Name\"")
private String name;
@Column(name = "\"Budget\"")
private Float budget;
@Column(name = "\"Description\"")
private String description;
@Column(name = "\"StartsAt\"")
private ZonedDateTime startsAt;
@Column(name = "\"EndsAt\"")
private ZonedDateTime endsAt;
public Integer getTripId() {
return tripId;
}
public void setTripId(Integer tripId) {
this.tripId = tripId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public UUID getShareId() {
return shareId;
}
public void setShareId(UUID shareId) {
this.shareId = shareId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getBudget() {
return budget;
}
public void setBudget(Float budget) {
this.budget = budget;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ZonedDateTime getStartsAt() {
return startsAt;
}
public void setStartsAt(ZonedDateTime startsAt) {
this.startsAt = startsAt;
}
public ZonedDateTime getEndsAt() {
return endsAt;
}
public void setEndsAt(ZonedDateTime endsAt) {
this.endsAt = endsAt;
}
}To get the service running again, we have to update the migration script V1_0__jpa.sql as well:
SET schema "Trippin";
CREATE SEQUENCE "TripId";
CREATE TABLE "Person"(
"UserName" VARCHAR(250) NOT NULL,
"FirstName" VARCHAR(250) NOT NULL,
"LastName" VARCHAR(26),
"MiddleName" VARCHAR(250),
"Gender" SMALLINT,
"Age" INTEGER,
PRIMARY KEY ("UserName"));
insert into "Person" values ('russellwhyte', 'Russell', 'Whyte', null, 0, null);
insert into "Person" values ('scottketchum', 'Scott', 'Ketchum', null, 0, null);
insert into "Person" values ('ronaldmundy', 'Ronald', 'Mundy', null, 0, null);
insert into "Person" values ('javieralfred', 'Javier', 'Alfred', null, 0, null);
insert into "Person" values ('willieashmore', 'Willie', 'Ashmore', null, 0, null);
insert into "Person" values ('vincentcalabrese', 'Vincent', 'Calabrese', null, 0, null);
insert into "Person" values ('clydeguess', 'Clyde', 'Guess', null, 0, null);
insert into "Person" values ('keithpinckney', 'Keith', 'Pinckney', null, 0, null);
insert into "Person" values ('marshallgaray', 'Marshall', 'Garay', null, 0, null);
insert into "Person" values ('ryantheriault', 'Ryan', 'Theriault', null, 0, null);
insert into "Person" values ('elainestewart', 'Elaine', 'Stewart', null, 0, null);
insert into "Person" values ('sallysampson', 'Sally', 'Sampson', null, 1, null);
insert into "Person" values ('jonirosales', 'Joni', 'Rosales', null, 2, null);
insert into "Person" values ('georginabarlow', 'Georgina', 'Barlow', null, 1, null);
insert into "Person" values ('angelhuffman', 'Angel', 'Huffman', null, 1, null);
insert into "Person" values ('laurelosborn', 'Laurel', 'Osborn', null, 1, null);
insert into "Person" values ('sandyosborn', 'Sandy', 'Osborn', null, 1, null);
insert into "Person" values ('ursulabright', 'Ursula', 'Bright', null, 1, null);
insert into "Person" values ('genevievereeves', 'Genevieve', 'Reeves', null, 1, null);
insert into "Person" values ('kristakemp', 'Krista', 'Kemp', null, 1, null);
CREATE TABLE "Trip"(
"TripId" INTEGER NOT NULL,
"UserName" VARCHAR(250) NOT NULL,
"ShareId" UUID NOT NULL,
"Name" VARCHAR(250),
"Budget" FLOAT,
"Description" VARCHAR(2500),
"StartsAt" TIMESTAMP WITH TIME ZONE,
"EndsAt" TIMESTAMP WITH TIME ZONE,
PRIMARY KEY ("TripId"));
insert into "Trip" values (0, 'russellwhyte', '9d9b2fa0-efbf-490e-a5e3-bac8f7d47354', 'Trip in US', 3000, 'Trip from San Francisco to New York City', '2014-01-01T00:00:00Z', '2014-01-04T00:00:00Z');
insert into "Trip" values (1, 'russellwhyte', 'f94e9116-8bdd-4dac-ab61-08438d0d9a71', 'Trip in Beijing', 2000, 'Trip from Shanghai to Beijing', '2014-02-01T00:00:00Z', '2014-02-04T00:00:00Z');
insert into "Trip" values (2, 'russellwhyte', '9ce142c3-5fd6-4a71-848e-5220ebf1e9f3', 'Honeymoon', 2650, 'Happy honeymoon trip', '2014-02-01T00:00:00Z', '2014-02-04T00:00:00Z');
insert into "Trip" values (3, 'scottketchum', '9d9b2fa0-efbf-490e-a5e3-bac8f7d47354', 'Trip in US', 5000, 'Trip from San Francisco to New York City', '2014-01-01T00:00:00Z', '2014-01-04T00:00:00Z');
insert into "Trip" values (4, 'scottketchum', 'f94e9116-8bdd-4dac-ab61-08438d0d9a71', 'Trip in Beijing', 11000, 'Trip from Shanghai to Beijing', '2014-02-01T00:00:00Z', '2014-02-04T00:00:00Z');
insert into "Trip" values (5, 'ronaldmundy', 'dd6a09c0-e59b-4745-8612-f4499b676c47', 'Gradutaion trip', 6000, 'Gradutaion trip with friends', '2013-05-01T00:00:00Z', '2013-05-08T00:00:00Z');
insert into "Trip" values (6, 'javieralfred', 'f94e9116-8bdd-4dac-ab61-08438d0d9a71', 'Trip in Beijing', 800, 'Trip from Shanghai to Beijing', '2014-02-01T00:00:00Z', '2014-02-04T00:00:00Z');
insert into "Trip" values (7, 'willieashmore', '5ae142c3-5ad6-4a71-768e-5220ebf1e9f3', 'Business Trip', 3800.5, 'This is my first business trip', '2014-02-01T00:00:00Z', '2014-02-04T00:00:00Z');
insert into "Trip" values (8, 'willieashmore', '9ce32ac3-5fd6-4a72-848e-2250ebf1e9f3', 'Trip in Europe', 2000, 'The trip is currently in plan.', '2014-02-01T00:00:00Z', '2014-02-04T00:00:00Z');
insert into "Trip" values (9, 'vincentcalabrese', 'dd6a09c0-e59b-4745-8612-f4499b676c47', 'Gradutaion trip', 6000, 'Gradutaion trip with friends', '2013-05-01T00:00:00Z', '2013-05-08T00:00:00Z');
insert into "Trip" values (11, 'keithpinckney', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (12, 'marshallgaray', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (13, 'ryantheriault', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (14, 'elainestewart', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (15, 'sallysampson', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 600, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (16, 'jonirosales', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 2000, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (17, 'georginabarlow', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1150.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (18, 'angelhuffman', 'cb0b8acb-79cb-4127-8316-772bc4302824', 'DIY Trip', 1500.3, 'This is a DIY trip', '2011-02-11T00:00:00Z', '2011-02-14T00:00:00Z');
insert into "Trip" values (19, 'laurelosborn', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (20, 'sandyosborn', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (21, 'ursulabright', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (22, 'genevievereeves', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');
insert into "Trip" values (23, 'kristakemp', 'a88f675d-9199-4392-9656-b08e3b46df8a', 'Study trip', 1550.3, 'This is a 2 weeks study trip', '2014-01-01T00:00:00Z', '2014-01-14T00:00:00Z');We can start the service and perform some requests like: