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 :D