Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of a Factory to allow a program to create an object instance that implements an abstract interface. And it allows an easy way

The purpose of a Factory to allow a program to create an object instance that implements an abstract interface. And it allows an easy way to substitute one implementation of the abstract interface for another.

In JPA, EntityManager is an example of an abstract interface. Both eclipse and hibernate are popular implementations of JPA. To use the eclipse implementation, you specify

org.eclipse.persistence.jpa.PersistenceProvider

in the persistence.xml. The application program executes the statement, Persistence.createEntityManagerFactory( ) which is a static method on the Persistence class. This method reads the persistence.xml file and loads the class from the eclipse implementation.

If you want to switch to Hibernate instead, no code in the application needs to change. Just modify the persistence.xml file to specify the hibernate implementation class.

Another useful pattern is the Singleton pattern. Creating an EntityManagerFactory involves a lot of CPU work; the persistence.xml file needs to opened, read, validated and parsed. The implementation class has to be loaded and database classes need to loaded as well.

An application only needs to do this once. A singleton pattern is used to ensure that only once instance of a particular object is created; in this example it would be EntityManagerFactory. If the application attempts to create another instance, the singleton does not create another instance, but rather return the previous instance already created.

Write a java class named EntityManagerSingleton that implements the singleton pattern for insuring only one instance of EntityManagerFactory is created by an application. Give an example of a servlet class that uses EntityManagerSingleton.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

What is an organizational feasibility analysis?

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago