Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 子类忽略方法的具体实现?_Java_Interface_Abstract Class - Fatal编程技术网

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

Java 子类忽略方法的具体实现?,java,interface,abstract-class,Java,Interface,Abstract Class,我有一个InboundMessageDao类,它扩展了BaseDao类,实现了BaseDaoInterface类 BaseDao类是抽象的,提供了具体和抽象的成员方法。 出于某种原因,Eclipse声称我没有从BaseDao接口实现persist、update和delete,尽管BaseDao中提供了它们的具体实现 奇怪的是,我有另一个子类UserDao,它做同样的事情,但是对于未实现的方法没有错误 BaseDao.java package com.MYPKG.data.dao; import

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

BaseDao类是抽象的,提供了具体和抽象的成员方法。 出于某种原因,Eclipse声称我没有从BaseDao接口实现persist、update和delete,尽管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);
    }
}
package com.MYPKG.data.dao;
导入java.text.MessageFormat;
导入java.util.List;
导入org.hibernate.Session;
导入org.hibernate.Transaction;
导入org.springframework.core.GenericTypeResolver;
导入com.MYPKG.model.User;
公共抽象类BaseDao实现BaseDao接口{
非公开会议;
私人交易;
公共数据库(){
}
公共会话openCurrentSession(){
currentSession=HibernateUtil.getSession();
返回当前会话;
}
公开会话openCurrentSessionwithTransaction(){
currentSession=HibernateUtil.getSession();
currentTransaction=currentSession.beginTransaction();
返回当前会话;
}
public void closeCurrentSession(){
currentSession.close();
}
public void closeCurrentSessionwithTransaction(){
currentTransaction.commit();
currentSession.close();
}
公共会话getCurrentSession(){
返回当前会话;
}
公共无效setCurrentSession(会话currentSession){
this.currentSession=currentSession;
}
公共事务getCurrentTransaction(){
返回当前事务;
}
公共作废setCurrentTransaction(事务处理currentTransaction){
this.currentTransaction=currentTransaction;
}
public void persist(用户实体){
getCurrentSession().save(实体);
}
公共无效更新(用户实体){
getCurrentSession().update(实体);
}
公共摘要T findById(字符串id);
公共作废删除(用户实体){
getCurrentSession().delete(实体);
}
公共摘要列表findAll();
public void deleteAll(){
List objects=findAll();
对于(T对象:对象)
getCurrentSession().delete(对象);
}
}
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();

}
package com.MYPKG.data.dao;
导入java.io.Serializable;
导入java.util.List;
公共接口BaseDaoInterface{
公共部门(T实体);
公共无效更新(T实体);
公共T findById(Id);
公共无效删除(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;
    }

}
package com.MYPKG.data.dao;
导入java.text.MessageFormat;
导入java.util.List;
导入com.MYPKG.model.InboundMessage;
导入com.MYPKG.model.User;
公共类InboundMessageDao扩展了BaseDao{
/////问题
/////InboundMessageDao类型必须实现继承的抽象方法BaseDaoInterface.update(InboundMessage)
/////
公共InboundMessageDao(){
}
@凌驾
公共InboundMessage findById(字符串id){
InboundMessage对象=(InboundMessage)getCurrentSession().get(InboundMessage.class,id);
返回对象;
}
@凌驾
公共列表findAll(){
@抑制警告(“未选中”)
List objects=(List)getCurrentSession().createQuery(MessageFormat.format(“from{0}”,“inbound_message”)).List();
归还物品;
}
}
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;
    }


}
package com.MYPKG.data.dao;
导入java.text.MessageFormat;
导入java.util.List;
导入org.hibernate.Session;
导入org.hibernate.Transaction;
导入com.MYPKG.model.User;
公共类UserDao扩展了BaseDao{;
公共用户dao(){
}
公共列表findAll(){
@抑制警告(“未选中”)
List objects=(List)getCurrentSession().createQuery(MessageFormat.format(“from{0}”,“user”)).List();
归还物品;
}
公共用户findById(字符串id){
用户对象=(用户)getCurrentSession().get(用户类,id);
返回对象;
}
}

因为您的
BaseDao
类是用泛型
定义的,所以您应该使用
T
而不是
User
来定义重写的方法。如果将
@Override
注释添加到这些方法中,您将看到它们实际上不会覆盖接口的方法,相反,它们有自己的方法。

什么是
InboundMessage
?@PritamBanerjee:POJO代表entity@jonhopkins:哇,我简直不敢相信我用用户类而不是泛型类型标记了这些。。你能把它贴出来作为答案吗?哈哈,我实际上是把它写下来作为答案的,但我觉得它看起来太短了。。