1 / 21

Exploring YagDAO: A Versatile Java DAO Framework for JPA and Hibernate

YagDAO is a lightweight and flexible Java Data Access Object (DAO) framework built on JPA 2.0 and Hibernate, designed to streamline CRUD operations while supporting custom queries with minimal dependencies. It offers an annotation-based interface, removing the need for static code generation. With support for Spring 3.0 and above, YagDAO enables seamless integration into Java applications. This guide covers the essential setup for implementing a UserDAO, along with examples of custom methods for querying and data manipulation, making it ideal for modern Java developers.

watson
Download Presentation

Exploring YagDAO: A Versatile Java DAO Framework for JPA and Hibernate

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. yagdao 0.3.1 Yet Another DAO JPA Guide

  2. yagdao • http://www.altuure.com/projects/yagdao • Mert Can Akkan • mcakkan@yahoo.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 Lightweight framework with minimal dependency

  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. my first yagdao • easy implementation public interface UserDAO extends GenericDAO<User,Long>{ }

  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:jpaid="DAOFactory1" base-package="com.altuure.yagdao.blog.dao" entity-manager-factory="entityManagerFactory"/> • one by one definition <yagdao:jpa 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 methodscreate & update • Get rid of all setter and getter operations public 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. custom methodsquery • 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 methodsexecute @YMethod(type = YMethodType.EXECUTE, query="update User set password=:password") intupdateAllPasswords(String newPassword); Tip: All execute methods must return an integer

  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>=?") inti);

  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.

  15. custom methods criteria @YMethod(type = YMethodType.CRITERIA) SearchResultList<SimpleBean> criteria1(@YParameter(value = "pint>=") Integer arg1); • Custom Paging and order is supported • Selected field and fetch are supported

  16. custom methodscount @YMethod(type = YMethodType.COUNT) long count2(@YParameter("pint") Integer arg1, @YParameter("pdate<=")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<T> 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<SimpleBean> criteria1(@YParameter(value = "pint>=") Integer arg1);

  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. thanks  For more please see sample application: http://code.google.com/p/yagdao/downloads/list?q=label:Type-Sample maven jetty:run http://localhost:8080/blog

More Related