|
Project Information
Featured
Links
|
Hibernate Generic D.A.O. FrameworkNOTICE: Fellow developers, As of Dec 2011, I am not currently supporting this project. I have been the sole owner of this project from the beginning, but I will no longer be following up on issues or making releases. At this point there is no one else to take over this responsibility, so it will not be happening. There are still some users who respond on the Google group. I apologize for the inconvenience. If anyone would like to become a contributor and take charge of patches and releases, contact me and I can work with you on it. My contact info can be found here. It is possible that I will return to the project at some time, but at this point it's just not a technology stack that I am working with. The motivation behind the frameworkWe had worked on a project where we hand-coded all of our DAOs. This produced four irksome difficulties: (1) Method names and implementations were not altogether consistent. (2) It was a pain to make additional columns sortable or filterable, and as a result, a lot of pages lacked good sorting and filtering. (3) Making additional DAOs was tedious and took a fair amount of time. (4) Writing tests for DAOs is tricky and tedious, so no one really did. This framework aims to ease our troubles. Why might you consider looking into this framework?
*A fairly simple adapter is required for each JPA provider. Right now we only have an adapter for Hibernate Entity Manager. If anyone would like to contribute an adapter for any other JPA provider (OpenJPA, TopLink, etc.), that would be great. **If time permits, we would like to eventually post our corresponding Adobe Flex 3 framework and utilities. More InformationWiki Documentation: UserGuide Javadoc: http://hibernate-generic-dao.googlecode.com/svn/trunk/docs/api/index.html Blog: http://hibernategenericdao.wordpress.com/ Questions and CommentsPlease post at http://groups.google.com/group/java-generic-dao. Code ExamplesCreating DAOs for individual model classes:Simply extend the GenericDAO class with the specific type. public interface ProjectDAO extends GenericDAO<Project, Long> {
}
public class ProjectDAOImpl extends GenericDAOImpl<Project, Long> implements ProjectDAO {
}The following methods (and several more) are now available on ProjectDAOProject project = projectDAO.find(projectId);
List<Project> list = projectDAO.findAll();
projectDAO.save(project);
projectDAO.remove(project);
projectDAO.removeById(project.getId());
Search search = new Search();
search.addFilterEqual("name", "hibernate-generic-dao");
List<Project> list = projectDAO.search(search);
int count = projectDAO.count(search);
SearchResult<Project> result = projectDAO.searchAndCount(search);
list = result.getResult();
count = result.getTotalCount();
search.clear();
search.addField("rating", Field.OP_AVG);
int avgProjectRating = (Integer) prjoectDAO.searchUnique(search);A GeneralDAO is also provided with DAO methods for any entity:public interface GeneralDAO {
public <T> T find(Class<T> type, Serializable id);
public <T> T[] find(Class<T> type, Serializable... ids);
public <T> T getReference(Class<T> type, Serializable id);
public <T> T[] getReferences(Class<T> type, Serializable... ids);
public boolean save(Object entity);
public boolean[] save(Object... entities);
public boolean remove(Object entity);
public void remove(Object... entities);
public boolean removeById(Class<?> type, Serializable id);
public void removeByIds(Class<?> type, Serializable... ids);
public <T> List<T> findAll(Class<T> type);
public List search(ISearch search);
public Object searchUnique(ISearch search);
public int count(ISearch search);
public SearchResult searchAndCount(ISearch search);
public boolean isAttached(Object entity);
public void refresh(Object... entities);
public void flush();
public Filter getFilterFromExample(Object example);
public Filter getFilterFromExample(Object example, ExampleOptions options);
}Search DTO usage examplesSearch search = new Search(Project.class);
//filtering
search.addFilterEqual("name", "hibernate-generic-dao");
search.addFilterLessThan("completionDate", new Date());
search.addFilterOr(
Filter.equal("name", "Jack"),
Filter.and(
Filter.equal("name", "Jill"),
Filter.like("location", "%Chicago%"),
Filter.greaterThan("age", 5)
)
);
search.addFilterIn("name", "Jack", "Jill", "Bob");
search.addFilterNot(Filter.in("name","Jack", "Jill", "Bob"));
//sorting
search.addSort("name");
search.addSort("age", true); //descending
//projection
search.addField("name");
search.addField("location");
//or with column operators
search.addField("rating", Field.OP_AVG);
search.addField("developerCount", Field.OP_MAX);
//paging
search.setMaxResults(15); //a.k.a. results per page
search.setPage(3);
//controlling eager fetching of relationships
serach.addFetch("owner");Nested properties are also fully supported...search.addFilterEqual("status.name", "active");
search.addFilterGreaterThan("workgroup.manager.salary", 75000.00);
search.addSort("status.name");
|