Yet Another Generic DAO:yagdao
yagdao is a Generic DAO (Data Access Objects) implementation for both JPA 2 and hibernate, that enables user to support custom CRUD operations and custom query operations without coding at all.
Features
- very light
- easy to setup
- support for JPA
- support for hibernate
- support for spring
- dynamic criteria queries.
- named query support
- custom query support for both named and indexed parameters
- enables both custom ordering and paging
- no code generation
Sample DAO
Your entity
@Entity
@NamedQueries({
@NamedQuery(name = "namedQuery1", query = "select s from SimpleBean s where s.pint>:arg1"),
@NamedQuery(name = "namedQuery2", query = "select s from SimpleBean s where s.pbyte>:arg1") })
public class SimpleBean implements Serializable {
@Id
@GeneratedValue
public long id;
public String pstring;
public double pdouble;
public int pint;
public Date pdate;
public byte pbyte;
private double pint2;
}
Your Generic DAO
public interface SimpleBeanDAO extends GenericDAO<SimpleBean, Long> {
/**
* Sample save method with two properties
*/
@YMethod(type = YMethodType.SAVE)
SimpleBean customSave1(@YParameter("pstring") String p1, @YParameter("pdouble") double p2);
@YMethod(type = YMethodType.UPDATE)
SimpleBean customUpdate1(long id, @YParameter("pstring") String p1, @YParameter("pdouble") double p2);
@YMethod(type = YMethodType.QUERY, query = "select s from SimpleBean s where s.pint>:minint")
List<SimpleBean> customQuery2(@YParameter("minint") int arg1);
@YMethod(type = YMethodType.CRITERIA)
SearchResultList<SimpleBean> criteria1(@YParameter(value = "pint", operator = YOperator.GE) Integer arg1, YPage page);
@YMethod(type = YMethodType.COUNT)
long count1(@YParameter(value = "pint", operator = YOperator.GE) Integer arg1);
}
and create Proxy classes with JPA or Hibernate
//FOR JPA simpleBeanDAO = (SimpleBeanDAO) GenericJpaDAOFactory.createInstance(SimpleBeanDAO.class, new DefaultEntityManagerAccessor(entityManagerFactory)); //or FOR HIBERNATE simpleBeanDAO = (SimpleBeanDAO) GenericHibernateDAOFactory.createInstance(SimpleBeanDAO.class, new DefaultHibernateSessionAccessor(sessionFactory));
More
For detailed setup instructions read quickstart
for more annotations and methods read usage


Holy hell that code is ugly. Fix the formatting, kill the annotation overload and then I’ll have another look at it.
What’s the point? I don’t see what this buys us. The code is a lot uglier and more complex than using the standard Hibernate/JPA APIs.
@Brim @Chris
Hi
I know it is not the best looking code or looks like anything standard but the total line of java code is actually 0 ZERO that makes it easy to use, implement and maintain.
I think this qualifies as annotation abuse. You claim “no Java code” because you pile all the query logic into the annotations. It’s just a sleigh of hand achieved by reclassifying what we call “code”. Again, I don’t see the point. It’s not very readable (easy to use?), and I don’t see how it can be easier to implement or maintain compared to a conventional Dao approach.
Your clever method:
@YMethod(type = YMethodType.QUERY, query = “select s from SimpleBean s where s.pint>:minint”)
List customQuery2(@YParameter(“minint”) int arg1);
Use a conventional DAO:
List customQuery2(int arg) {
return dao.query(“select s from SimpleBean s where s.pint>:minint”, arg);
}
but try to add custom sorting and paging to this code and a one more optional query parameter
yagdao result would be like
@YMethod(type = YMethodType.CRITERIA)
List customQuery2(@YParameter(“minint”) int arg1,@YParameter(“minint2”) int arg2,YPage page);
this project is extremely helpful if you are bored all this DAO stuff like me. they are nothing but code duplication,
try sample application or sample test cases.
http://code.google.com/p/yagdao/source/browse/tags/com.altuure.yagdao-0.2/src/test/java/com/altuure/yagdao/dao/SimpleBeanDAO.java
http://code.google.com/p/yagdao/source/browse/tags/com.altuure.yagdao-0.2/src/test/java/com/altuure/yagdao/base/SimpleDAOCriteriaTest.java
http://code.google.com/p/yagdao/
cheers