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

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

    • Bram
    • November 3rd, 2010

    Holy hell that code is ugly. Fix the formatting, kill the annotation overload and then I’ll have another look at it.

    • Chris W
    • November 3rd, 2010

    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.

  1. @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.

    • Chris W
    • November 3rd, 2010

    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);
    }

  1. No trackbacks yet.