DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. DAO Layer - Generics to the Rescue

DAO Layer - Generics to the Rescue

Gal Levinsky user avatar by
Gal Levinsky
·
Sep. 29, 12 · Interview
Like (2)
Save
Tweet
Share
40.62K Views

Join the DZone community and get the full member experience.

Join For Free
Generics can be a powerful tool to create reusable code with the power of compile time verification (type safety..).
Unfortunately I feel the main stream developers still afraid of it.
However, in analogy to Hagrid's spiders I would say that Generics are seriously misunderstood creatures... :-)

I hope the following example will demonstrate how useful they can be.

The Problem - DAO (Data Access Objects) classes have common methods such as save, update, delete, loadAll.. which are required in every DAO class.
Writing a base class with these common methods and making every DAO object extend it, is simply not enough since each DAO class represents a different domain class and therefore the type used in the common methods' signature is different (although implementation is similar), for example:
class OrderDAO {
//save method receive an Order
public void save(Order order){....}
//getAll method return Orders List
public List<Order> getAll(){...}
}
 
class UserDAO{
//save method receive an User
public void save(User user){....}
//getAll method return Users List
public List<User> getAll(){...}
}

How Generics can help us create a base class with a common implementation and yet, keep method signature type-safety?
First, we need to define an interface with the common methods:
/**
 * Base interface for CRUD operations and common queries
 */
public interface IDaoBase<T> {
     
    public List<T> loadAll();
     
    public void save(T domain);
         
    public void update(T domain);
         
    public void delete(T domain);
     
    public T get(Serializable id);
     
    /**
     * Get list by criteria
     * @param detachedCriteria the domain query criteria, include condition and the orders.
     * @return
     *
     */
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria);
     
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria, int offset, int size);   
}

Please note that we utilize generics so each method signature has a type T, which in the implemented DAO classes, will be a concrete type, per domain.

The second step is to create an abstract class which implements the common functionality:
public abstract class DaoBase<T> extends HibernateDaoSupport implements IDaoBase<T> {
    private Class<T> entityClass;
     
    @Autowired
    public void setSession(SessionFactory sessionFactory){
        this.setSessionFactory(sessionFactory);
    }
         
    public DaoBase() {
         
        entityClass = (Class<T>) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
    }
 
        public List<T> loadAll(){
        return getHibernateTemplate().loadAll(entityClass);
    }
 
    public void delete(T domain) {
        getHibernateTemplate().delete(domain);
    }
 
    public void save(T domain) {
        getHibernateTemplate().saveOrUpdate(domain);
         
    }
 
    public void update(T domain) {
        getHibernateTemplate().merge(domain);
    }
     
     
        public T get(Serializable id) {
        T o = (T) getHibernateTemplate().get(entityClass, id);
        return o;
    }
 
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria,
            int offset, int size) {
        return getHibernateTemplate().findByCriteria(detachedCriteria, offset, size);
    }
     
    public List<T> getListByCriteria(DetachedCriteria detachedCriteria) {
        return getHibernateTemplate().findByCriteria(detachedCriteria);
    }
}
 
And that's it !
Take a minute or two to inspect how the base object implements a generic functionality with a type-safety manner.

All we have to do when implementing a new DAO is:
1. Interface to extend the IDaoBase with a concrete type
public interface DaoUser extends IDaoBase<User> {//<=Notice the User typing
    //Add any additional custom methods..
    public User getbyUsername(String username);
        public User getbyEmail(String email);
}

2. Implementation to extend the DaoBase with a concrete type
//This class has all the common methods, which are type safe for the User class
@Repository("daoUser")
public class DaoUserImpl extends DaoBase<User> implements DaoUser { //<=Notice the User typing
 
    public User getbyUsername(String username) {
// concrete implmentation       ...
    }
So now you see how powerful it is to use generics. Hope it is now a bit less scary and more understood...

Please post me if you have further cool tricks utilizing generics for ease development.

Moreover, if you think any of the above can be improved, I'll be happy to hear about it. 
 
Decentralized autonomous organization

Published at DZone with permission of Gal Levinsky, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • Top Five Tools for AI-based Test Automation
  • Secrets Management
  • Writing a Modern HTTP(S) Tunnel in Rust

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: