gpt4 book ai didi

java - Hibernate JPA 简化了 update()、delete()、add() 的代码

转载 作者:行者123 更新时间:2023-11-30 01:53:22 29 4
gpt4 key购买 nike

我创建了使用 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()。代码的其余部分与此处类似。我如何重构此代码以简化此操作?

最佳答案

对于名为 template method 的模式来说,这是一个非常好的用例。 .

例如,您可以创建一个抽象类,它将所有样板代码包装在 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;
}
}
}

那么你可以创建一个继承HibernateAction的类:

public class UpdateAction extends HibernateAction<Program> {

public UpdateAction(EntityManagerFactory entityManagerFactory) {
super(entityManagerFactory);
}

@Override
protected Program action(EntityManager entityManager, Program entity) {
return entityManager.merge(entity);
}
}

最后你可以像这样使用它:

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

然后你可以像这样使用它:

hibernateAction2.perform(em -> em.merge(program)); //for merge

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

这称为借用模式借用者模式(或FP语言中的括号),因为您将实体管理器从 hibernateAction2“借用”到使用它执行某种操作,但它处理所有其他事情,例如创建对象或关闭连接。

关于java - Hibernate JPA 简化了 update()、delete()、add() 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301042/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com