3

I am working with Spring 3.2 and Hibernate 4. I am writing the persistence layer access with dao. Here is the dao interface code :

public interface AbstractDao<E, I extends Serializable> {

    public E findById(I id);
    public void saveOrUpdate(E e);
    public void delete(E e);
    public List<E> findByCriteria(Criterion criterion);
}

Just after I have written following the abstract class which implements the interface :

public abstract class AbstractDaoImpl<E, I extends Serializable> implements AbstractDao<E,I> {

        private Class<E> entityClass;

        protected AbstractDaoImpl(Class<E> entityClass) {
            this.entityClass = entityClass;
        }

        @Autowired
        private SessionFactory sessionFactory;

        public Session getCurrentSession() {
            return sessionFactory.getCurrentSession();
        }

        @Override
        public E findById(I id) {
            return (E) getCurrentSession().get(entityClass, id);
        }

        @Override
        public void saveOrUpdate(E e) {
            getCurrentSession().saveOrUpdate(e);
        }

        @Override
        public void delete(E e) {
            getCurrentSession().delete(e);
        }

        @Override
        public List<E> findByCriteria(Criterion criterion) {
            Criteria criteria = getCurrentSession().createCriteria(entityClass);
            criteria.add(criterion);
            return criteria.list();
        }
}

I am getting some compilation errors on all methods that i have implemented in the abstract class : findById(I id), saveOrUpdate(E e), delete(E e) ,findByCriteria(Criterion criterion).

The error message is :

Multiple markers at this line
- implements com.startup.app.models.hibernate.dao.AbstractDao.findById
- The method findById(I) of type AbstractDaoImpl must override a superclass method

What wrong, I don't understand. thanks. enter image description here

6
  • It compiles fine for me. Feb 8, 2014 at 22:23
  • Did you remove override annotation ?
    – Pracede
    Feb 8, 2014 at 22:29
  • I copy-pasted your code exactly. Feb 8, 2014 at 22:31
  • 2
    You're probably using Java 5 to compile this code, where @Override couldn't be used to indicate an interface method implementation. Use Java 7 or at least 6. Java 5 is completely obsolete.
    – JB Nizet
    Feb 8, 2014 at 22:36
  • 1
    FWIW, save yourself the hassle and look into Spring Data JPA. Feb 9, 2014 at 1:05

1 Answer 1

1

Make sure that you are using atleast Java 1.6 inorder to use @Override in this case.

In Java 1.5, @Override could only be applied to methods overriding a superclass method.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.