Skip to content

Commit 355931f

Browse files
author
eugenp
committed
adding some simple persistence
1 parent f64e24e commit 355931f

7 files changed

Lines changed: 291 additions & 97 deletions

File tree

‎spring-5/pom.xml‎

Lines changed: 104 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,116 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.baeldung</groupId>
7-
<artifactId>spring-5</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
9-
<packaging>jar</packaging>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>spring-5</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
1010

11-
<name>spring-5</name>
12-
<description></description>
11+
<name>spring-5</name>
12+
<description></description>
1313

14-
<parent>
15-
<groupId>org.springframework.boot</groupId>
16-
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>2.0.0.BUILD-SNAPSHOT</version>
18-
<relativePath/> <!-- lookup parent from repository -->
19-
</parent>
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.BUILD-SNAPSHOT</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
2020

21-
<properties>
22-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24-
<java.version>1.8</java.version>
25-
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-jpa</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-security</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-validation</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
2638

27-
<dependencies>
28-
<dependency>
29-
<groupId>org.springframework.boot</groupId>
30-
<artifactId>spring-boot-starter-data-jpa</artifactId>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.springframework.boot</groupId>
34-
<artifactId>spring-boot-starter-security</artifactId>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.springframework.boot</groupId>
38-
<artifactId>spring-boot-starter-validation</artifactId>
39-
</dependency>
40-
<dependency>
41-
<groupId>org.springframework.boot</groupId>
42-
<artifactId>spring-boot-starter-web</artifactId>
43-
</dependency>
39+
<!-- utils -->
40+
<dependency>
41+
<groupId>org.apache.commons</groupId>
42+
<artifactId>commons-lang3</artifactId>
43+
</dependency>
4444

45-
<dependency>
46-
<groupId>org.springframework.boot</groupId>
47-
<artifactId>spring-boot-devtools</artifactId>
48-
<scope>runtime</scope>
49-
</dependency>
50-
<dependency>
51-
<groupId>com.h2database</groupId>
52-
<artifactId>h2</artifactId>
53-
<scope>runtime</scope>
54-
</dependency>
55-
<dependency>
56-
<groupId>org.springframework.boot</groupId>
57-
<artifactId>spring-boot-starter-test</artifactId>
58-
<scope>test</scope>
59-
</dependency>
60-
</dependencies>
45+
<!-- runtime and test scoped -->
6146

62-
<build>
63-
<plugins>
64-
<plugin>
65-
<groupId>org.springframework.boot</groupId>
66-
<artifactId>spring-boot-maven-plugin</artifactId>
67-
</plugin>
68-
</plugins>
69-
</build>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-devtools</artifactId>
50+
<scope>runtime</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.h2database</groupId>
54+
<artifactId>h2</artifactId>
55+
<scope>runtime</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-test</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
7063

71-
<repositories>
72-
<repository>
73-
<id>spring-snapshots</id>
74-
<name>Spring Snapshots</name>
75-
<url>https://repo.spring.io/snapshot</url>
76-
<snapshots>
77-
<enabled>true</enabled>
78-
</snapshots>
79-
</repository>
80-
<repository>
81-
<id>spring-milestones</id>
82-
<name>Spring Milestones</name>
83-
<url>https://repo.spring.io/milestone</url>
84-
<snapshots>
85-
<enabled>false</enabled>
86-
</snapshots>
87-
</repository>
88-
</repositories>
89-
<pluginRepositories>
90-
<pluginRepository>
91-
<id>spring-snapshots</id>
92-
<name>Spring Snapshots</name>
93-
<url>https://repo.spring.io/snapshot</url>
94-
<snapshots>
95-
<enabled>true</enabled>
96-
</snapshots>
97-
</pluginRepository>
98-
<pluginRepository>
99-
<id>spring-milestones</id>
100-
<name>Spring Milestones</name>
101-
<url>https://repo.spring.io/milestone</url>
102-
<snapshots>
103-
<enabled>false</enabled>
104-
</snapshots>
105-
</pluginRepository>
106-
</pluginRepositories>
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
73+
<repositories>
74+
<repository>
75+
<id>spring-snapshots</id>
76+
<name>Spring Snapshots</name>
77+
<url>https://repo.spring.io/snapshot</url>
78+
<snapshots>
79+
<enabled>true</enabled>
80+
</snapshots>
81+
</repository>
82+
<repository>
83+
<id>spring-milestones</id>
84+
<name>Spring Milestones</name>
85+
<url>https://repo.spring.io/milestone</url>
86+
<snapshots>
87+
<enabled>false</enabled>
88+
</snapshots>
89+
</repository>
90+
</repositories>
91+
<pluginRepositories>
92+
<pluginRepository>
93+
<id>spring-snapshots</id>
94+
<name>Spring Snapshots</name>
95+
<url>https://repo.spring.io/snapshot</url>
96+
<snapshots>
97+
<enabled>true</enabled>
98+
</snapshots>
99+
</pluginRepository>
100+
<pluginRepository>
101+
<id>spring-milestones</id>
102+
<name>Spring Milestones</name>
103+
<url>https://repo.spring.io/milestone</url>
104+
<snapshots>
105+
<enabled>false</enabled>
106+
</snapshots>
107+
</pluginRepository>
108+
</pluginRepositories>
109+
110+
<properties>
111+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
112+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
113+
<java.version>1.8</java.version>
114+
</properties>
107115

108116
</project>

‎spring-5/src/main/java/com/baeldung/Spring5Application.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public class Spring5Application {
99
public static void main(String[] args) {
1010
SpringApplication.run(Spring5Application.class, args);
1111
}
12+
1213
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.persistence;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
5+
import java.util.stream.IntStream;
6+
7+
import org.springframework.beans.factory.InitializingBean;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Component;
10+
11+
import com.baeldung.web.Foo;
12+
13+
@Component
14+
public class DataSetupBean implements InitializingBean {
15+
16+
@Autowired
17+
private FooRepository repo;
18+
19+
//
20+
21+
@Override
22+
public void afterPropertiesSet() throws Exception {
23+
IntStream.range(1, 20).forEach(i -> repo.save(new Foo(randomAlphabetic(8))));
24+
}
25+
26+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.persistence;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
5+
6+
import com.baeldung.web.Foo;
7+
8+
public interface FooRepository extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
9+
10+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.web;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Foo {
10+
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.AUTO)
13+
private long id;
14+
15+
private String name;
16+
17+
public Foo() {
18+
super();
19+
}
20+
21+
public Foo(final String name) {
22+
super();
23+
24+
this.name = name;
25+
}
26+
27+
public Foo(final long id, final String name) {
28+
super();
29+
30+
this.id = id;
31+
this.name = name;
32+
}
33+
34+
// API
35+
36+
public long getId() {
37+
return id;
38+
}
39+
40+
public void setId(final long id) {
41+
this.id = id;
42+
}
43+
44+
public String getName() {
45+
return name;
46+
}
47+
48+
public void setName(final String name) {
49+
this.name = name;
50+
}
51+
52+
//
53+
54+
@Override
55+
public int hashCode() {
56+
final int prime = 31;
57+
int result = 1;
58+
result = prime * result + ((name == null) ? 0 : name.hashCode());
59+
return result;
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
if (this == obj)
65+
return true;
66+
if (obj == null)
67+
return false;
68+
if (getClass() != obj.getClass())
69+
return false;
70+
Foo other = (Foo) obj;
71+
if (name == null) {
72+
if (other.name != null)
73+
return false;
74+
} else if (!name.equals(other.name))
75+
return false;
76+
return true;
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "Foo [name=" + name + "]";
82+
}
83+
84+
}

0 commit comments

Comments
 (0)