Added showcase for JDK 8's Optional support on repository methods.
Abbreviated JPA sample module folder names. Renamed the Java 8 auditing sample module to Java 8 module as it not only contains samples for auditing anymore.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.after;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Repository to manage {@link Account} instances.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface AccountRepository extends CrudRepository<Account, Long> {
|
||||
|
||||
/**
|
||||
* Returns all accounts belonging to the given {@link Customer}.
|
||||
*
|
||||
* @param customer
|
||||
* @return
|
||||
*/
|
||||
List<Account> findByCustomer(Customer customer);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.after;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Repository to manage {@link Customer} instances.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
|
||||
|
||||
/**
|
||||
* Returns a page of {@link Customer}s with the given lastname.
|
||||
*
|
||||
* @param lastname
|
||||
* @param pageable
|
||||
* @return
|
||||
*/
|
||||
Page<Customer> findByLastname(String lastname, Pageable pageable);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Service interface for {@link Account}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface AccountService {
|
||||
|
||||
/**
|
||||
* Saves the given {@link Account}.
|
||||
*
|
||||
* @param account
|
||||
* @return
|
||||
*/
|
||||
Account save(Account account);
|
||||
|
||||
/**
|
||||
* Returns all {@link Account}s of the given {@link Customer}.
|
||||
*
|
||||
* @param customer
|
||||
* @return
|
||||
*/
|
||||
List<Account> findByCustomer(Customer customer);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Plain JPA implementation of {@link AccountService}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Repository
|
||||
@Transactional(readOnly = true)
|
||||
class AccountServiceImpl implements AccountService {
|
||||
|
||||
@PersistenceContext private EntityManager em;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.before.AccountService#save(example.springdata.jpa.showcase.core.Account)
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public Account save(Account account) {
|
||||
|
||||
if (account.getId() == null) {
|
||||
em.persist(account);
|
||||
return account;
|
||||
} else {
|
||||
return em.merge(account);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.before.AccountService#findByCustomer(example.springdata.jpa.showcase.core.Customer)
|
||||
*/
|
||||
@Override
|
||||
public List<Account> findByCustomer(Customer customer) {
|
||||
|
||||
TypedQuery<Account> query = em.createQuery("select a from Account a where a.customer = ?1", Account.class);
|
||||
query.setParameter(1, customer);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Service interface for {@link Customer}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface CustomerService {
|
||||
|
||||
/**
|
||||
* Returns the {@link Customer} with the given id or {@literal null} if no {@link Customer} with the given id was
|
||||
* found.
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
Customer findById(Long id);
|
||||
|
||||
/**
|
||||
* Saves the given {@link Customer}.
|
||||
*
|
||||
* @param customer
|
||||
* @return
|
||||
*/
|
||||
Customer save(Customer customer);
|
||||
|
||||
/**
|
||||
* Returns all customers.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<Customer> findAll();
|
||||
|
||||
/**
|
||||
* Returns the page of {@link Customer}s with the given index of the given size.
|
||||
*
|
||||
* @param page
|
||||
* @param pageSize
|
||||
* @return
|
||||
*/
|
||||
List<Customer> findAll(int page, int pageSize);
|
||||
|
||||
/**
|
||||
* Returns the page of {@link Customer}s with the given lastname and the given page index and page size.
|
||||
*
|
||||
* @param lastname
|
||||
* @param page
|
||||
* @param pageSize
|
||||
* @return
|
||||
*/
|
||||
List<Customer> findByLastname(String lastname, int page, int pageSize);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Plain JPA implementation of {@link CustomerService}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Repository
|
||||
@Transactional(readOnly = true)
|
||||
public class CustomerServiceImpl implements CustomerService {
|
||||
|
||||
@PersistenceContext private EntityManager em;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.before.CustomerService#findById(java.lang.Long)
|
||||
*/
|
||||
@Override
|
||||
public Customer findById(Long id) {
|
||||
return em.find(Customer.class, id);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.before.CustomerService#findAll()
|
||||
*/
|
||||
@Override
|
||||
public List<Customer> findAll() {
|
||||
return em.createQuery("select c from Customer c", Customer.class).getResultList();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.before.CustomerService#findAll(int, int)
|
||||
*/
|
||||
@Override
|
||||
public List<Customer> findAll(int page, int pageSize) {
|
||||
|
||||
TypedQuery<Customer> query = em.createQuery("select c from Customer c", Customer.class);
|
||||
|
||||
query.setFirstResult(page * pageSize);
|
||||
query.setMaxResults(pageSize);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.before.CustomerService#save(example.springdata.jpa.showcase.core.Customer)
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public Customer save(Customer customer) {
|
||||
|
||||
// Is new?
|
||||
if (customer.getId() == null) {
|
||||
em.persist(customer);
|
||||
return customer;
|
||||
} else {
|
||||
return em.merge(customer);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.before.CustomerService#findByLastname(java.lang.String, int, int)
|
||||
*/
|
||||
@Override
|
||||
public List<Customer> findByLastname(String lastname, int page, int pageSize) {
|
||||
|
||||
TypedQuery<Customer> query = em.createQuery("select c from Customer c where c.lastname = ?1", Customer.class);
|
||||
|
||||
query.setParameter(1, lastname);
|
||||
query.setFirstResult(page * pageSize);
|
||||
query.setMaxResults(pageSize);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.core;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Entity
|
||||
public class Account {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;
|
||||
|
||||
@ManyToOne private Customer customer;
|
||||
|
||||
@Temporal(TemporalType.DATE) private Date expiryDate;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Customer getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
|
||||
public Date getExpiryDate() {
|
||||
return expiryDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.core;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Entity
|
||||
public class Customer {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;
|
||||
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstname() {
|
||||
return firstname;
|
||||
}
|
||||
|
||||
public String getLastname() {
|
||||
return lastname;
|
||||
}
|
||||
}
|
||||
10
jpa/showcase/src/main/resources/data.sql
Normal file
10
jpa/showcase/src/main/resources/data.sql
Normal file
@@ -0,0 +1,10 @@
|
||||
INSERT INTO Customer (id, firstname, lastname) VALUES (1, 'Dave', 'Matthews');
|
||||
INSERT INTO Customer (id, firstname, lastname) VALUES (2, 'Carter', 'Beauford');
|
||||
INSERT INTO Customer (id, firstname, lastname) VALUES (3, 'Stephan', 'Lassard');
|
||||
|
||||
INSERT INTO Account (id, customer_id, expiry_date) VALUES (1, 1, '2010-12-31');
|
||||
INSERT INTO Account (id, customer_id, expiry_date) VALUES (2, 1, '2011-03-31');
|
||||
|
||||
INSERT INTO Customer (id, firstname, lastname) VALUES (4, 'Charly', 'Matthews');
|
||||
INSERT INTO Customer (id, firstname, lastname) VALUES (5, 'Chris', 'Matthews');
|
||||
INSERT INTO Customer (id, firstname, lastname) VALUES (6, 'Paula', 'Matthews');
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.snippets;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
import com.mysema.query.types.expr.BooleanExpression;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
import example.springdata.jpa.showcase.core.QAccount;
|
||||
|
||||
/**
|
||||
* Predicates for {@link Account}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AccountPredicates {
|
||||
|
||||
private static QAccount account = QAccount.account;
|
||||
|
||||
public static BooleanExpression isExpired() {
|
||||
return expiresBefore(new LocalDate());
|
||||
}
|
||||
|
||||
public static BooleanExpression expiresBefore(LocalDate date) {
|
||||
return account.expiryDate.before(date.toDateTimeAtStartOfDay().toDate());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.snippets;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Repository to manage {@link Account} instances.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@NoRepositoryBean
|
||||
public interface AccountRepository extends CrudRepository<Account, Long>, AccountRepositoryCustom,
|
||||
QueryDslPredicateExecutor<Account> {
|
||||
|
||||
/**
|
||||
* Returns all accounts belonging to the given {@link Customer}.
|
||||
*
|
||||
* @param customer
|
||||
* @return
|
||||
*/
|
||||
List<Account> findByCustomer(Customer customer);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.snippets;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
interface AccountRepositoryCustom {
|
||||
|
||||
void removedExpiredAccounts(LocalDate reference);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.snippets;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
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;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Repository
|
||||
class AccountRepositoryImpl implements AccountRepositoryCustom {
|
||||
|
||||
private final EntityManager em;
|
||||
|
||||
@Autowired
|
||||
public AccountRepositoryImpl(EntityManager em) {
|
||||
this.em = em;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.snippets.AccountRepositoryCustom#removedExpiredAccounts(org.joda.time.LocalDate)
|
||||
*/
|
||||
@Override
|
||||
public void removedExpiredAccounts(LocalDate reference) {
|
||||
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
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()));
|
||||
|
||||
for (Account each : em.createQuery(query).getResultList()) {
|
||||
em.remove(each);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.snippets;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Repository
|
||||
class AccountRepositoryJdbcImpl implements AccountRepositoryCustom {
|
||||
|
||||
private JdbcTemplate template;
|
||||
|
||||
public void setTemplate(JdbcTemplate template) {
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see example.springdata.jpa.showcase.snippets.AccountRepositoryCustom#removedExpiredAccounts(org.joda.time.LocalDate)
|
||||
*/
|
||||
@Override
|
||||
public void removedExpiredAccounts(LocalDate reference) {
|
||||
template.update("DELETE Account AS a WHERE a.expiryDate < ?", reference.toDateTimeAtStartOfDay().toDate());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.snippets;
|
||||
|
||||
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.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class CustomerSpecifications {
|
||||
|
||||
/**
|
||||
* All customers with an {@link Account} expiring before the given date.
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static Specification<Customer> accountExpiresBefore(final LocalDate date) {
|
||||
|
||||
return new Specification<Customer>() {
|
||||
|
||||
@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, date.toDateTimeAtStartOfDay().toDate());
|
||||
|
||||
return cb.and(customerIsAccountOwner, accountExpiryDateBefore);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
7
jpa/showcase/src/snippets/resources/jdbc.xml
Normal file
7
jpa/showcase/src/snippets/resources/jdbc.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<bean id="accountDaoImpl" class="example.springdata.jpa.showcase.after.AccountDaoJdbcImpl">
|
||||
<property name="template">
|
||||
<bean class="org.springframework.jdbc.core.JdbcTemplate">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.snippets.test;
|
||||
|
||||
import static example.springdata.jpa.showcase.snippets.AccountPredicates.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
import example.springdata.jpa.showcase.snippets.AccountRepository;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class AccountRepositoryIntegrationTest {
|
||||
|
||||
private AccountRepository accountRepository;
|
||||
|
||||
public void removesExpiredAccountsCorrectly() throws Exception {
|
||||
|
||||
accountRepository.removedExpiredAccounts(new LocalDate(2011, 1, 1));
|
||||
assertThat(accountRepository.count(), is(1L));
|
||||
}
|
||||
|
||||
public void findsExpiredAccounts() {
|
||||
|
||||
Account expired = accountRepository.findOne(1L);
|
||||
Account valid = accountRepository.findOne(2L);
|
||||
|
||||
Iterable<Account> findAll = accountRepository.findAll(expiresBefore(new LocalDate(2011, 3, 1)));
|
||||
|
||||
assertThat(findAll, hasItem(expired));
|
||||
assertThat(findAll, not(hasItem(valid)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.snippets.test;
|
||||
|
||||
import static example.springdata.jpa.showcase.snippets.CustomerSpecifications.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.jpa.domain.Specifications.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import example.springdata.jpa.showcase.after.CustomerRepository;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Snippets to show the usage of {@link Specification}s.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class CustomerRepositoryIntegrationTest {
|
||||
|
||||
private CustomerRepository repository;
|
||||
|
||||
public void findsCustomersBySpecification() throws Exception {
|
||||
|
||||
Customer dave = repository.findOne(1L);
|
||||
|
||||
LocalDate expiryLimit = new LocalDate(2011, 3, 1);
|
||||
List<Customer> result = repository.findAll(where(accountExpiresBefore(expiryLimit)));
|
||||
|
||||
assertThat(result.size(), is(1));
|
||||
assertThat(result, hasItems(dave));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import example.springdata.jpa.showcase.AbstractShowcaseTest.TestConfig;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@SpringApplicationConfiguration(classes = TestConfig.class)
|
||||
@Transactional
|
||||
public abstract class AbstractShowcaseTest extends AbstractTransactionalJUnit4SpringContextTests {
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan
|
||||
static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
@BeforeTransaction
|
||||
public void setupData() throws Exception {
|
||||
|
||||
deleteFromTables("account", "customer");
|
||||
executeSqlScript("classpath:data.sql", false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.after;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import example.springdata.jpa.showcase.AbstractShowcaseTest;
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Integration tests for Spring Data JPA {@link AccountRepository}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AccountRepositoryIntegrationTest extends AbstractShowcaseTest {
|
||||
|
||||
@Autowired AccountRepository accountRepository;
|
||||
@Autowired CustomerRepository customerRepository;
|
||||
|
||||
@Test
|
||||
public void savesAccount() {
|
||||
|
||||
Account account = accountRepository.save(new Account());
|
||||
assertThat(account.getId(), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsCustomersAccounts() {
|
||||
|
||||
Customer customer = customerRepository.findOne(1L);
|
||||
List<Account> accounts = accountRepository.findByCustomer(customer);
|
||||
|
||||
assertFalse(accounts.isEmpty());
|
||||
assertThat(accounts.get(0).getCustomer(), is(customer));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.after;
|
||||
|
||||
import static example.springdata.jpa.showcase.snippets.CustomerSpecifications.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.jpa.domain.Specifications.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import example.springdata.jpa.showcase.AbstractShowcaseTest;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Integration tests for Spring Data JPA {@link CustomerRepository}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class CustomerRepositoryIntegrationTest extends AbstractShowcaseTest {
|
||||
|
||||
@Autowired CustomerRepository repository;
|
||||
|
||||
@Test
|
||||
public void findsAllCustomers() throws Exception {
|
||||
|
||||
Iterable<Customer> result = repository.findAll();
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertTrue(result.iterator().hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsFirstPageOfMatthews() throws Exception {
|
||||
|
||||
Page<Customer> customers = repository.findByLastname("Matthews", new PageRequest(0, 2));
|
||||
|
||||
assertThat(customers.getContent().size(), is(2));
|
||||
assertFalse(customers.hasPreviousPage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsCustomerById() throws Exception {
|
||||
|
||||
Customer customer = repository.findOne(2L);
|
||||
|
||||
assertThat(customer.getFirstname(), is("Carter"));
|
||||
assertThat(customer.getLastname(), is("Beauford"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsCustomersBySpecification() throws Exception {
|
||||
|
||||
Customer dave = repository.findOne(1L);
|
||||
|
||||
LocalDate expiryLimit = new LocalDate(2011, 3, 1);
|
||||
List<Customer> result = repository.findAll(where(accountExpiresBefore(expiryLimit)));
|
||||
|
||||
assertThat(result.size(), is(1));
|
||||
assertThat(result, hasItems(dave));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.before;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import example.springdata.jpa.showcase.AbstractShowcaseTest;
|
||||
import example.springdata.jpa.showcase.core.Account;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Integration test for {@link AccountService}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class AccountServiceIntegrationTest extends AbstractShowcaseTest {
|
||||
|
||||
@Autowired AccountService accountService;
|
||||
@Autowired CustomerService customerService;
|
||||
|
||||
@Test
|
||||
public void savesAccount() {
|
||||
|
||||
Account account = accountService.save(new Account());
|
||||
assertThat(account.getId(), is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testname() throws Exception {
|
||||
|
||||
Customer customer = customerService.findById(1L);
|
||||
|
||||
List<Account> accounts = accountService.findByCustomer(customer);
|
||||
|
||||
assertThat(accounts, is(not(empty())));
|
||||
assertThat(accounts.get(0).getCustomer(), is(customer));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.showcase.before;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import example.springdata.jpa.showcase.AbstractShowcaseTest;
|
||||
import example.springdata.jpa.showcase.core.Customer;
|
||||
|
||||
/**
|
||||
* Integration test for {@link CustomerService}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class CustomerServiceIntegrationTest extends AbstractShowcaseTest {
|
||||
|
||||
@Autowired CustomerService repository;
|
||||
|
||||
@Test
|
||||
public void findsAllCustomers() throws Exception {
|
||||
|
||||
List<Customer> result = repository.findAll();
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertFalse(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsPageOfMatthews() throws Exception {
|
||||
|
||||
List<Customer> customers = repository.findByLastname("Matthews", 0, 2);
|
||||
|
||||
assertThat(customers.size(), is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsCustomerById() throws Exception {
|
||||
|
||||
Customer customer = repository.findById(2L);
|
||||
|
||||
assertThat(customer.getFirstname(), is("Carter"));
|
||||
assertThat(customer.getLastname(), is("Beauford"));
|
||||
}
|
||||
}
|
||||
16
jpa/showcase/src/test/resources/logback.xml
Normal file
16
jpa/showcase/src/test/resources/logback.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework.data" level="debug" />
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user