#553 - Adapt Spring Data JPA.

Replace Joda-Time with JSR-310 types.
This commit is contained in:
Mark Paluch
2020-02-10 15:36:21 +01:00
parent 523d2c8a56
commit c0d2544995
9 changed files with 30 additions and 39 deletions

View File

@@ -14,7 +14,7 @@
<name>Spring Data JPA - Examples</name>
<description>Sample projects for Spring Data JPA</description>
<url>https://projects.spring.io/spring-data-jpa</url>
<inceptionYear>2011-2014</inceptionYear>
<inceptionYear>2011</inceptionYear>
<modules>
<module>example</module>
@@ -56,11 +56,6 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>

View File

@@ -18,7 +18,8 @@ package example.springdata.jpa.showcase.snippets;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.core.QAccount;
import org.joda.time.LocalDate;
import java.sql.Date;
import java.time.LocalDate;
import com.querydsl.core.types.dsl.BooleanExpression;
@@ -32,10 +33,10 @@ public class AccountPredicates {
private static QAccount account = QAccount.account;
public static BooleanExpression isExpired() {
return expiresBefore(new LocalDate());
return expiresBefore(LocalDate.now());
}
public static BooleanExpression expiresBefore(LocalDate date) {
return account.expiryDate.before(date.toDateTimeAtStartOfDay().toDate());
return account.expiryDate.before(Date.valueOf(date));
}
}

View File

@@ -15,7 +15,7 @@
*/
package example.springdata.jpa.showcase.snippets;
import org.joda.time.LocalDate;
import java.time.LocalDate;
/**
* @author Oliver Gierke

View File

@@ -15,6 +15,7 @@
*/
package example.springdata.jpa.showcase.snippets;
import java.time.LocalDate;
import java.util.Date;
import javax.persistence.EntityManager;
@@ -22,7 +23,6 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@@ -52,7 +52,7 @@ class AccountRepositoryImpl implements AccountRepositoryCustom {
CriteriaQuery<Account> query = cb.createQuery(Account.class);
Root<Account> account = query.from(Account.class);
query.where(cb.lessThan(account.get("expiryDate").as(Date.class), reference.toDateTimeAtStartOfDay().toDate()));
query.where(cb.lessThan(account.get("expiryDate").as(Date.class), java.sql.Date.valueOf(reference)));
for (Account each : em.createQuery(query).getResultList()) {
em.remove(each);

View File

@@ -15,7 +15,9 @@
*/
package example.springdata.jpa.showcase.snippets;
import org.joda.time.LocalDate;
import java.sql.Date;
import java.time.LocalDate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@@ -37,6 +39,6 @@ class AccountRepositoryJdbcImpl implements AccountRepositoryCustom {
*/
@Override
public void removedExpiredAccounts(LocalDate reference) {
template.update("DELETE Account AS a WHERE a.expiryDate < ?", reference.toDateTimeAtStartOfDay().toDate());
template.update("DELETE Account AS a WHERE a.expiryDate < ?", Date.valueOf(reference));
}
}

View File

@@ -15,20 +15,18 @@
*/
package example.springdata.jpa.showcase.snippets;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.core.Customer;
import java.time.LocalDate;
import java.util.Date;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.joda.time.LocalDate;
import org.springframework.data.jpa.domain.Specification;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.core.Customer;
/**
* Collection of {@link Specification} implementations.
*
@@ -42,20 +40,16 @@ public class CustomerSpecifications {
* @param date
* @return
*/
public static Specification<Customer> accountExpiresBefore(final LocalDate date) {
public static Specification<Customer> accountExpiresBefore(LocalDate date) {
return new Specification<Customer>() {
return (Specification<Customer>) (root, query, cb) -> {
@Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Root<Account> accounts = query.from(Account.class);
Path<Date> expiryDate = accounts.<Date> get("expiryDate");
Predicate customerIsAccountOwner = cb.equal(accounts.<Customer> get("customer"), root);
Predicate accountExpiryDateBefore = cb.lessThan(expiryDate, java.sql.Date.valueOf(date));
Root<Account> accounts = query.from(Account.class);
Path<Date> expiryDate = accounts.<Date> get("expiryDate");
Predicate customerIsAccountOwner = cb.equal(accounts.<Customer> get("customer"), root);
Predicate accountExpiryDateBefore = cb.lessThan(expiryDate, date.toDateTimeAtStartOfDay().toDate());
return cb.and(customerIsAccountOwner, accountExpiryDateBefore);
}
return cb.and(customerIsAccountOwner, accountExpiryDateBefore);
};
}
}

View File

@@ -23,10 +23,9 @@ import static org.junit.Assert.assertThat;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.snippets.AccountRepository;
import java.time.LocalDate;
import java.util.Optional;
import org.joda.time.LocalDate;
/**
* @author Oliver Gierke
*/
@@ -36,7 +35,7 @@ public abstract class AccountRepositoryIntegrationTest {
public void removesExpiredAccountsCorrectly() throws Exception {
accountRepository.removedExpiredAccounts(new LocalDate(2011, 1, 1));
accountRepository.removedExpiredAccounts(LocalDate.of(2011, 1, 1));
assertThat(accountRepository.count(), is(1L));
}
@@ -45,7 +44,7 @@ public abstract class AccountRepositoryIntegrationTest {
Optional<Account> expired = accountRepository.findById(1L);
Optional<Account> valid = accountRepository.findById(2L);
Iterable<Account> findAll = accountRepository.findAll(expiresBefore(new LocalDate(2011, 3, 1)));
Iterable<Account> findAll = accountRepository.findAll(expiresBefore(LocalDate.of(2011, 3, 1)));
assertThat(findAll).contains(expired.get());
assertThat(findAll).doesNotContain(valid.get());

View File

@@ -21,10 +21,10 @@ import static org.assertj.core.api.Assertions.*;
import example.springdata.jpa.showcase.after.CustomerRepository;
import example.springdata.jpa.showcase.core.Customer;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.joda.time.LocalDate;
import org.springframework.data.jpa.domain.Specification;
/**
@@ -40,7 +40,7 @@ public class CustomerRepositoryIntegrationTest {
Optional<Customer> dave = repository.findById(1L);
LocalDate expiryLimit = new LocalDate(2011, 3, 1);
LocalDate expiryLimit = LocalDate.of(2011, 3, 1);
List<Customer> result = repository.findAll(accountExpiresBefore(expiryLimit));
assertThat(result).hasSize(1);

View File

@@ -20,10 +20,10 @@ import static org.assertj.core.api.Assertions.*;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Customer;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.joda.time.LocalDate;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
@@ -66,7 +66,7 @@ public class CustomerRepositoryIntegrationTest extends AbstractShowcaseTest {
Optional<Customer> dave = repository.findById(1L);
LocalDate expiryLimit = new LocalDate(2011, 3, 1);
LocalDate expiryLimit = LocalDate.of(2011, 3, 1);
List<Customer> result = repository.findAll(accountExpiresBefore(expiryLimit));
assertThat(result).hasSize(1);