yagdao/usage
- Default methods
- Create and Update Support
- Custom Query Support
- Named Query Support
- Criteria Query Support
When you extend GenericDAO, you will get some default methods as below.
public interface GenericDAO<T, ID extends Serializable> {
/**
* save entity to the persistance layer.
* @param entity entity
*/
void save(T entity);
/**
* get with select.
* @param id object id
* @return null if not exists
*/
T get(ID id);
/**
* Load entity from persitance layer. Lazy Load JPA getReference hibernate
* load
* @param id object id
* @return entity
*/
T load(ID id);
/**
* Load entity from persitance layer with additional relational properties.
* @param id object id
* @return entity
*/
T fetch(ID id);
/**
* Fetch All .
* @return list of entity
*/
SearchResultList<T> fetchAll();
/**
* Fetch All .
* @param page paging for result set
* @return list of entity
*/
SearchResultList<T> fetchAll(YPage page);
/**
* list of all entities.
* @return list of entites
*/
SearchResultList<T> getAll();
/**
* Entity class for DAO.
* @return handled object
*/
Class<?> getObjectClass();
/**
* load all with given paging.
* @param paging paging
* @return list of entities
*/
SearchResultList<T> getAll(YPage paging);
/**
* count of all entites.
* @return entity count
*/
long count();
/**
* update entity.
* @param entity entity object
*/
void update(T entity);
/**
* delete entity by id.
* @param id entity id
*/
void deleteById(ID id);
/**
* detach entity from session transaction.
* @param entity entity to be detached
*/
void detach(T entity);
/**
* release entity.
* @param entity entity to be deleted
*/
void delete(T entity);
/**
* returns the id property name.
* @return id field
*/
public String getIdProperty();
}
Create and Update Support
you can create custom Create and Update method which would set relative properties of the entity object.
For update operations first argument is assumed to be the id of the entity.
PS: for moment there is no locking support.
public interface LinkDAO extends GenericDAO<Link, Long> {
@YMethod(type = YMethodType.SAVE)
Link create(@YParameter("href") String href, @YParameter("name") String name, @YParameter("index") int index);
@YMethod(type = YMethodType.UPDATE)
Link update(Long id,@YParameter("href") String href, @YParameter("name") String name, @YParameter("index") int index);
}
Custom Query Support
you can create custom query method to execute defined query. It also supports paging
public interface PostDAO extends GenericDAO<Post, Long> {
@YMethod(type = YMethodType.QUERY,query = "from Post p where year(p.publishDate) =:year ")
SearchResultList listByYear(@YParameter("year") int year,YPage yPage);
}
Named Query Support
@Entity
@NamedQueries({
@NamedQuery( name="posts.byyear" ,query="from Post p where year(p.publishDate) =:year "),
@NamedQuery( name="posts.bymonth" ,query="from Post p where month(p.publishDate)=:month and year(p.publishDate) =:year ")
}
)
public class Post implements Serializable {}
public interface PostDAO extends GenericDAO<Post, Long> {
@YMethod(type = YMethodType.QUERY,queryName = "posts.bymonth")
SearchResultList listByMonth(@YParameter("year") int year,@YParameter("month")int month,YPage yPage);
@YMethod(type = YMethodType.QUERY,queryName = "posts.byyear")
SearchResultList listByYear(@YParameter("year") int year,YPage yPage);
}
Criteria Query Support
this type of query is build at the runtime and parameters are valid only if they are not null. the operator are also can be set by the annotations. Custom paging and ordering is also supported
@YMethod(type = YMethodType.CRITERIA) SearchResultList<SimpleBean> criteria2(@YParameter(value = "pint", operator = YOperator.GE) Integer arg1, @YParameter(value = "pdate", operator = YOperator.LE) Date arg2, YPage page);
Result set Custom Paging,Sorting and Counting
you can customize the result set offset by adding and argument of type YLimit in method definition, supported for both custom queries and criteria queries
public interface PostDAO{
@YMethod(type = YMethodType.QUERY,queryName = "posts.bymonth")
SearchResultList listByMonth(@YParameter("year") int year,@YParameter("month")int month,YLimit yPage);
}
public class BrowseServiceImpl{
public SearchResultList<Post> getPostListByDate(Integer year, Integer month, int page) {
return postDAO.listByMonth(year,month, new YPage(firstIndex,pagesize));
}
}
For criteria Queries you can also customize order of the result set. Simply add and method parameter with YPage
public interface PostDAO{
@YMethod(type = YMethodType.CRITERIA)
SearchResultList search(@YParameter(value="body",operator = YOperator.ULIKE) String keyword,YPage yPage);
}
public class BrowseServiceImpl{
public SearchResultList<Post> search(String keyword) {
return postDAO.search(keyword, new YPage("publishDate",ascending));
}
}
Smart Return Type
- a method with a collection return type would return whole result set from persistence engine
- a method with entityclass would return first instance of the resultset with an expectation of there is only one item in the result set
- a method with SearchResultList would count the resultset size and return an object with all that information.


Gayet basarili, tebrikler. Iyi dusunulmus. DAO lar biraz eski moda olsa da Query kismi icin projeme ekledim. Keep up the good work, fellow marmarian.
@kemal tesekkurler
henuz bitiremedim istedigim gibi ilk firsatta artik