oddly
oddly

Reputation: 260

Eclipse dao component implement error

I have been working on spring+hibernate+mysql integration recently.For that, I try to build a basic program that adds, edits, deletes and searches students.I have firstly created model class and added necessary JPA annotations:

package com.joseph.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Student {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO) // for autonumber
private int studentId;
@Column
private String firstname;
@Column
private String lastname;
@Column
private int yearLevel;

public Student(){}

public Student(int studentId, String firstname, String lastname,
        int yearLevel) {
    super();
    this.studentId = studentId;
    this.firstname = firstname;
    this.lastname = lastname;
    this.yearLevel = yearLevel;
}
  /* Getters and Setters */

}

Then I constructed DAO interface:

package com.joseph.dao;

import java.util.List;

import com.joseph.model.Student;

public interface StudentDao {
  public void add(Student student);
  public void edit(Student student);
  public void delete(int studentId);
  public Student getStudent(int studentId);
  public List getAllStudent();
}

To get the necessary data from SQL, I implemented the dao interface as follows:

package com.joseph.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.joseph.dao.StudentDao;
import com.joseph.model.Student;

@Repository
public class StudentDaoImpl implements StudentDao {
  @Autowired
  private SessionFactory session;

  @Override
  public void add(Student student) {
    session.getCurrentSession().save(student);
  }

  @Override
  public void edit(Student student) {
    session.getCurrentSession().update(student);
  }

  @Override
  public void delete(int studentId) {
    session.getCurrentSession().delete(getStudent(studentId));
  }

  @Override
  public Student getStudent(int studentId) {
    return (Student)session.getCurrentSession().get(Student.class,    studentId);
  }

  @Override
  public List getAllStudent() {
    return session.getCurrentSession().createQuery("from   Student").list();
  }

}

But I have error at add(Student student) method at StudentDaoImpl.java class that says:

Multiple markers at this line - The method add(Student) of type StudentDaoImpl must override or implement a supertype method - implements com.joseph.dao.StudentDao.add

This error is similar for all other methods in the same class. How can I fix this?

Upvotes: 1

Views: 641

Answers (2)

Beri
Beri

Reputation: 11620

Your code is 100% working, under my test enviroment (InteliJ, JDK7+), so it is connected with your IDE purely.

Try to restart your IDE or reimport this project.

I could share also some tips:

  • use Long instead of int, when creating model id's
  • you don't need to add @Column annotation next to @Id, @Id will do alone
  • inside your parametrized constructor (Student), you don't need to add super(), compiler will add it automatically, seconsly your upper class is Object, so there is no need for that line :)
  • using such approach would couse lots of code duplication when you will be using many dao, you could use inheritance here, like so:

Here is example of generic DAO class:

public class GenericDaoImpl <D>{

  protected SessionFactory session;
  protected Class clazz;

  public GenericDaoImpl(SessionFactory session, Class clazz){
    this.session = session;
    this.clazz = clazz;
  }

  @Override
  public void add(D entity) {
    session.getCurrentSession().save(entity);
  }

  @Override
  public void update(D entity) {
    session.getCurrentSession().update(entity);
  }

  @Override
  public void remove(Long id) {
    session.getCurrentSession().delete(get(id));
  }

  @Override
  public Student get(Long id) {
    return (Student)session.getCurrentSession().get(clazz, id);
  }

  @Override
  public List getAll() {
    return session.getCurrentSession().createQuery("from  " + clazz.getSimpleName()).list();
  }
}

To create StudentDao, simply do so:

public interface StudentDao{
   void add(Student student);
   void update(Student student);
   void remove(int id);
   Student get(int id);
   List getAll();
}

And finally your Dao implementation:

public class StudentDaoImpl extends GenericDaoImpl<Student> implements StudentDao{
    @Autowired
    public StudentDaoImpl(SessionFactory sessionFactory){
     super(sessionFactory, Student.class);
    }
}

With this approach you could reuse most of this code in all Dao :) So less code to maintain, easy to use.

Upvotes: 0

Arcangel2p
Arcangel2p

Reputation: 320

You should remove the public from the methods on the Interface:

package com.joseph.dao;

import java.util.List;

import com.joseph.model.Student;

public interface StudentDao {
   void add(Student student);
   void edit(Student student);
   void delete(int studentId);
   Student getStudent(int studentId);
   List getAllStudent();
}

Upvotes: 1

Related Questions