Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2013 14:34
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 anonymous/6700538 to your computer and use it in GitHub Desktop.
Save anonymous/6700538 to your computer and use it in GitHub Desktop.
//STANDARD CRUD WITH JPA : appropriate when you are dealing with objects used only as list of values
public class MyObjectDao extends JpaDao<MyObject> {}
public class MyObjectCRUDServices extends CRUDServices<MyObject> {
public MyObjectCRUDServices(MyObjectDao dao) { super(dao); }
}
//BEGINNER JPA MADNESS
public class CustomerServices extends CRUDServices<Customer> {
public CustomerServices(CustomerDao dao) { super(dao); }
public List<Customer> listPremiumCustomers() {
return dao.list(DetachedCriteria.forClass(Customer.class).add(Property.forName(“premium”).eq(true)));
//either you have all eagerly fetched and the SQL query is inefficient
//or during serialization you have n+1 select because you have lazy everywhere
}
}
//INTERMEDIATE JPA
public class CustomerServices extends CRUDServices<Customer> {
public CustomerServices(CustomerDao dao) { super(dao); }
public List<TinyCustomerDto> listPremiumCustomers() {
return TinyCustomerDto.transfert(dao.list(DetachedCriteria.forClass(Customer.class).add(Property.forName(“premium”).eq(true)).addProjectionList(“id, name”)));
//SQL query is the same I'd write myself
}
}
//QUERYDSL JPA
public class CustomerServices extends CRUDServices<Customer> {
public CustomerServices(CustomerDao dao) { super(dao); }
public List<TinyCustomerDto> listPremiumCustomers() {
return dao.list(QCustomer.where(QCustomer.premium.isTrue()).list(TinyCustomerDto.class);
//SQL query is the same I'd write myself and typesafe !!!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment