Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate JPA简化更新()、删除()、添加()的代码_Java_Hibernate_Jpa_Refactoring - Fatal编程技术网

Java Hibernate JPA简化更新()、删除()、添加()的代码

Java Hibernate JPA简化更新()、删除()、添加()的代码,java,hibernate,jpa,refactoring,Java,Hibernate,Jpa,Refactoring,我创建了使用JPA的update()方法。看起来是这样的: public boolean update(Programy program) throws Exception { try { entityManagerFactory = Persistence.createEntityManagerFactory("firebird_config_file"); entityManager = entityManagerFactory

我创建了使用JPA的update()方法。看起来是这样的:

public boolean update(Programy program) throws Exception {
        try {
            entityManagerFactory = Persistence.createEntityManagerFactory("firebird_config_file");
            entityManager = entityManagerFactory.createEntityManager();

            entityManager.getTransaction().begin();
            entityManager.merge(program);
            entityManager.getTransaction().commit();

            entityManager.close();
            entityManagerFactory.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

在我的save()和delete方法中,我只更改了一件事-merge()->persist()或delete()。代码的其余部分与此处类似。如何重构此代码以简化此过程

这是一个非常好的模式用例,名为

例如,您可以创建一个抽象类,它将所有样板代码包装在
perform
方法中:

abstract public class HibernateAction<T> {
    private EntityManagerFactory entityManagerFactory;
    //I'm passing EntityManagerFactory, because it should be singleton and you shouldn't
    //probably create it from scratch everytime
    public HibernateAction(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    protected abstract T action(EntityManager entityManager, T entity);

    public boolean perform(T entity) throws Exception {
        try {
            var entityManager = entityManagerFactory.createEntityManager();
            entityManager.getTransaction().begin();
            action(entityManager, entity); //call to action which need to be overriden
            entityManager.getTransaction().commit();
            entityManager.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }   
    }
}
最后你可以这样使用它:

public boolean update(Program program) throws Exception {
    return updateAction.perform(program);
}
但是,由于Java支持匿名方法(自Java 8以来),您也可以使用更高阶函数以稍微不太冗长的方式重写它:

public class HibernateAction2{ // no longer abstract

    private EntityManagerFactory entityManagerFactory;

    public HibernateAction2(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    //we expect a user to pass lambda function, which would tell us what to do with an entity manager
    public boolean perform (Consumer<EntityManager> action) throws Exception {
        try {
            var entityManager = entityManagerFactory.createEntityManager();
            entityManager.getTransaction().begin();
            action.accept(entityManager);
            entityManager.getTransaction().commit();
            entityManager.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
这被称为loan模式或loaner模式(FP语言中的括号),因为您可以从hibernateAction2“借用”实体管理器来使用它执行某种操作,但它可以处理所有其他事情,如创建对象或关闭连接

public class HibernateAction2{ // no longer abstract

    private EntityManagerFactory entityManagerFactory;

    public HibernateAction2(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    //we expect a user to pass lambda function, which would tell us what to do with an entity manager
    public boolean perform (Consumer<EntityManager> action) throws Exception {
        try {
            var entityManager = entityManagerFactory.createEntityManager();
            entityManager.getTransaction().begin();
            action.accept(entityManager);
            entityManager.getTransaction().commit();
            entityManager.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
hibernateAction2.perform(em -> em.merge(program)); //for merge

hibernateAction2.perform(em -> em.persist(program)); //for persist, etc.