Skip to content

Commit e8b0399

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
#8 - Expand examples to show JavaConfig more prominently.
Added additional examples for java config. Restructured the packages a bit according to the particular use case. Updated spring-data-jpa dependency to 1.4.0.RC1. Original pull request: #9.
1 parent 0347d19 commit e8b0399

40 files changed

+640
-109
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<aspectj.version>1.6.12</aspectj.version>
3737
<jodatime.version>2.2</jodatime.version>
3838
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
39-
<spring.data.jpa.version>1.4.0.M1</spring.data.jpa.version>
39+
<spring.data.jpa.version>1.4.0.RC1</spring.data.jpa.version>
4040
</properties>
4141

4242
<build>
@@ -153,7 +153,7 @@
153153
<version>2.2.8</version>
154154
<scope>runtime</scope>
155155
</dependency>
156-
156+
157157
<dependency>
158158
<groupId>joda-time</groupId>
159159
<artifactId>joda-time</artifactId>

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/auditing/AuditableUser.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.jpa.example.auditing;
217

318
import javax.persistence.Entity;
@@ -10,6 +25,7 @@
1025
* {@link Auditable} or extend {@link AbstractAuditable}.
1126
*
1227
* @author Oliver Gierke
28+
* @author Thomas Darimont
1329
*/
1430
@Entity
1531
public class AuditableUser extends AbstractAuditable<AuditableUser, Long> {

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/auditing/AuditorAwareImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.jpa.example.auditing;
217

318
import org.springframework.data.domain.AuditorAware;
@@ -8,6 +23,7 @@
823
* current user.
924
*
1025
* @author Oliver Gierke
26+
* @author Thomas Darimont
1127
*/
1228
public class AuditorAwareImpl implements AuditorAware<AuditableUser> {
1329

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Package showing auditing support with Spring Data repositories.
3+
*/
4+
package org.springframework.data.jpa.example.auditing;

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/domain/User.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.jpa.example.domain;
217

318
import javax.persistence.Column;
@@ -10,15 +25,15 @@
1025
* Sample user class.
1126
*
1227
* @author Oliver Gierke
28+
* @author Thomas Darimont
1329
*/
1430
@Entity
1531
@NamedQuery(name = "User.findByTheUsersName", query = "from User u where u.username = ?1")
1632
public class User extends AbstractPersistable<Long> {
1733

1834
private static final long serialVersionUID = -2952735933715107252L;
1935

20-
@Column(unique = true)
21-
private String username;
36+
@Column(unique = true) private String username;
2237

2338
private String firstname;
2439
private String lastname;

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/UserRepositoryCustom.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/caching/CachingConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Java config to use Spring Data JPA alongside the Spring caching support.
3232
*
3333
* @author Oliver Gierke
34+
* @author Thomas Darimont
3435
*/
3536
@Configuration
3637
@EnableJpaRepositories

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/caching/CachingUserRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* User repository using Spring's caching abstraction.
2525
*
2626
* @author Oliver Gierke
27+
* @author Thomas Darimont
2728
*/
2829
public interface CachingUserRepository extends CrudRepository<User, Long> {
2930

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
22
* Sample for the integration of the Spring Caching abstraction with Spring Data repositories.
33
*/
4-
package org.springframework.data.jpa.example.repository.caching;
4+
package org.springframework.data.jpa.example.repository.caching;
5+

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/UserRepository.java renamed to spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/custom/UserRepository.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
package org.springframework.data.jpa.example.repository;
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.example.repository.custom;
217

318
import java.util.List;
419

@@ -12,6 +27,7 @@
1227
* {@link JpaRepository}. Includes custom implemented functionality by extending {@link UserRepositoryCustom}.
1328
*
1429
* @author Oliver Gierke
30+
* @author Thomas Darimont
1531
*/
1632
public interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {
1733

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.example.repository.custom;
17+
18+
import java.util.List;
19+
20+
import org.springframework.data.jpa.example.domain.User;
21+
22+
/**
23+
* Interface for repository functionality that ought to be implemented manually.
24+
*
25+
* @author Oliver Gierke
26+
* @author Thomas Darimont
27+
*/
28+
interface UserRepositoryCustom {
29+
30+
/**
31+
* Custom repository operation.
32+
*
33+
* @return
34+
*/
35+
List<User> myCustomBatchOperation();
36+
}
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
package org.springframework.data.jpa.example.repository;
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.example.repository.custom;
217

318
import java.util.List;
419

@@ -29,14 +44,14 @@
2944
* &lt;jpa:repositories base-package=&quot;com.acme.repository&quot; /&gt;
3045
* </pre>
3146
*
32-
* If you need to manually configure the custom instance see {@link UserRepositoryJdbcImpl} for an example.
47+
* If you need to manually configure the custom instance see {@link UserRepositoryImplJdbc} for an example.
3348
*
3449
* @author Oliver Gierke
50+
* @author Thomas Darimont
3551
*/
3652
class UserRepositoryImpl implements UserRepositoryCustom {
3753

38-
@PersistenceContext
39-
private EntityManager em;
54+
@PersistenceContext private EntityManager em;
4055

4156
/**
4257
* Configure the entity manager to be used.
@@ -54,6 +69,7 @@ public void setEntityManager(EntityManager em) {
5469
public List<User> myCustomBatchOperation() {
5570

5671
CriteriaQuery<User> criteriaQuery = em.getCriteriaBuilder().createQuery(User.class);
72+
criteriaQuery.select(criteriaQuery.from(User.class));
5773
return em.createQuery(criteriaQuery).getResultList();
5874
}
5975
}
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
package org.springframework.data.jpa.example.repository;
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.example.repository.custom;
217

318
import java.sql.ResultSet;
419
import java.sql.SQLException;
520
import java.util.List;
621

22+
import javax.sql.DataSource;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Profile;
726
import org.springframework.data.jpa.example.domain.User;
827
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
928
import org.springframework.jdbc.core.support.JdbcDaoSupport;
29+
import org.springframework.stereotype.Component;
1030

1131
/**
1232
* Class with the implementation of the custom repository code. Uses JDBC in this case. For basic programatic setup see
@@ -27,11 +47,19 @@
2747
* logic as the default postfix for custom DAO instances is {@code Impl}.
2848
*
2949
* @author Oliver Gierke
50+
* @author Thomas Darimont
3051
*/
31-
class UserRepositoryJdbcImpl extends JdbcDaoSupport implements UserRepositoryCustom {
52+
@Profile("jdbc")
53+
@Component("userRepositoryImpl")
54+
class UserRepositoryImplJdbc extends JdbcDaoSupport implements UserRepositoryCustom {
3255

3356
private static final String COMPLICATED_SQL = "SELECT * FROM User";
3457

58+
@Autowired
59+
public UserRepositoryImplJdbc(DataSource dataSource) {
60+
setDataSource(dataSource);
61+
}
62+
3563
/*
3664
* (non-Javadoc)
3765
* @see org.springframework.data.jpa.example.repository.UserRepositoryCustom#myCustomBatchOperation()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Package showing a repository interface to use basic finder method execution functionality as well as <em>customized</em> repository functionality.
3+
*/
4+
package org.springframework.data.jpa.example.repository.custom;
5+

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/package.html

Lines changed: 0 additions & 7 deletions
This file was deleted.

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/simple/SimpleUserRepository.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.jpa.example.repository.simple;
217

318
import java.util.List;
@@ -12,6 +27,7 @@
1227
* methods to retrieve single entities or collections of them.
1328
*
1429
* @author Oliver Gierke
30+
* @author Thomas Darimont
1531
*/
1632
public interface SimpleUserRepository extends CrudRepository<User, Long> {
1733

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Package showing a simple repository interface to use basic finder method execution functionality.
3+
*/
4+
package org.springframework.data.jpa.example.repository.simple;
5+

spring-data-jpa-example/src/main/java/org/springframework/data/jpa/example/repository/simple/package.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

spring-data-jpa-example/src/main/resources/META-INF/persistence.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
3-
<persistence-unit name="jpa.sample" />
3+
<persistence-unit name="jpa.sample"/>
44
<persistence-unit name="jpa.sample.plain">
55
<properties>
66
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />

0 commit comments

Comments
 (0)