0

I want to realise a java class, based on generic interface. I got error class GroupDaoImpl must be declared as abstract or implement abstract method update()

What i do wrong?

My interface

public interface EntitiesDao<T> {
    void add(List<?> entity);
    List<T> getList();
    void update(T entity);
    T findById(Long entityId);
    void delete(Long entityId);
}

My class

@Repository
public class GroupDaoImpl implements EntitiesDao {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void add(Group group) {
        em.persist(group);
    }

    @Override
    public List<Group> getGroupsList() {
        CriteriaQuery<Group> criteriaQuery = em.getCriteriaBuilder().createQuery(Group.class);
        Root<Group> root = criteriaQuery.from(Group.class);
        return em.createQuery(criteriaQuery).getResultList();
    }

    @Override
    public void update(Group group) {
        em.merge(group);
    }

    @Override
    public Group findById(Long groupId) {
        Group group = em.find(Group.class, groupId);
        if (group == null) {
            throw new EntityNotFoundException("Группа с ID = " + groupId + " не найдена");
        }
        return group;
    }

    //удаляет сущность по id
    @Override
    public void delete(Long groupId) {
        Group group = em.find(Group.class, groupId);
        if (group != null) em.remove(group);
        else throw new EntityNotFoundException("Группа с ID = " + groupId + " не найдена");
    }

    public void addStudent(Student student){}

}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Vytsalo
  • 670
  • 3
  • 9
  • 19
  • 3
    `implements EntitiesDao` ? – Michał Krzywański Oct 23 '19 at 08:47
  • 3
    Make it `class GroupDaoImpl implements EntitiesDao` and implement all the methods from the interface. – Jesper Oct 23 '19 at 08:47
  • I got the same error with method add. I should change only class signature? – Vytsalo Oct 23 '19 at 08:51
  • Since you are using wildcard - method from this interface will be hardly useful. Why not make `add` method in your interface at least `void add(List entity);`? Also you are passing a single instance of `Group` to your `add` method - not a `List`. – Michał Krzywański Oct 23 '19 at 08:56
  • the add method from `EntitiesDao` uses a `List` so you should implement your interface correctly and add the `implements EnitiesDao` in your class declaration – octopus Oct 23 '19 at 10:47

1 Answers1

0

How your interface is using generic, you must implement using the wildcards:

public class GroupDaoImpl implements EntitiesDao<Group> {}

And for the method add you must implements as declared in the interface, you are declaring as a List so you must implement as a List:

void add(List<?> entity);

Implementation:

@Override
public void add(List<Group> group) {

}
Allan Braga
  • 460
  • 5
  • 19