Skip to content

Instantly share code, notes, and snippets.

@preetham-salehundam
Last active April 23, 2018 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save preetham-salehundam/e0fcc5a42cb842331b87771cd86a0276 to your computer and use it in GitHub Desktop.
Save preetham-salehundam/e0fcc5a42cb842331b87771cd86a0276 to your computer and use it in GitHub Desktop.
Custom JPA repo
package com.jpa.demo.repo;
import com.jpa.demo.entities.Customer;
import java.util.List;
/**
*
* The class that implements should contain the IMPL postfix for spring recognise and provide implementation during run time
*
* */
/**
* mimic how spring data provides implementation durinhg runtime when we extend to JPA REPO
*
* */
public interface CustomJPARepository<T> {
Iterable<T> retriveAll();
//PROVIDING CUSTOM SAVE IMPLEMENATION.
// this has higher precendence over save method from CRUD REPO
// if return type is defined as T directly, we can either use T class or a sub class of T
// when u say S extends T, a DTO can be passed which need not be extending to T,
// the save method however returns an new object which is extended to T
<S extends T> S save(S entity);
}
package com.jpa.demo.repo;
import com.jpa.demo.entities.Customer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/**
*
* The class that implements should contain the IMPL postfix for spring recognise and provide implementation during run time
*
* */
/**
* mimic how spring data provides implementation durinhg runtime when we extend to JPA REPO
* spring data searches for the implementation with IMPL postfix and uses it as implementation at run time
* */
// add <T> as <Customer>
public class CustomJPARepositoryImpl implements CustomJPARepository<Customer> {
private static final Logger log = LoggerFactory.getLogger(CustomJPARepositoryImpl.class);
@Autowired
JdbcTemplate jdbc;
@PersistenceContext
EntityManager entityManager;
@Override
public List<Customer> retriveAll() {
log.info("query being executed from customrepoimpl");
return jdbc.query("Select * from customers c order by c.customer_id",(rs, rowNum) -> {
return new Customer(rs.getLong("customer_id"),rs.getString("first_name"), rs.getString("last_name"));
});
}
@Override
@Transactional
public Customer save(Customer entity) {
log.info("saving entity "+ entity);
entityManager.persist(entity);
return entity;
}
}
package com.jpa.demo.service;
import com.jpa.demo.entities.Customer;
import com.jpa.demo.repo.CustomerRepository;
import net.bytebuddy.asm.Advice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* refer : http://www.baeldung.com/java-completablefuture
*/
@Component
public class FuturesTester {
@Autowired
CustomerRepository repo;
private static final Logger log = LoggerFactory.getLogger(FuturesTester.class);
/**
* retriveAll is picked up from CustomRepositoryImpl during runtime
*/
public CompletableFuture<List<Customer>> fetchAllUsingCustomRepo(){
CompletableFuture cf = CompletableFuture.supplyAsync(() -> repo.retriveAll());
return cf;
}
/**
* save is picked up from CustomRepositoryImpl during runtime
*/
//overiging with custom save behaviour
public CompletableFuture<Customer> customSave(Customer customer){
// custom repo CustomerRepoImpl's save
return CompletableFuture.supplyAsync(() -> repo.save(customer));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment