使用dao接口和实现来实现通用抽象实体类 [英] Implement Generic Abstract Entity class with dao interface and implemantation
问题描述
嘿,我想有一个通用的模型/实体类,该类/实体类将通过类型为long element的id从db下载.的方法是这样的:
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);
}
}
但是在将由子模型类扩展的泛型模型中,我必须动态声明要在db中找到的实体类名称.
But in Generic model that will be extended by child model class I'have to dynamically declare entity class name to find in db.
我想拥有通用的实体类,在这里我将拥有诸如getById()之类的通用方法.该泛型类将由具体实体类扩展.我不必在每个模型类中编写相同的方法,因为它将从通用类继承–
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 –
我该如何实现?
这是我的DAO界面.我不太确定:
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();
}
该接口的My DAO实现类
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);
}
}
还有我的GenericModel类
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;
}
}
请再给我一些额外的帮助:D
Please give me some extra help once more :D
推荐答案
您不能在静态方法中使用泛型参数(请参见 https: //stackoverflow.com/a/936951/1643132 了解更多信息),因此您必须从getBy()方法中删除static关键字.
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.
第一步,您可以在GenericModel类中引入泛型:
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);
}
}
问题是,类似T.class的东西将无法工作(在getBy()方法中). 感谢java.lang.reflect.ParameterizedType,您可以在运行时检索T类.因此,将您的GenericModel更新为:
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;
}
}
这篇关于使用dao接口和实现来实现通用抽象实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!