Create a gist now

Instantly share code, notes, and snippets.

Generic DAO using Java generics! :D
* Establishes the DAO pattern for use with other classes.
* @author Adil <>
public class GenericDao<T> {
* 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) {
public void delete(T item) {
public Class<? extends Object> getType(T item) {
return item.getClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment