2

I want UserDao class to extend GenericDAO where i'll have all CRUD methods. I have read article from IBM: http://www.ibm.com/developerworks/java/library/j-genericdao/index.html , but i could not implement it. Could someone show me example based on my custom UserDao class.

@Transactional(value="myTransactionManager")
public class UserDao {

    @Qualifier("mySessionFactory")
    public SessionFactoryImpl sessionFactory;

    public void setSessionFactory(SessionFactoryImpl sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public List<UserEntity> getAll() {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "from UserEntity ");
        List<UserEntity> userList = query.list();
        return userList;
    }

    public void updaet(UserEntity userEntity) {
        sessionFactory.getCurrentSession().update(userEntity);
    }

    public void delete(UserEntity userEntity) {
        sessionFactory.getCurrentSession().delete(userEntity);
    }

    public void save(UserEntity userEntity) {
        sessionFactory.getCurrentSession().save(userEntity);
    }

}

i tried to write class like this

public class GenericDao{

    @Qualifier("mySessionFactory")
    public SessionFactoryImpl sessionFactory;

    public void setSessionFactory(SessionFactoryImpl sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public <T> List<T> getAll(Class<T> t) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "from " + t.getName());
        List<T> list = query.list();
        return list;
    }

    public <T> void save(T t) {
        sessionFactory.getCurrentSession().save(t);
    }

    public <T> void update(T t) {
        sessionFactory.getCurrentSession().update(t);
    }

    public <T> void delete(T t) {
        sessionFactory.getCurrentSession().delete(t);
    }
}

but when i try to pull data with UserDao like this:

public List<UserEntity> getAll() {
    List<UserEntity> list = UserDao.findAll();
}

Eclipse IDE for line List<UserEntity> list = UserDao.findAll(); give error : The method findAll() is underfined for type UserDao.

4
  • You need to pass the type to GenericDao, GenericDao<T> and extends it, obviously... Commented Apr 13, 2016 at 7:53
  • UserDaodoes not extend GenericDaoYou not add the generics to the class definition. GenericDao <T, PK extends Serializable> Commented Apr 13, 2016 at 7:55
  • UserDao does not extend GenericDao in description. But when i extend it gives error as described. Commented Apr 13, 2016 at 8:57
  • I was implemented as per your requirement : github.com/srinivas1918/spring4-and-hibernate4/blob/master/src/… Commented Apr 13, 2016 at 12:13

1 Answer 1

1

this is my implementation :

GenericDao :

@Repository
public class GenericDao<T extends DbObject> {

    @Autowired
    private SessionFactory sessionFactory;

    private Class<T> getParameterizedClass() {
        return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public T findById(final Serializable id) {
        return (T) getCurrentSession().get(getParameterizedClass(), id.toString());
    }

    public void persist(final T object) {
        getCurrentSession().persist(object);
    }

    public void saveOrUpdate(final T object) {
        getCurrentSession().saveOrUpdate(object);
    }

    public void delete(final T object) {
        getCurrentSession().delete(object);
    }

    public T merge(final T object) {
        return (T) getCurrentSession().merge(object);
    }

}

UserDao :

public class UserDao extends GenericDao<User> {

}

Entity :

@Entity
@Table(name = "...")    
public class User extends DbObject {
}
Sign up to request clarification or add additional context in comments.

1 Comment

what this method for: private Class<T> getParameterizedClass() { return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.