-
-
Save Jinz0/4075390 to your computer and use it in GitHub Desktop.
Generic DAO using Java generics! :D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment