yagdao/quickstart
Although hibernate and JPA are two different APIs after the creating initial setup all runtime behaviors of the yagdao is expected to be same.
- Dependency
- Setup and Download
- Defining GenericDAO interfaces
- Quickstart with hibernate
- Quickstart with JPA
- Quickstart with Hibernate and Spring Framework
- Quickstart with JPA and Spring Framework
- Utilities for JPA and hibernate
- Sample Application
Dependencies
| GroupId | ArtifactId | Version | Optional | |
|---|---|---|---|---|
| cglib | cglib | 2.2 | No | |
| commons-logging | commons-logging | 1.1.1 | No | |
| org.slf4j | slf4j-log4j12 | 1.5.8 | No | |
| org.apache.geronimo.specs | geronimo-jpa_2.0_spec | 1.1 | Yes(for JPA) | |
| org.hibernate | hibernate-core | 3.5.1-Final | Yes(for hibernate) | |
| org.hibernate | hibernate-entitymanager | 3.5.1-Final | Yes (for hibernate) | |
| org.springframework | spring-beans | 3.0.4.RELEASE | Yes (for springframework support) | |
| org.springframework | spring-jdbc | 3.0.4.RELEASE | Yes (for springframework support) | |
| org.springframework | spring-orm | 3.0.4.RELEASE | Yes (for springframework support) |
Downloads : http://code.google.com/p/yagdao/
Source Code repository: http://yagdao.googlecode.com/svn/trunk/
maven setup
<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>
Defining Generic DAO
assuming you SimpleBean is an entity class (or pick any of your entity class and) create an implementation like below. Then follow the instructions as your application dependencies.
public interface SimpleBeanDAO extends GenericDAO<SimpleBean, Long> {}
Quickstart with hibernate
Creating a proxy instance for that generic DAO. If you use “sessionFactory.getCurrentSession()” then you can use DefaultHibernateSessionAccessor or you can implement your own SessionAccessor
sessionAccessor = new DefaultHibernateSessionAccessor(sessionFactory); simpleBeanDAO = (SimpleBeanDAO) GenericHibernateDAOFactory.createInstance(SimpleBeanDAO.class, sessionAccessor);
Quickstart with JPA
sessionAccessor =null;// TODO implement yours simpleBeanDAO = (SimpleBeanDAO) GenericJpaDAOFactory.createInstance(SimpleBeanDAO.class, accessor);
Quickstart with hibernate with Springframework support
Defining yagdao namespace in the spring context. Afterwards assuming that you have a valid SessionFactory with id mySessionFactory
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:yagdao="http://www.altuure.com/projects/yagdao" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.altuure.com/projects/yagdao http://www.altuure.com/projects/yagdao-1.0.xsd"> </beans>
Then you can apply one of three different configuration options.
defining each GenericDAO one by one
<yagdao:hibernate base-class="com.altuure.yagdao.dao.SimpleBeanDAO" session-factory="mySessionFactory" /> <yagdao:hibernate base-class="com.altuure.yagdao.dao.OrderDAO" session-factory="mySessionFactory" /> <yagdao:hibernate base-class="com.altuure.yagdao.dao.OrderItemDAO" session-factory="mySessionFactory" /> <yagdao:hibernate base-class="com.altuure.yagdao.dao.ProductDAO" session-factory="mySessionFactory"/>
Defining each genericDAO in a single tag
<yagdao:hibernate session-factory="mySessionFactory">
<yagdao:dao base-class="com.altuure.yagdao.dao.SimpleBeanDAO" />
<yagdao:dao base-class="com.altuure.yagdao.dao.OrderDAO" />
<yagdao:dao base-class="com.altuure.yagdao.dao.ProductDAO" />
<yagdao:dao base-class="com.altuure.yagdao.dao.OrderItemDAO" />
</yagdao:hibernate>
Auto Detection
<yagdao:hibernate id="DAOFactory1" base-package="com.altuure.yagdao.dao" session-factory="mySessionFactory" />
Quickstart with JPA with Springframework support
Defining yagdao namespace in the spring context. Afterwards assuming that you have a valid Entity Manager Factory with id entityManagerFactory
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:yagdao="http://www.altuure.com/projects/yagdao" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.altuure.com/projects/yagdao http://www.altuure.com/projects/yagdao-1.0.xsd"> </beans>
Then you can apply one of three configuration options
defining each GenericDAO one by one
<yagdao:jpa
base-class="com.altuure.yagdao.dao.SimpleBeanDAO"
entity-manager-factory="entityManagerFactory"/>
<yagdao:jpa
base-class="com.altuure.yagdao.dao.OrderDAO"
entity-manager-factory="entityManagerFactory"/>
<yagdao:jpa
base-class="com.altuure.yagdao.dao.OrderItemDAO"
entity-manager-factory="entityManagerFactory"/>
<yagdao:jpa
base-class="com.altuure.yagdao.dao.ProductDAO"
entity-manager-factory="entityManagerFactory"/>
Defining each genericDAO in a single tag
<yagdao:jpa
entity-manager-factory="entityManagerFactory">
<yagdao:dao base-class="com.altuure.yagdao.dao.SimpleBeanDAO" />
<yagdao:dao base-class="com.altuure.yagdao.dao.OrderDAO" />
<yagdao:dao base-class="com.altuure.yagdao.dao.ProductDAO" />
<yagdao:dao base-class="com.altuure.yagdao.dao.OrderItemDAO" />
</yagdao:jpa>
Auto Detection
<yagdao:jpa id="DAOFactory1"
base-package="com.altuure.yagdao.dao"
entity-manager-factory="entityManagerFactory"/>
Utilities
Here you can use this two interface to enable the yagdao access your own active session.
public interface HibernateSessionAccessor {
Session getCurrentSession();
SessionFactory getSessionFactory();
}
public interface EntityManagerAccessor {
public EntityManager getEntityManager();
}
for Custom method for Queries and CRUD operation
Sample Application
download the sample application and you can see most of the features.
mvn jetty:run
and click on
http://localhost:8080/com.altuure.yablog/


No comments yet.