SlideShare a Scribd company logo
1 of 21
Download to read offline
Yet Another DAO
JPA 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 Implementation Framework
   No Static Code Generation
   Annotation Based
   GenericDAO/CRUD operations
   Custom operations
dependency-heaven

   Lightweight framework with minimal dependency
GroupId                     ArtifactId                Version       Optional

cglib                       cglib                     2.2                 No
commons-logging             commons-logging           1.0.2               No
org.antlr                   antlr-runtime             3.2                 No
org.slf4j                   slf4j-log4j12             1.5.8               No
org.apache.geronimo.specs   geronimo-jpa_2.0_spec     1.1                 JPA
org.hibernate               hibernate-core            3.5.1-Final      hibernate
org.hibernate               hibernate-entitymanager   3.5.1-Final      hibernate
org.springframework         spring-beans              3.0.4.RELEASE spring support
org.springframework         spring-jdbc               3.0.4.RELEASE spring support
org.springframework         spring-orm                3.0.4.RELEASE Spring support
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>
my first yagdao
   easy implementation

public interface UserDAO extends
  GenericDAO<User,Long>{

}
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:jpa id="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"/>
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<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);


    }
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);
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
custom methods
append
   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);
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.
custom methods
criteria
•Custom Paging and order is supported
•Selected field and fetch are supported




@YMethod(type = YMethodType.CRITERIA)
SearchResultList<SimpleBean>
  criteria1(@YParameter(value = "pint>=") Integer
  arg1);
custom methods
count
@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
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;
        }
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
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);
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
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 Content

What's hot

Overview of Google spreadsheet API for Java by Nazar Kostiv
Overview of Google spreadsheet API for Java by Nazar Kostiv Overview of Google spreadsheet API for Java by Nazar Kostiv
Overview of Google spreadsheet API for Java by Nazar Kostiv
IT Booze
 
Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능
knight1128
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnit
Peter Wilcsinszky
 

What's hot (20)

Code transformation With Spoon
Code transformation With SpoonCode transformation With Spoon
Code transformation With Spoon
 
Unit testing with mock libs
Unit testing with mock libsUnit testing with mock libs
Unit testing with mock libs
 
Architecture Components
Architecture ComponentsArchitecture Components
Architecture Components
 
The Ring programming language version 1.5.2 book - Part 76 of 181
The Ring programming language version 1.5.2 book - Part 76 of 181The Ring programming language version 1.5.2 book - Part 76 of 181
The Ring programming language version 1.5.2 book - Part 76 of 181
 
Why Kotlin - Apalon Kotlin Sprint Part 1
Why Kotlin - Apalon Kotlin Sprint Part 1Why Kotlin - Apalon Kotlin Sprint Part 1
Why Kotlin - Apalon Kotlin Sprint Part 1
 
Java Quiz - Meetup
Java Quiz - MeetupJava Quiz - Meetup
Java Quiz - Meetup
 
Overview of Google spreadsheet API for Java by Nazar Kostiv
Overview of Google spreadsheet API for Java by Nazar Kostiv Overview of Google spreadsheet API for Java by Nazar Kostiv
Overview of Google spreadsheet API for Java by Nazar Kostiv
 
Web scraping using scrapy - zekeLabs
Web scraping using scrapy - zekeLabsWeb scraping using scrapy - zekeLabs
Web scraping using scrapy - zekeLabs
 
Тестирование на Android с Dagger 2
Тестирование на Android с Dagger 2Тестирование на Android с Dagger 2
Тестирование на Android с Dagger 2
 
Building High Performance and Reliable Windows Phone 8 Apps
Building High Performance and Reliable Windows Phone 8 AppsBuilding High Performance and Reliable Windows Phone 8 Apps
Building High Performance and Reliable Windows Phone 8 Apps
 
Activities
ActivitiesActivities
Activities
 
Selenium Webdriver with data driven framework
Selenium Webdriver with data driven frameworkSelenium Webdriver with data driven framework
Selenium Webdriver with data driven framework
 
Android Architecture - Khoa Tran
Android Architecture -  Khoa TranAndroid Architecture -  Khoa Tran
Android Architecture - Khoa Tran
 
Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능
 
Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1
 
Msql
Msql Msql
Msql
 
spring-tutorial
spring-tutorialspring-tutorial
spring-tutorial
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnit
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
 
PHP Traits
PHP TraitsPHP Traits
PHP Traits
 

Similar to yagdao-0.3.1 JPA guide

10reasons
10reasons10reasons
10reasons
Li Huan
 
Powerful persistence layer with Google Guice & MyBatis
Powerful persistence layer with Google Guice & MyBatisPowerful persistence layer with Google Guice & MyBatis
Powerful persistence layer with Google Guice & MyBatis
simonetripodi
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
Yekmer Simsek
 

Similar to yagdao-0.3.1 JPA guide (20)

Androidaop 170105090257
Androidaop 170105090257Androidaop 170105090257
Androidaop 170105090257
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 
Struts2 - 101
Struts2 - 101Struts2 - 101
Struts2 - 101
 
What's Coming in Spring 3.0
What's Coming in Spring 3.0What's Coming in Spring 3.0
What's Coming in Spring 3.0
 
Struts2 notes
Struts2 notesStruts2 notes
Struts2 notes
 
We sport architecture_implementation
We sport architecture_implementationWe sport architecture_implementation
We sport architecture_implementation
 
Migration to Extent Report 4
Migration to Extent Report 4Migration to Extent Report 4
Migration to Extent Report 4
 
Log4j2
Log4j2Log4j2
Log4j2
 
10reasons
10reasons10reasons
10reasons
 
Struts2 in a nutshell
Struts2 in a nutshellStruts2 in a nutshell
Struts2 in a nutshell
 
Dost.jar and fo.jar
Dost.jar and fo.jarDost.jar and fo.jar
Dost.jar and fo.jar
 
Meteor iron:router
Meteor iron:routerMeteor iron:router
Meteor iron:router
 
Powerful persistence layer with Google Guice & MyBatis
Powerful persistence layer with Google Guice & MyBatisPowerful persistence layer with Google Guice & MyBatis
Powerful persistence layer with Google Guice & MyBatis
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Retrofitting
RetrofittingRetrofitting
Retrofitting
 
Jtr Fosdem 09
Jtr Fosdem 09Jtr Fosdem 09
Jtr Fosdem 09
 
Struts2
Struts2Struts2
Struts2
 
Struts 2
Struts 2Struts 2
Struts 2
 
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)Boost Development With Java EE7 On EAP7 (Demitris Andreadis)
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

yagdao-0.3.1 JPA guide

  • 1. Yet Another DAO JPA Guide YAGDAO 0.3.1
  • 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 GroupId ArtifactId Version Optional cglib cglib 2.2 No commons-logging commons-logging 1.0.2 No org.antlr antlr-runtime 3.2 No org.slf4j slf4j-log4j12 1.5.8 No org.apache.geronimo.specs geronimo-jpa_2.0_spec 1.1 JPA org.hibernate hibernate-core 3.5.1-Final hibernate org.hibernate hibernate-entitymanager 3.5.1-Final hibernate org.springframework spring-beans 3.0.4.RELEASE spring support org.springframework spring-jdbc 3.0.4.RELEASE spring support org.springframework spring-orm 3.0.4.RELEASE Spring support
  • 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:jpa id="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 methods create & 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 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  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. 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 •Custom Paging and order is supported •Selected field and fetch are supported @YMethod(type = YMethodType.CRITERIA) SearchResultList<SimpleBean> criteria1(@YParameter(value = "pint>=") Integer arg1);
  • 16. custom methods count @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