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

Code transformation With Spoon
Code transformation With SpoonCode transformation With Spoon
Code transformation With SpoonGérard Paligot
 
Architecture Components
Architecture ComponentsArchitecture Components
Architecture ComponentsSang Eel Kim
 
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 181Mahmoud Samir Fayed
 
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 1Kirill Rozov
 
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
 
Web scraping using scrapy - zekeLabs
Web scraping using scrapy - zekeLabsWeb scraping using scrapy - zekeLabs
Web scraping using scrapy - zekeLabszekeLabs Technologies
 
Тестирование на Android с Dagger 2
Тестирование на Android с Dagger 2Тестирование на Android с Dagger 2
Тестирование на Android с Dagger 2Kirill Rozov
 
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 AppsMichele Capra
 
Selenium Webdriver with data driven framework
Selenium Webdriver with data driven frameworkSelenium Webdriver with data driven framework
Selenium Webdriver with data driven frameworkDavid Rajah Selvaraj
 
Android Architecture - Khoa Tran
Android Architecture -  Khoa TranAndroid Architecture -  Khoa Tran
Android Architecture - Khoa TranTu Le Dinh
 
Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능Jdk(java) 7 - 6 기타기능
Jdk(java) 7 - 6 기타기능knight1128
 
Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Zianed Hou
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitPeter Wilcsinszky
 
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.Kuldeep Jain
 
PHP Traits
PHP TraitsPHP Traits
PHP Traitsmattbuzz
 

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

Androidaop 170105090257
Androidaop 170105090257Androidaop 170105090257
Androidaop 170105090257newegg
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2wiradikusuma
 
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.0Matt Raible
 
We sport architecture_implementation
We sport architecture_implementationWe sport architecture_implementation
We sport architecture_implementationaurelianaur
 
Migration to Extent Report 4
Migration to Extent Report 4Migration to Extent Report 4
Migration to Extent Report 4RapidValue
 
10reasons
10reasons10reasons
10reasonsLi 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 & MyBatissimonetripodi
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
Jtr Fosdem 09
Jtr Fosdem 09Jtr Fosdem 09
Jtr Fosdem 09fruxo
 
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)Red Hat Developers
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best PracticesYekmer 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

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging 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 MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
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 MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 

Recently uploaded (20)

Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 

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