yagdao-0.3.1 jpa guide
DESCRIPTION
yagdao-0.3.1 JPA guide http://www.altuure.com/projects/yagdao/TRANSCRIPT
![Page 1: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/1.jpg)
YAGDAO 0.3.1
Yet Another DAO
JPA Guide
![Page 2: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/2.jpg)
yagdao
http://www.altuure.com/projects/yagdao
Mert Can Akkan
http://www.altuure.com
![Page 3: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/3.jpg)
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
![Page 4: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/4.jpg)
dependency-heaven
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
Lightweight framework with minimal dependency
![Page 5: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/5.jpg)
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>
![Page 6: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/6.jpg)
my first yagdao
easy implementation
public interface UserDAO extends
GenericDAO<User,Long>{
}
![Page 7: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/7.jpg)
quickstart
GenericHibernateDAOFactoryuserDAO = (UserDAO)
GenericHibernateDAOFactory.createInstance(UserDAO.class,
sessionAccessor);
The GenericHibernateDAOFactory will create instance
of given DAO at the runtime
![Page 8: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/8.jpg)
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"/>
![Page 9: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/9.jpg)
GenericDAO
save(Object entity)
update(Object entity)
load(T id)
loadLazy(T id)
delete(Object object)
delete(T id)
vs….
![Page 10: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/10.jpg)
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);
}
![Page 11: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/11.jpg)
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);
![Page 12: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/12.jpg)
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
![Page 13: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/13.jpg)
custom methods
append
@YMethod(type = YMethodType.APPEND,
select = "pbyte,count(id)",
groupBy = "pbyte",
having = "count(id)>10")
List<SimpleBean> appendQuery(
@YParameter("pint>=?") int i);
APPEND Method handler is a simple query builder
in which you can append query strings with not null
parameters
![Page 14: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/14.jpg)
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.
![Page 15: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/15.jpg)
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
![Page 16: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/16.jpg)
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
![Page 17: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/17.jpg)
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 listpublic SearchResultList(List<T> result,
long totalCount, YPage paging) {
super(result);
this.totalCount = totalCount;
this.paging = paging;
}
![Page 18: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/18.jpg)
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
![Page 19: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/19.jpg)
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);
![Page 20: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/20.jpg)
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
![Page 21: yagdao-0.3.1 JPA guide](https://reader033.vdocuments.us/reader033/viewer/2022052410/547c5a07b47959b1508b4637/html5/thumbnails/21.jpg)
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