Post on 26-Oct-2015
17/09/13 GenericDAOExamples - hibernate-generic-dao - Examples of using GenericDAO - Generic DAO implementation: extendable, detailed search, remote …
https://code.google.com/p/hibernate-generic-dao/wiki/GenericDAOExamples 1/1
Project Home Downloads Wiki Issues Source
Updated Feb 26, 2011 by dwolvert
My favorites ▼ | Sign in
hibernate-generic-daoGeneric DAO implementation: extendable, detailed search, remote service interface Search projects
Search Current pages for Search
GenericDAOExamples
Examples of using GenericDAO
Here is all the code it usually takes to create a DAO interface and implementation for a Project domain object; all the content is inherited from
the superclass:
public interface ProjectDAO extends GenericDAO<Project, Long> {
}
public class ProjectDAOImpl extends GenericDAOImpl<Project, Long> implements ProjectDAO {
}
This could be customized by adding and/or overriding methods if needed:
public interface ProjectDAO extends GenericDAO<Project, Long> { public Project findByName(String name); public Project findByStatus(String status);}
public class ProjectDAOImpl extends GenericDAOImpl<Project, Long> implements ProjectDAO { //basic added method public Project findByName(String name) { Criteria crit getSession().createCriteria(Project.class); crit.add(Restrictions.eq("name", name)); return (Project) crit.uniqueResult(); }
//basic added method using search public List<Project> searchByStatus(String status) { return search(new Search().addFilterEqual("status", status)); }
//overriding a method @Override public void save(Project project) { if (project.getId() == null) project.setStatus("NEW"); super.save(project); }}
A more complex example can be found in the test code at: http://code.google.com/p/hibernate-generic-dao/source/browse/tags/1.0.0/dao-hibernate/src/test/java/test/googlecode/genericdao/dao/hibernate/dao/ProjectDAOImpl.java
Comment by kat...@gmail.com, Jul 14, 2011
Is it possible to use naming strategies to externalize the hql queries. Are there any examples of that?
Comment by project member dwolvert, Jul 15, 2011
The framework doesn't provide anything for that. Of course you can always add whatever custom methods you want to your DAO and use any and allHibernate features.
Comment by ben...@gmail.com, Sep 12, 2012
The Criteria crit getSesion().createCriteria(Project.class); is incorrect
► Sign in to add a comment
Terms - Privacy - Project Hosting Help
Powered by Google Project Hosting