Yet Another DAOHibernate Guide           YAGDAO 0.3.1
yagdao   http://www.altuure.com/projects/yagdao   Mert Can Akkan     mcakkan@yahoo.com     http://www.altuure.com
overview   Popular Java ORM layer     JPA 2.0     Hibernate   Spring 3.0+ Support (optional)   Lightweight   No Impl...
dependency-heaven   Lightweight framework with minimal dependencyGroupId                     ArtifactId                Ver...
maven dependecy<dependencies>   <dependency>         <groupId>com.altuure</groupId>         <artifactId>com.altuure.yagdao...
my first yagdao   easy implementationpublic interface UserDAO extends  GenericDAO<User,Long>{}
quickstart   GenericHibernateDAOFactoryuserDAO = (UserDAO)  GenericHibernateDAOFactory.createInstance(UserDAO.class,  ses...
springframework support   package scan feature<yagdao:hibernate id="DAOFactory1"      base-package="com.altuure.yagdao.bl...
GenericDAO   save(Object entity)   update(Object entity)   load(T id)   loadLazy(T id)   delete(Object object)   del...
custom methodscreate & update       Get rid of all setter and getter operationspublic interface UserDAO extends GenericDA...
custom methods    query   Embeded Query Support@YMethod(    type = YMethodType.QUERY,    query="select u.username from Us...
custom methodsexecute@YMethod(type = YMethodType.EXECUTE,  query="update User set password=:password")int updateAllPasswor...
custom methodsappend   APPEND Method handler is a simple query builder    in which you can append query strings with not ...
custom methodscriteria &count(experimental)   Criteria method handler is like append method    handler tries to build a q...
custom methodscriteria•Custom Paging and order is supported•Selected field and fetch are supported•Grouping and having cla...
custom methodscount @YMethod(type = YMethodType.COUNT)    long count2(@YParameter(value = "pint", operator =YOperator.GE) ...
smart parameters & return types   YPage: enables order and paging       criteria methods   YLimit: enables only paging ...
pagingTo add paging to any querying method is easy just  add YPage,YLimit as a method parameter@YMethod(type = YMethodType...
prefetch result sizeDefining ‘SearchResultList’ as a return type enables a  count queires for all methods@YMethod(type = Y...
and more   Object based and method based fetch support@YMethod(type = YMethodType.CRITERIA)@YFetch({ "product", "order", ...
thanks For more please see sample application:http://code.google.com/p/yagdao/downloads/list?q=label:Type-Samplemaven jet...
Upcoming SlideShare
Loading in...5
×

yagdao-0.3.1 hibernate guide

804
-1

Published on

yagdao hibernate guide
http://www.altuure.com/projects/yagdao/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
804
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

yagdao-0.3.1 hibernate guide

  1. 1. Yet Another DAOHibernate Guide YAGDAO 0.3.1
  2. 2. yagdao http://www.altuure.com/projects/yagdao Mert Can Akkan  mcakkan@yahoo.com  http://www.altuure.com
  3. 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. 4. dependency-heaven Lightweight framework with minimal dependencyGroupId ArtifactId Version Optionalcglib cglib 2.2 Nocommons-logging commons-logging 1.0.2 Noorg.antlr antlr-runtime 3.2 Noorg.slf4j slf4j-log4j12 1.5.8 Noorg.apache.geronimo.specs geronimo-jpa_2.0_spec 1.1 JPAorg.hibernate hibernate-core 3.5.1-Final hibernateorg.hibernate hibernate-entitymanager 3.5.1-Final hibernateorg.springframework spring-beans 3.0.4.RELEASE spring supportorg.springframework spring-jdbc 3.0.4.RELEASE spring supportorg.springframework spring-orm 3.0.4.RELEASE Spring support
  5. 5. maven dependecy<dependencies> <dependency> <groupId>com.altuure</groupId> <artifactId>com.altuure.yagdao</artifactId> <version>0.3.1</version> </dependency><dependencies><!-- repo.altuure.com--><repositories> <repository> <id>repo.altuure.com</id> <name>repo.altuure.com</name> <url>http://repo.altuure.com</url> <layout>default</layout> </repository></repositories>
  6. 6. my first yagdao easy implementationpublic interface UserDAO extends GenericDAO<User,Long>{}
  7. 7. quickstart GenericHibernateDAOFactoryuserDAO = (UserDAO) GenericHibernateDAOFactory.createInstance(UserDAO.class, sessionAccessor);The GenericHibernateDAOFactory will create instance of given DAO at the runtime
  8. 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. 9. GenericDAO save(Object entity) update(Object entity) load(T id) loadLazy(T id) delete(Object object) delete(T id) vs….
  10. 10. custom methodscreate & update Get rid of all setter and getter operationspublic interface UserDAO extends GenericDAO<User,Long>{ @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. 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. 12. custom methodsexecute@YMethod(type = YMethodType.EXECUTE, query="update User set password=:password")int updateAllPasswords(String newPassword);Tip:All execute methods must return an integer
  13. 13. custom methodsappend APPEND Method handler is a simple query builder in which you can append query strings with not null parameters@YMethod(type = YMethodType.APPEND,select = "pbyte,count(id)",groupBy = "pbyte",having = "count(id)>10") List<SimpleBean> appendQuery( @YParameter("pint>=?") int i);
  14. 14. custom methodscriteria &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. 15. custom methodscriteria•Custom Paging and order is supported•Selected field and fetch are supported•Grouping and having clauses are not supported@YMethod(type = YMethodType.CRITERIA)SearchResultList<SimpleBean> criteria1(@YParameter(value = "pint", operator = YOperator.GE) Integer arg1);
  16. 16. custom methodscount @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. 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<T> result, long totalCount, YPage paging) { super(result); this.totalCount = totalCount; this.paging = paging; }
  18. 18. pagingTo 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. 19. prefetch result sizeDefining ‘SearchResultList’ as a return type enables a count queires for all methods@YMethod(type = YMethodType.CRITERIA) SearchResultList<SimpleBean> criteria1(@YParameter(value = "pint", operator = YOperator.GE) Integer arg1);
  20. 20. and more Object based and method based fetch support@YMethod(type = YMethodType.CRITERIA)@YFetch({ "product", "order", "order.customer" })List<OrderItem> findByCustomerCityAndMaxPrice2(…); projection support at append methods
  21. 21. thanks For more please see sample application:http://code.google.com/p/yagdao/downloads/list?q=label:Type-Samplemaven jetty:run
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×