in Education by
I created update() method which is using JPA. It looks like this: 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; } } In my save() and delete method I am changing only one thing - merge() -> persist() or delete(). Remaining part of code is similar as here. How can i refactor this code to simplify this? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This is a very good use-case for a pattern called template method. For example, you can create an abstract class, which wraps all your boilerplate code in perform method: abstract public class HibernateAction { 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; } } } then you can just create a class which inherits HibernateAction: public class UpdateAction extends HibernateAction { public UpdateAction(EntityManagerFactory entityManagerFactory) { super(entityManagerFactory); } @Override protected Program action(EntityManager entityManager, Program entity) { return entityManager.merge(entity); } } and finally you can use it like this: public boolean update(Program program) throws Exception { return updateAction.perform(program); } But since anonymous methods are supported in Java (since Java 8), you could also rewrite it in a little bit less verbose way using higher order functions: 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 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; } } } and then you can use it like: hibernateAction2.perform(em -> em.merge(program)); //for merge hibernateAction2.perform(em -> em.persist(program)); //for persist, etc. This is called loan pattern or loaner pattern (or bracket in FP languages), because you "loan" entity manager from hibernateAction2 to use it to do some kind of action, but it handles all other things, like creating an object or closing connection.

Related questions

0 votes
    I am wanting to understand this Karaf JPA example. When I follow the instructions to add features and run ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have a problem with Hibernate. In Short: How to configure a ManyToMany association with Hibernate when the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    You can add, delete,duplicate and rearrange slide in…………… Tab Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    which of the following tab should be used to add and delete element such as chapter number hyperlink etc a) ... style d) background Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    which of the following tab should be used to add and delete element such as chapter number ,hyperlink etc Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    To add, delete objects in a report, applying conditional formatting, applying formulas in report, creating variables, you should open Webi document in which of the following mode?...
asked Mar 13, 2021 in Technology by JackTerrance
0 votes
    Make Twenty (20) SQL questions including create table, insert, update, delete, join, string operation, aggregate ... books or webs Select the correct answer from above options...
asked Dec 9, 2021 in Education by JackTerrance
0 votes
    Make Twenty (20) SQL questions including create table, insert, update, delete, join, string operation, aggregate ... books or webs Select the correct answer from above options...
asked Dec 3, 2021 in Education by JackTerrance
0 votes
    What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'] ... ;, ‘c’, [‘p’, ‘q’], ‘san}...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What will be the output of the following Python program? z=set('abc') z.add('san') z.update(set(['p', 'q'] ... ;, ‘c’, [‘p’, ‘q’], ‘san}...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    I need to determine the status of a parent object based on 2 variables of each of its children. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I need to determine the status of a parent object based on 2 variables of each of its children. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Simplify the Boolean expression using algebraic method (A’+B)(A+B) Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    How does Kubernetes simplify containerized Deployment?...
asked Oct 2, 2021 in Technology by JackTerrance
0 votes
    There is a practice in coding to keep some code blocks in comment symbols than delete it when debugging. How this affects when debugging?...
asked Nov 8, 2020 in Technology by JackTerrance
...