Skip to content

Commit efd39a5

Browse files
committed
Got rid of XML based samples.
1 parent 51d1c5d commit efd39a5

32 files changed

+255
-335
lines changed

spring-data-jpa-example/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<dependency>
2020
<groupId>org.springframework</groupId>
2121
<artifactId>spring-aspects</artifactId>
22-
<version>${spring.version}</version>
2322
</dependency>
2423

2524
</dependencies>

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
*/
1616
package org.springframework.data.jpa.example.repository.auditing;
1717

18+
import javax.sql.DataSource;
19+
1820
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
1921
import org.springframework.context.annotation.Bean;
2022
import org.springframework.context.annotation.Configuration;
2123
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
24+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
25+
import org.springframework.orm.jpa.vendor.Database;
26+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
2227

2328
/**
2429
* @author Oliver Gierke
@@ -28,6 +33,31 @@
2833
@EnableJpaAuditing
2934
class AuditingConfiguration {
3035

36+
/**
37+
* We need to configure a {@link LocalContainerEntityManagerFactoryBean} manually here as Spring does <em>not</em>
38+
* automatically add the {@code orm.xml} <em>if</em> a {@code persistence.xml} is located right beside it. This is
39+
* necessary to get the {@link org.springframework.data.jpa.example.repository.basics.BasicFactorySetup} sample
40+
* working. However, in a {code persistence.xml}-less codebase you can rely on Spring Boot on setting the correct
41+
* defaults.
42+
*
43+
* @return
44+
*/
45+
@Bean
46+
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
47+
48+
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
49+
adapter.setDatabase(Database.HSQL);
50+
adapter.setGenerateDdl(true);
51+
52+
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
53+
factoryBean.setPackagesToScan(getClass().getPackage().getName());
54+
factoryBean.setMappingResources("META-INF/orm.xml");
55+
factoryBean.setJpaVendorAdapter(adapter);
56+
factoryBean.setDataSource(dataSource);
57+
58+
return factoryBean;
59+
}
60+
3161
@Bean
3262
AuditorAwareImpl auditorAware() {
3363
return new AuditorAwareImpl();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717

1818
import org.springframework.cache.annotation.CacheEvict;
1919
import org.springframework.cache.annotation.Cacheable;
20-
import org.springframework.data.jpa.example.domain.User;
2120
import org.springframework.data.repository.CrudRepository;
2221

2322
/**
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.caching;
17+
18+
import javax.persistence.Column;
19+
import javax.persistence.Entity;
20+
import javax.persistence.NamedQuery;
21+
22+
import org.springframework.data.jpa.domain.AbstractPersistable;
23+
24+
/**
25+
* Sample user class.
26+
*
27+
* @author Oliver Gierke
28+
* @author Thomas Darimont
29+
*/
30+
@Entity
31+
@NamedQuery(name = "User.findByTheUsersName", query = "from User u where u.username = ?1")
32+
public class User extends AbstractPersistable<Long> {
33+
34+
private static final long serialVersionUID = -2952735933715107252L;
35+
36+
@Column(unique = true) private String username;
37+
38+
private String firstname;
39+
private String lastname;
40+
41+
public User() {
42+
this(null);
43+
}
44+
45+
/**
46+
* Creates a new user instance.
47+
*/
48+
public User(Long id) {
49+
this.setId(id);
50+
}
51+
52+
/**
53+
* Returns the username.
54+
*
55+
* @return
56+
*/
57+
public String getUsername() {
58+
59+
return username;
60+
}
61+
62+
/**
63+
* @param username the username to set
64+
*/
65+
public void setUsername(String username) {
66+
this.username = username;
67+
}
68+
69+
/**
70+
* @return the firstname
71+
*/
72+
public String getFirstname() {
73+
return firstname;
74+
}
75+
76+
/**
77+
* @param firstname the firstname to set
78+
*/
79+
public void setFirstname(String firstname) {
80+
this.firstname = firstname;
81+
}
82+
83+
/**
84+
* @return the lastname
85+
*/
86+
public String getLastname() {
87+
return lastname;
88+
}
89+
90+
/**
91+
* @param lastname the lastname to set
92+
*/
93+
public void setLastname(String lastname) {
94+
this.lastname = lastname;
95+
}
96+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.example.domain;
16+
package org.springframework.data.jpa.example.repository.custom;
1717

1818
import javax.persistence.Column;
1919
import javax.persistence.Entity;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717

1818
import java.util.List;
1919

20-
import org.springframework.data.jpa.example.domain.User;
2120
import org.springframework.data.jpa.repository.JpaRepository;
2221
import org.springframework.data.jpa.repository.Query;
2322
import org.springframework.data.repository.CrudRepository;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,8 +17,6 @@
1717

1818
import java.util.List;
1919

20-
import org.springframework.data.jpa.example.domain.User;
21-
2220
/**
2321
* Interface for repository functionality that ought to be implemented manually.
2422
*

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,6 @@
2121
import javax.persistence.PersistenceContext;
2222
import javax.persistence.criteria.CriteriaQuery;
2323

24-
import org.springframework.data.jpa.example.domain.User;
25-
2624
/**
2725
* Implementation fo the custom repository functionality declared in {@link UserRepositoryCustom} based on JPA. To use
2826
* this implementation in combination with Spring Data JPA you can either register it programatically:

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@
2323

2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.context.annotation.Profile;
26-
import org.springframework.data.jpa.example.domain.User;
2726
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
2827
import org.springframework.jdbc.core.support.JdbcDaoSupport;
2928
import org.springframework.stereotype.Component;

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616
package org.springframework.data.jpa.example.repository.simple;
1717

18+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
19+
import org.springframework.context.annotation.Configuration;
20+
1821
/**
19-
*
2022
* @author Oliver Gierke
2123
*/
22-
public class SimpleConfiguration {
23-
24-
}
24+
@Configuration
25+
@EnableAutoConfiguration
26+
class SimpleConfiguration {}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717

1818
import java.util.List;
1919

20-
import org.springframework.data.jpa.example.domain.User;
2120
import org.springframework.data.jpa.repository.Query;
2221
import org.springframework.data.repository.CrudRepository;
2322
import org.springframework.data.repository.query.Param;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.simple;
17+
18+
import javax.persistence.Column;
19+
import javax.persistence.Entity;
20+
import javax.persistence.NamedQuery;
21+
22+
import org.springframework.data.jpa.domain.AbstractPersistable;
23+
24+
/**
25+
* Sample user class.
26+
*
27+
* @author Oliver Gierke
28+
* @author Thomas Darimont
29+
*/
30+
@Entity
31+
@NamedQuery(name = "User.findByTheUsersName", query = "from User u where u.username = ?1")
32+
public class User extends AbstractPersistable<Long> {
33+
34+
private static final long serialVersionUID = -2952735933715107252L;
35+
36+
@Column(unique = true) private String username;
37+
38+
private String firstname;
39+
private String lastname;
40+
41+
public User() {
42+
this(null);
43+
}
44+
45+
/**
46+
* Creates a new user instance.
47+
*/
48+
public User(Long id) {
49+
this.setId(id);
50+
}
51+
52+
/**
53+
* Returns the username.
54+
*
55+
* @return
56+
*/
57+
public String getUsername() {
58+
59+
return username;
60+
}
61+
62+
/**
63+
* @param username the username to set
64+
*/
65+
public void setUsername(String username) {
66+
this.username = username;
67+
}
68+
69+
/**
70+
* @return the firstname
71+
*/
72+
public String getFirstname() {
73+
return firstname;
74+
}
75+
76+
/**
77+
* @param firstname the firstname to set
78+
*/
79+
public void setFirstname(String firstname) {
80+
this.firstname = firstname;
81+
}
82+
83+
/**
84+
* @return the lastname
85+
*/
86+
public String getLastname() {
87+
return lastname;
88+
}
89+
90+
/**
91+
* @param lastname the lastname to set
92+
*/
93+
public void setLastname(String lastname) {
94+
this.lastname = lastname;
95+
}
96+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,9 +22,7 @@
2222
import org.junit.runner.RunWith;
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
25-
import org.springframework.data.jpa.example.repository.auditing.AuditableUser;
26-
import org.springframework.data.jpa.example.repository.auditing.AuditableUserRepository;
27-
import org.springframework.data.jpa.example.repository.auditing.AuditorAwareImpl;
25+
import org.springframework.test.context.ContextConfiguration;
2826
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2927
import org.springframework.test.util.ReflectionTestUtils;
3028
import org.springframework.transaction.annotation.Transactional;
@@ -35,7 +33,8 @@
3533
*/
3634
@RunWith(SpringJUnit4ClassRunner.class)
3735
@Transactional
38-
public abstract class AbstractAuditableUserSample {
36+
@ContextConfiguration(classes = AuditingConfiguration.class)
37+
public class AuditableUserSample {
3938

4039
@Autowired AuditableUserRepository repository;
4140
@Autowired AuditorAwareImpl auditorAware;

0 commit comments

Comments
 (0)