Jason Lee Jason Lee - 9 days ago 5
Java Question

Concrete implementation of method being ignored by subclass?

I have an InboundMessageDao class which extends the BaseDao class, which implements the BaseDaoInterface class.

The BaseDao class is abstract, providing concrete and abstract member methods.
For some reason, Eclipse is claming that I did not implement persist, update, and delete from the BaseDaoInterface, even though a concrete implementation of them are provided in BaseDao.

The stranger thing is that I have another subclass, UserDao which does the same thing, but there is no error about unimplemented methods.

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;
}


}

Answer

Since your BaseDao class is defined with the generic <T>, you should define your overridden methods using T instead of User. If you add the @Override annotation to these methods, you will see that they in fact do not override the interface's methods, but instead are their own methods.