Skip to content

Commit 27bd3aa

Browse files
committed
DATAJPA-17 - Initial port of Hades sample project and showcase project.
Added two sample projects to show usage of Spring Data JPA. The example project contains various test cases that demonstrate using the library in various flavours: - BasicSample - Plain API usage (no Spring container involved, CRUD methods only) - BasicFactory - Programatic usage of the JpaRepositoryFactory (no Spring container involved, CRUD + query methods) - SimpleUserRepositorySample - Namespace auto-discovery usage - UserRepositorySample - Namespace usage, using custom implementation - AuditingUserSample - shows the auditing features The second example is a showcase scenario demonstrating the amount of code you save when using Spring Data JPA over a plain JPA/Spring repository implementation. It contains a guide to transform the the code in "before" package to introduce Spring Data JPA step by step and finally end up with the "after" package.
0 parents  commit 27bd3aa

File tree

56 files changed

+2140
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2140
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.project
2+
.classpath
3+
.springBeans
4+
.settings/
5+
target/

pom.xml

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.springframework.data.jpa.examples</groupId>
6+
<artifactId>spring-data-jpa-examples-parent</artifactId>
7+
<version>1.0.0.BUILD-SNAPSHOT</version>
8+
<packaging>pom</packaging>
9+
10+
<name>Spring Data JPA sample projects</name>
11+
<description>Sample projects for Spring Data JPA</description>
12+
<url>http://www.springframework.org/spring-data</url>
13+
<inceptionYear>2010</inceptionYear>
14+
15+
<modules>
16+
<module>spring-data-jpa-example</module>
17+
<module>spring-data-jpa-showcase</module>
18+
</modules>
19+
20+
<developers>
21+
<developer>
22+
<id>gierke</id>
23+
<name>Oliver Gierke</name>
24+
<email>[email protected]</email>
25+
<url>http://www.olivergierke.de</url>
26+
</developer>
27+
</developers>
28+
29+
<properties>
30+
<spring.version>3.0.5.RELEASE</spring.version>
31+
<jpa.version>2.0.0</jpa.version>
32+
<junit.version>4.8</junit.version>
33+
<hibernate.version>3.5.6-Final</hibernate.version>
34+
<slf4j.version>1.5.8</slf4j.version>
35+
<aspectj.version>1.6.8</aspectj.version>
36+
<jodatime.version>1.5.2</jodatime.version>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<version>2.3</version>
46+
<configuration>
47+
<source>1.6</source>
48+
<target>1.6</target>
49+
</configuration>
50+
</plugin>
51+
<plugin>
52+
<groupId>com.springsource.bundlor</groupId>
53+
<artifactId>com.springsource.bundlor.maven</artifactId>
54+
<version>1.0.0.RELEASE</version>
55+
<executions>
56+
<execution>
57+
<id>bundlor</id>
58+
<goals>
59+
<goal>bundlor</goal>
60+
</goals>
61+
<phase>process-classes</phase>
62+
</execution>
63+
</executions>
64+
<configuration>
65+
<failOnWarnings>true</failOnWarnings>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
<dependencies>
72+
73+
<dependency>
74+
<groupId>org.springframework.data</groupId>
75+
<artifactId>spring-data-jpa</artifactId>
76+
<version>${project.version}</version>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>javax.persistence</groupId>
81+
<artifactId>com.springsource.javax.persistence</artifactId>
82+
<version>${jpa.version}</version>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>junit</groupId>
87+
<artifactId>junit</artifactId>
88+
<version>${junit.version}</version>
89+
<scope>test</scope>
90+
</dependency>
91+
92+
<dependency>
93+
<groupId>log4j</groupId>
94+
<artifactId>log4j</artifactId>
95+
<version>1.2.16</version>
96+
<scope>test</scope>
97+
</dependency>
98+
<dependency>
99+
<groupId>org.springframework</groupId>
100+
<artifactId>spring-test</artifactId>
101+
<version>${spring.version}</version>
102+
<scope>test</scope>
103+
</dependency>
104+
105+
<dependency>
106+
<groupId>org.hibernate</groupId>
107+
<artifactId>hibernate-entitymanager</artifactId>
108+
<version>${hibernate.version}</version>
109+
<scope>runtime</scope>
110+
</dependency>
111+
<dependency>
112+
<groupId>org.slf4j</groupId>
113+
<artifactId>slf4j-log4j12</artifactId>
114+
<version>${slf4j.version}</version>
115+
<scope>runtime</scope>
116+
</dependency>
117+
118+
<!-- HSQL -->
119+
<dependency>
120+
<groupId>org.hsqldb</groupId>
121+
<artifactId>com.springsource.org.hsqldb</artifactId>
122+
<version>1.8.0.9</version>
123+
<scope>runtime</scope>
124+
</dependency>
125+
126+
<dependency>
127+
<groupId>joda-time</groupId>
128+
<artifactId>joda-time</artifactId>
129+
<version>${jodatime.version}</version>
130+
</dependency>
131+
132+
<dependency>
133+
<groupId>org.aspectj</groupId>
134+
<artifactId>aspectjweaver</artifactId>
135+
<version>${aspectj.version}</version>
136+
<scope>runtime</scope>
137+
</dependency>
138+
139+
</dependencies>
140+
141+
<repositories>
142+
<repository>
143+
<id>jboss</id>
144+
<name>JBoss repository</name>
145+
<url>https://repository.jboss.org/nexus/content/repositories/releases</url>
146+
</repository>
147+
</repositories>
148+
149+
</project>

spring-data-jpa-example/pom.xml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>spring-data-jpa-example</artifactId>
6+
7+
<parent>
8+
<groupId>org.springframework.data.jpa.examples</groupId>
9+
<artifactId>spring-data-jpa-examples-parent</artifactId>
10+
<version>1.0.0.BUILD-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<name>Spring Data JPA sample</name>
15+
<description>Small sample project showing the usage of Sprign Data JPA.</description>
16+
17+
<dependencies>
18+
19+
<dependency>
20+
<groupId>org.springframework</groupId>
21+
<artifactId>org.springframework.aspects</artifactId>
22+
<version>${spring.version}</version>
23+
</dependency>
24+
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-surefire-plugin</artifactId>
32+
<version>2.4.3</version>
33+
<configuration>
34+
<includes>
35+
<include>**/*.java</include>
36+
</includes>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
42+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.springframework.data.jpa.example.auditing;
2+
3+
import javax.persistence.Entity;
4+
5+
import org.springframework.data.domain.Auditable;
6+
import org.springframework.data.jpa.domain.AbstractAuditable;
7+
8+
9+
/**
10+
* User domain class that uses auditing functionality of Spring Data that can either
11+
* be aquired implementing {@link Auditable} or extend
12+
* {@link AbstractAuditable}.
13+
*
14+
* @author Oliver Gierke
15+
*/
16+
@Entity
17+
public class AuditableUser extends AbstractAuditable<AuditableUser, Long> {
18+
19+
private static final long serialVersionUID = 1L;
20+
21+
private String username;
22+
23+
24+
/**
25+
* Set's the user's name.
26+
*
27+
* @param username the username to set
28+
*/
29+
public void setUsername(String username) {
30+
31+
this.username = username;
32+
}
33+
34+
35+
/**
36+
* Returns the user's name.
37+
*
38+
* @return the username
39+
*/
40+
public String getUsername() {
41+
42+
return username;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.springframework.data.jpa.example.auditing;
2+
3+
import org.springframework.data.domain.AuditorAware;
4+
5+
6+
/**
7+
* Dummy implementation of {@link AuditorAware}. It will return the configured
8+
* {@link AuditableUser} as auditor on every call to
9+
* {@link #getCurrentAuditor()}. Normally you would access the applications
10+
* security subsystem to return the current user.
11+
*
12+
* @author Oliver Gierke
13+
*/
14+
public class AuditorAwareImpl implements AuditorAware<AuditableUser> {
15+
16+
private AuditableUser auditor;
17+
18+
19+
/**
20+
* @param auditor the auditor to set
21+
*/
22+
public void setAuditor(AuditableUser auditor) {
23+
24+
this.auditor = auditor;
25+
}
26+
27+
28+
/*
29+
* (non-Javadoc)
30+
*
31+
* @see org.springframework.data.domain.AuditorAware#getCurrentAuditor()
32+
*/
33+
public AuditableUser getCurrentAuditor() {
34+
35+
return auditor;
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.springframework.data.jpa.example.domain;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.NamedQuery;
6+
7+
import org.springframework.data.jpa.domain.AbstractPersistable;
8+
9+
10+
/**
11+
* Sample user class.
12+
*
13+
* @author Oliver Gierke
14+
*/
15+
@Entity
16+
@NamedQuery(name = "User.findByTheUsersName", query = "from User u where u.username = ?")
17+
public class User extends AbstractPersistable<Long> {
18+
19+
private static final long serialVersionUID = -2952735933715107252L;
20+
21+
@Column(unique = true)
22+
private String username;
23+
24+
private String firstname;
25+
private String lastname;
26+
27+
28+
public User() {
29+
30+
this(null);
31+
}
32+
33+
34+
/**
35+
* Creates a new user instance.
36+
*/
37+
public User(Long id) {
38+
39+
this.setId(id);
40+
}
41+
42+
43+
/**
44+
* Returns the username.
45+
*
46+
* @return
47+
*/
48+
public String getUsername() {
49+
50+
return username;
51+
}
52+
53+
54+
/**
55+
* @param username the username to set
56+
*/
57+
public void setUsername(String username) {
58+
59+
this.username = username;
60+
}
61+
62+
63+
/**
64+
* @return the firstname
65+
*/
66+
public String getFirstname() {
67+
68+
return firstname;
69+
}
70+
71+
72+
/**
73+
* @param firstname the firstname to set
74+
*/
75+
public void setFirstname(String firstname) {
76+
77+
this.firstname = firstname;
78+
}
79+
80+
81+
/**
82+
* @return the lastname
83+
*/
84+
public String getLastname() {
85+
86+
return lastname;
87+
}
88+
89+
90+
/**
91+
* @param lastname the lastname to set
92+
*/
93+
public void setLastname(String lastname) {
94+
95+
this.lastname = lastname;
96+
}
97+
}

0 commit comments

Comments
 (0)