Java – Implement Generic Abstract Entity class with dao interface and implemantation

entityjavajpamodelspring

Hey I would like to have generic model/entity class that would download by id of type long element from db. The method for that is like this:

public class GenericModel   {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    public static GenericModel getBy(Long id) {
        return JPA.em().find(GenericModel.class, id);
    }
}

But in Generic model that will be extended by child model class I'have to dynamically declare entity class name to find in db.

I would like to have generic entity class where I will have common methods like getById(). And that generic class will be extended by concret entity class. I will not have to write in each model class the same method, cause it will be inherited from generic class –

How can I achive this?

Here is my DAO interface. I am not quite sure of it:

public interface GenericModelDao<T> {
    public void add(T entityClass);
    public void update(T entityClass);
    public void delete(long id);
    public T get(long id);
    public List<T> get();
}

And My DAO implementation class of this interface

 @Repository
public class GenericModelDaoImpl <T extends GenericModel> implements GenericModelDao {
    public Class<T> entityClass;

    GenericModelDaoImpl(){
        setEntityClass(((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
    }
    public void setEntityClass(Class<T> entityClass) {
        this.entityClass = entityClass;
    }
    @Autowired
    private SessionFactory sessionFactory;
    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public T get(long id) {
        return (T) getCurrentSession().get(entityClass, id);
    }
    @Override
    public void delete(long id) {
        T entityClass = get(id);
            getCurrentSession().delete(entityClass);
    }
    @Override
    public List<T> get() {
        return getCurrentSession().createQuery("from " + entityClass ).list();
    }
    @Override
    public void add(Object entityClass) {
        getCurrentSession().save(entityClass);
    }
    @Override
    public void update(Object entityClass) {
        getCurrentSession().update(entityClass);
    }
}

And my GenericModel Class

@MappedSuperclass
public abstract class GenericModel<T extends GenericModel> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

}

Please give me some extra help once more 😀

Best Solution

You cannot use generics parameters in a static method (see https://stackoverflow.com/a/936951/1643132 for more details), so you will have to remove static keyword from getBy() method.

As a first step, you can introduce generics in your GenericModel class:

public abstract class GenericModel<T extends GenericModel> {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    public T getBy(Long id) {
        return JPA.em().find(????, id);
    }
}

The problem is, something like T.class will not work (in getBy() method). Thanks to java.lang.reflect.ParameterizedType, you can retrieve T class at runtime. So update you GenericModel to :

@MappedSuperclass
public abstract class GenericModel<T extends GenericModel> {

    private Class<T> entityClass;

    GenericModel(){
        entityClass = ((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    public T getBy(Long id) {
        return JPA.em().find(entityClass, id);
    }

    public Long getId() {
        return id;
    }
}

Your child entity may look like:

@Table(name="child_model")
@javax.persistence.Entity
public class ChildModel extends GenericModel<ChildModel> {

    @Column
    private String data;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
Related Question