Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two classes with almost duplicate code inside

At this moment, I have two classes: UserHibernateDao and TicketHibernateDao:

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import model.dao.Dao;
import model.entity.User;

public class UserDaoHibernate extends HibernateDaoSupport implements Dao<User> {

    public User get(long id) {
        return getHibernateTemplate().get(User.class, id);
    }

    public void save(User user) {
        getHibernateTemplate().save(user);

    }

    public void remove(long id) {
        getHibernateTemplate().delete(
                getHibernateTemplate().get(User.class, id));
    }
}

and second:

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import model.dao.Dao;
import model.entity.Ticket;

public class TicketDaoHibernate extends HibernateDaoSupport implements
        Dao<Ticket> {

    public Ticket get(long id) {

        return getHibernateTemplate().get(Ticket.class, id);

    }

    public void save(Ticket ticket) {
        getHibernateTemplate().save(ticket);
    }

    public void remove(long id) {

        getHibernateTemplate().delete(
                getHibernateTemplate().get(Ticket.class, id));

    }

    public void save(Ticket ticket) {

        getHibernateTemplate().saveOrUpdate(ticket);

    }
}

I see that both classes contain almost equal methods and method implementations.

I think, it is not very good. What do you think? Or it is normal?

like image 743
user471011 Avatar asked Dec 28 '22 07:12

user471011


1 Answers

Something like this should work for you, it's generally called the "Generic DAO Pattern".

public abstract class BaseDaoHibernate<T> extends HibernateDaoSupport implements Dao<T> {

    private final Class<T> entityType;

    protected BaseDaoHibernate(Class<T> entityType) {
        this.entityType = entityType;
    }

    public T get(long id) {
        return getHibernateTemplate().get(entityType, id);
    }

    public void save(T entity) {
        getHibernateTemplate().save(entity);
    }

    public void remove(long id) {
        getHibernateTemplate().delete(get(id));
    }
}

and then:

public class TicketDaoHibernate extends BaseDaoHibernate<Ticket> {
    public TicketDaoHibernate() {
        super(Ticket.class);
    }
}

and

public class UserDaoHibernate extends BaseDaoHibernate<User> {
    public UserDaoHibernate() {
        super(User.class);
    }
}
like image 75
skaffman Avatar answered Jan 06 '23 08:01

skaffman