public
Created — forked from adilhz/GenericDao.java

Generic DAO using Java generics! :D

  • Download Gist
GenericDao.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/**
* Establishes the DAO pattern for use with other classes.
* @author Adil <adil.h@me.com>
*
*/
public class GenericDao<T> {
 
 
@SuppressWarnings("unchecked")
/**
* Load an item by its ID (primary key).
* @param item Type of item
* @param id The id of the item
* @return The item
*/
public T loadById(T item, Integer id) {
// typecheck
if(item instanceof Class<?>) {
return (T)Shark.getDataStore().openSession().load(getType(item),id);
}
return null;
}
public void saveOrUpdate(T item) {
Shark.getDataStore().openSession().saveOrUpdate(item);
}
public void delete(T item) {
Shark.getDataStore().openSession().delete(item);
}
public Class<? extends Object> getType(T item) {
return item.getClass();
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.