Presentation is loading. Please wait.

Presentation is loading. Please wait.

YAGDAO 0.3.1 Yet Another DAO Hibernate Guide. yagdao   Mert Can Akkan.

Similar presentations


Presentation on theme: "YAGDAO 0.3.1 Yet Another DAO Hibernate Guide. yagdao   Mert Can Akkan."— Presentation transcript:

1 YAGDAO 0.3.1 Yet Another DAO Hibernate Guide

2 yagdao  http://www.altuure.com/projects/yagdao http://www.altuure.com/projects/yagdao  Mert Can Akkan  mcakkan@yahoo.com mcakkan@yahoo.com  http://www.altuure.com http://www.altuure.com

3 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

4 dependency-heaven GroupIdArtifactIdVersionOptional cglib 2.2No commons-logging 1.0.2No org.antlrantlr-runtime3.2No org.slf4jslf4j-log4j121.5.8No 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

5 maven dependecy com.altuure com.altuure.yagdao 0.3.1 repo.altuure.com http://repo.altuure.com default

6 my first yagdao  easy implementation public interface UserDAO extends GenericDAO { }

7 quickstart  GenericHibernateDAOFactory userDAO = (UserDAO) GenericHibernateDAOFactory.createInstance(UserDAO.class, sessionAccessor); The GenericHibernateDAOFactory will create instance of given DAO at the runtime

8 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"/>

9 GenericDAO  save(Object entity)  update(Object entity)  load(T id)  loadLazy(T id)  delete(Object object)  delete(T id)  vs….

10 custom methods create & update  Get rid of all setter and getter operations public interface UserDAO extends GenericDAO { @YMethod(type = YMethodType.SAVE) User create( @YParameter("username")String username, @YParameter("password")String password, @YParameter("email")String email); @YMethod(type = YMethodType.UPDATE) User updateRoles(long id, @YParameter("roles")Set roles); }

11 custom methods query  Embeded Query Support @YMethod( type = YMethodType.QUERY, query="select u.username from User u where u.email=:email“ ) String findUsernameByEmailQuery( @YParameter(value = "email")String email);  Named Query Support @YMethod(type = YMethodType.QUERY,queryName="findByEmail") String findUsernameByEmailNamed( @YParameter(value = "email")String email);

12 custom methods execute @YMethod(type = YMethodType.EXECUTE, query="update User set password=:password") int updateAllPasswords(String newPassword); Tip: All execute methods must return an integer

13 custom methods append @YMethod(type = YMethodType.APPEND, select = "pbyte,count(id)", groupBy = "pbyte", having = "count(id)>10") List appendQuery( @YParameter("pint>=?") int i);  APPEND Method handler is a simple query builder in which you can append query strings with not null parameters

14 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

15 custom methods criteria @YMethod(type = YMethodType.CRITERIA) SearchResultList criteria1(@YParameter(value = "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

16 custom methods count @YMethod(type = YMethodType.COUNT) long count2(@YParameter(value = "pint", operator = YOperator.GE) Integer arg1, @YParameter(value = "pdate", operator = YOperator.LE) Date endDate); Returns only count query result of criteria method To execute both see SearchResultList

17 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; }

18 paging To add paging to any querying method is easy just add YPage,YLimit as a method parameter @YMethod(type = YMethodType.APPEND,orderBy = "id desc") SearchResultList appendPage1(@YParameter("pbyte>=?") byte arg1,YLimit limit); PS: YLimit is valid in all while YPage is valid on only Criteria Methods

19 prefetch result size Defining ‘SearchResultList’ as a return type enables a count queires for all methods @YMethod(type = YMethodType.CRITERIA) SearchResultList criteria1(@YParameter(value = "pint", operator = YOperator.GE) Integer arg1);

20 and more  Object based and method based fetch support @YMethod(type = YMethodType.CRITERIA) @YFetch({ "product", "order", "order.customer" }) List findByCustomerCityAndMaxPrice2(…);  projection support at append methods

21 thanks For more please see sample application: http://code.google.com/p/yagdao/downloads/list?q=label:Type-Sample maven jetty:run


Download ppt "YAGDAO 0.3.1 Yet Another DAO Hibernate Guide. yagdao   Mert Can Akkan."

Similar presentations


Ads by Google