YAGDAO Yet Another DAO Hibernate Guide
yagdao Mert Can Akkan
overview Popular Java ORM layer JPA 2.0 Hibernate Spring 3.0+ Support (optional) Lightweight No Implementation Framework No Static Code Generation Annotation Based GenericDAO/CRUD operations Custom operations
dependency-heaven GroupIdArtifactIdVersionOptional cglib 2.2No commons-logging 1.0.2No org.antlrantlr-runtime3.2No org.slf4jslf4j-log4j No org.apache.geronimo.specsgeronimo-jpa_2.0_spec1.1JPA org.hibernatehibernate-core3.5.1-Finalhibernate org.hibernatehibernate-entitymanager3.5.1-Finalhibernate org.springframeworkspring-beans3.0.4.RELEASEspring support org.springframeworkspring-jdbc3.0.4.RELEASEspring support org.springframeworkspring-orm3.0.4.RELEASESpring support Lightweight framework with minimal dependency
maven dependecy com.altuure com.altuure.yagdao repo.altuure.com default
my first yagdao easy implementation public interface UserDAO extends GenericDAO { }
quickstart GenericHibernateDAOFactory userDAO = (UserDAO) GenericHibernateDAOFactory.createInstance(UserDAO.class, sessionAccessor); The GenericHibernateDAOFactory will create instance of given DAO at the runtime
springframework support package scan feature <yagdao:hibernate id="DAOFactory1" base-package="com.altuure.yagdao.blog.dao" session-factory=" mySessionFactory "/> one by one definition <yagdao:hibernate base-class="com.altuure.yagdao.blog.dao.UserDAO" session-factory="mySessionFactory"/>
GenericDAO save(Object entity) update(Object entity) load(T id) loadLazy(T id) delete(Object object) delete(T id) vs….
custom methods create & update Get rid of all setter and getter operations public interface UserDAO extends GenericDAO = YMethodType.SAVE) User = YMethodType.UPDATE) User updateRoles(long roles); }
custom methods query Embeded Query type = YMethodType.QUERY, query="select u.username from User u where u. =: “ ) String = " ")String ); Named Query = YMethodType.QUERY,queryName="findBy ") String = " ")String );
custom methods = YMethodType.EXECUTE, query="update User set password=:password") int updateAllPasswords(String newPassword); Tip: All execute methods must return an integer
custom methods = YMethodType.APPEND, select = "pbyte,count(id)", groupBy = "pbyte", having = "count(id)>10") List int i); APPEND Method handler is a simple query builder in which you can append query strings with not null parameters
custom methods criteria &count(experimental) Criteria method handler is like append method handler tries to build a query with not null values by parsing query parameters. Due to incomplete criteria API of hibernate it supports hibernate partially
custom methods = YMethodType.CRITERIA) SearchResultList = "pint", operator = YOperator.GE) Integer arg1); Custom Paging and order is supported Selected field and fetch are supported Grouping and having clauses are not supported
custom methods = YMethodType.COUNT) long = "pint", operator = YOperator.GE) Integer = "pdate", operator = YOperator.LE) Date endDate); Returns only count query result of criteria method To execute both see SearchResultList
smart parameters & return types YPage: enables order and paging criteria methods YLimit: enables only paging append method criteria methods SearchResultList: fetch total size of result list public SearchResultList(List result, long totalCount, YPage paging) { super(result); this.totalCount = totalCount; this.paging = paging; }
paging To add paging to any querying method is easy just add YPage,YLimit as a method = YMethodType.APPEND,orderBy = "id desc") SearchResultList byte arg1,YLimit limit); PS: YLimit is valid in all while YPage is valid on only Criteria Methods
prefetch result size Defining ‘SearchResultList’ as a return type enables a count queires for all = YMethodType.CRITERIA) SearchResultList = "pint", operator = YOperator.GE) Integer arg1);
and more Object based and method based fetch = "product", "order", "order.customer" }) List findByCustomerCityAndMaxPrice2(…); projection support at append methods
thanks For more please see sample application: maven jetty:run