Created
September 25, 2013 14:34
-
-
Save anonymous/6700538 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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