使用dao接口和实现实现Generic Abstract Entity类

时间:2014-11-06 15:02:13

标签: java spring jpa model entity

嘿我想拥有通用模型/实体类,它将从db下载类型为long元素的id。对此的方法是这样的:

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

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

但是在子模型类扩展的Generic模型中,我必须动态声明要在db中查找的实体类名。

我想拥有通用实体类,我将使用常见的方法,如getById()。该泛型类将由concret实体类扩展。我不必在每个模型类中编写相同的方法,因为它将继承自泛型类 -

我怎么能做到这一点?

这是我的DAO界面。我不太确定:

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();
}

这个界面的我的DAO实现类

 @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 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

2 个答案:

答案 0 :(得分:4)

您不能在静态方法中使用泛型参数(有关详细信息,请参阅https://stackoverflow.com/a/936951/1643132),因此您必须从getBy()方法中删除static关键字。

作为第一步,您可以在GenericModel类中引入泛型:

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更新为:

@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;
    }
}

您的子实体可能如下所示:

@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;
    }
}

答案 1 :(得分:0)

这是我的工作代码。希望帮助别人:D

通用模型/实体类

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

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

    public long getId() {return id;}
    public void setId(long id) {this.id = id;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}

}

GenericModelDao界面

import java.util.List;

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 T getByName(String name);
    public List<T> get();
}

GenericModelDaoImpl类

import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.persistence.MappedSuperclass;

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

import model.GenericModel;

@Repository
@MappedSuperclass
public abstract class GenericModelDaoImpl <T extends GenericModel> implements GenericModelDao<T> {
    GenericModelDaoImpl(){
        setEntityClass(((Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));
    }

    public Class<T> entityClass;

    public Class<T> getEntityClass() {
        return entityClass;
    }
    public void setEntityClass(Class<T> entityClass) {
        this.entityClass = entityClass;
    }
    @Autowired
    private SessionFactory sessionFactory;
    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void add(T entityClass) {
        getCurrentSession().save(entityClass);
    }
    @Override
    public void update(T entityClass) {
        getCurrentSession().update(entityClass);
    }
    @Override
    public void delete(long id) {
        getCurrentSession().delete(get(id));
    }
    @Override
    public T get(long id) {
        return (T) getCurrentSession().get(entityClass, id);
    }
    @Override
    public T getByName(String name) {
        return (T) getCurrentSession().createQuery("FROM e " + entityClass.getSimpleName() + " WHERE name:=name").setParameter("name", name);
    }
    @Override
    public List<T> get() {
        return getCurrentSession().createQuery("FROM " + entityClass.getSimpleName() ).list();
    }
}

我的工人班的例子: 客户模型/实体类

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="common__Customer")
public class Customer extends GenericModel<Customer> {}

CustomerDao界面

import com.common.model.Customer;
public interface CustomerDao  extends GenericModelDao<Customer> {}

CustomerDaoImpl类

import org.springframework.stereotype.Repository;
import com.common.model.Customer;

@Repository
public class CustomerDaoImpl extends GenericModelDaoImpl<Customer> implements CustomerDao {}