我有一个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;
}
}
最佳答案
由于您的BaseDao
类是用通用的<T>
定义的,因此应使用T
而不是User
定义覆盖的方法。如果将@Override
批注添加到这些方法中,您将看到它们实际上并不覆盖接口的方法,而是它们自己的方法。
关于java - 方法的具体实现被子类忽略?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40895844/