JPA:所有实体都有一个DAO,每个实体有一个DAO?

时间:2014-07-11 13:08:56

标签: java jpa entity eclipselink dao

首先,对不起我的英语。

我为朋友制作了一个访问SQLite数据库的JPA(EclipseLink)桌面应用程序。

我已经在Eclipse中创建了数据库和实体。但是我还创建了一个名为UniversalDAO的类,它是所有实体用来访问和保存数据库的实用程序类:

package model.DAO;

import java.util.ArrayList;

import javax.persistence.*;

import model.entities.Entities;


public class UniversalDAO {


    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("TheDatabase");
    private static EntityManager em = emf.createEntityManager();

    private UniversalDAO (){}


    public static void close(){
        em.close();
        emf.close();
    }

    public static Entities getOne(Class<? extends Entities> table, Object primaryKey) {

        return em.find(table, primaryKey);
    }

    public static ArrayList<Entities> getAll(Class<? extends Entities> table) {

        ArrayList<Entities> ret = new ArrayList<>();

        for(Object obj : em.createQuery("SELECT o FROM " + table.getName() + " o").getResultList())
            ret.add((Entities) obj);

        return ret;
    }

    public static ArrayList<Entities> getWithCondition(Class<? extends Entities> table, String condition) {

        ArrayList<Entities> ret = new ArrayList<>();

        for(Object obj : em.createQuery("SELECT o FROM " + table.getName() + " o WHERE " + condition).getResultList())
            ret.add((Entities) obj);

        return ret;
    }

    public static void insert(Entities row) {
        em.getTransaction().begin();

        em.persist(row);

        em.flush();

        em.getTransaction().commit();
    }

    public static void update(Entities row) {

        em.getTransaction().begin();

        em.merge(row);

        em.flush();

        em.getTransaction().commit();
    }

    public static void delete(Class<? extends Entities> table, Object primaryKey) {

        em.getTransaction().begin();

        Entities row = em.find(table, primaryKey);

        em.remove(row);

        em.flush();

        em.getTransaction().commit();
    }
}

要对所有entites进行分组并在此类中使用它,我创建了一个名为Entities的空接口。

这就是其中一个实体的样子:

package model.entities;

import java.util.ArrayList;

import javax.persistence.*;


@Entity
@Table(name="emails")
public class EntityEmail implements Entities {

    @Id
    @Column(name="id_email")
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private int idEmail;

    @Column(name="email")
    private String email;

    @Column(name="description")
    private String description;

    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(name="people_emails",
               joinColumns=@JoinColumn(name="id_email", referencedColumnName="id_email"),
               inverseJoinColumns=@JoinColumn(name="id_person", referencedColumnName="id_person"))
    private ArrayList<EntityPerson> people;

    public EntityEmail() {

    }   

    public int getIdEmail() {
        return this.idEmail;
    }
    public void setIdEmail(int idEmail) {
        this.idEmail = idEmail;
    }   

    public String getEmail() {
        return this.email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    public ArrayList<EntityPerson> getPeople() {
        return people;
    }
    public void setPeople(ArrayList<EntityPerson> people) {
        this.people = people;
    }   
}

就像你可以欣赏我不是一个专业人士,我仍然有很多东西需要学习。 所以,我想知道这种方法是否正确,或者我是否应该为每个实体都有一个DAO。

1 个答案:

答案 0 :(得分:6)

您似乎尝试发明通用DAO模式。如果是这样,你基本上是以正确的方式。

通用DAO模式的工作原理如下:

  • 创建一个所有DAO将扩展的通用基类:

    public abstract class GenericDao<E, ID extends Serializable> {
        ...
        // Implement common operations that are relevant to all entities here:
        public E findById(ID id) { ... }
        public void save(E entity) { ... }
        // etc
        ...
    }
    
  • 通过扩展GenericDao

    来创建具体的DAO实现
    public class EntityEmailDao extends GenericDao<EntityEmail, Integer> {
        // This class may contain operations that are relevant to specific entity:
        public E findByEmail(String email) { ... }
    }
    
  • 由于GenericDao是通用的,因此您不需要让您的实体扩展任何常用界面

这种模式有很多现有的实现,例如,here