子类忽略方法的具体实现?

时间:2016-11-30 18:41:51

标签: java interface abstract-class

我有一个InboundMessageDao类,它扩展了BaseDao类,它实现了BaseDaoInterface类。

BaseDao类是抽象的,提供具体和抽象的成员方法。 出于某种原因,Eclipse声称我没有从BaseDaoInterface实现持久化,更新和删除,即使BaseDao中提供了它们的具体实现。

更奇怪的是,我有另一个子类,UserDao执行相同的操作,但没有关于未实现方法的错误。

BaseDao.java

package com.MYPKG.data.dao;

import java.text.MessageFormat;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.core.GenericTypeResolver;

import com.MYPKG.model.User;

public abstract class BaseDao <T> implements BaseDaoInterface<T, String> {
    private Session currentSession;
    private Transaction currentTransaction;

    public BaseDao() {
    }

    public Session openCurrentSession() {
        currentSession = HibernateUtil.getSession();
        return currentSession;
    }

    public Session openCurrentSessionwithTransaction() {
        currentSession = HibernateUtil.getSession();
        currentTransaction = currentSession.beginTransaction();
        return currentSession;
    }

    public void closeCurrentSession() {
        currentSession.close();
    }

    public void closeCurrentSessionwithTransaction() {
        currentTransaction.commit();
        currentSession.close();
    }
    public Session getCurrentSession() {
        return currentSession;
    }

    public void setCurrentSession(Session currentSession) {
        this.currentSession = currentSession;

    }

    public Transaction getCurrentTransaction() {
        return currentTransaction;
    }

    public void setCurrentTransaction(Transaction currentTransaction) {
        this.currentTransaction = currentTransaction;
    }


    public void persist(User entity) {
         getCurrentSession().save(entity);
    }

    public void update(User entity) {
        getCurrentSession().update(entity);
    }

    public abstract T findById(String id) ;

    public void delete(User entity) {
        getCurrentSession().delete(entity);
    }

    public abstract List<T> findAll() ;

    public void deleteAll() {
        List<T> objects = findAll();
        for (T object : objects)
            getCurrentSession().delete(object);
    }
}

BaseDaoInterface.java

package com.MYPKG.data.dao;

import java.io.Serializable;
import java.util.List;

public interface BaseDaoInterface <T, Id extends Serializable>{

    public void persist(T entity);

    public void update(T entity);

    public T findById(Id id);

    public void delete(T entity);

    public List<T> findAll();

    public void deleteAll();

}

InboundMessageDao.java

package com.MYPKG.data.dao;

import java.text.MessageFormat;
import java.util.List;

import com.MYPKG.model.InboundMessage;
import com.MYPKG.model.User;

public class InboundMessageDao extends BaseDao<InboundMessage>{

  ///// PROBLEM
  ///// The type InboundMessageDao must implement the inherited abstract method BaseDaoInterface<InboundMessage,String>.update(InboundMessage)
  /////

    public InboundMessageDao() {

    }

    @Override
    public InboundMessage findById(String id) {
        InboundMessage object = (InboundMessage) getCurrentSession().get(InboundMessage.class, id);
        return object;
    }

    @Override
    public List<InboundMessage> findAll() {
        @SuppressWarnings("unchecked")
        List<InboundMessage> objects = (List<InboundMessage>) getCurrentSession().createQuery(MessageFormat.format("from {0}",  "inbound_message")).list();
        return objects;
    }

}

UserDao.java

package com.MYPKG.data.dao;

import java.text.MessageFormat;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.MYPKG.model.User;

public class UserDao extends BaseDao<User>{;

    public UserDao() {
    }

    public List<User> findAll() {
        @SuppressWarnings("unchecked")
        List<User> objects = (List<User>)getCurrentSession().createQuery(MessageFormat.format("from {0}",  "user")).list();
        return objects;
    }

    public User findById(String id) {
        User object = (User) getCurrentSession().get(User.class, id);
        return object;
    }


}

1 个答案:

答案 0 :(得分:2)

由于您的BaseDao类是使用通用<T>定义的,因此您应使用T而不是User来定义已覆盖的方法。如果将@Override注释添加到这些方法中,您将看到它们实际上不会覆盖接口的方法,而是它们自己的方法。