Persistence createentitymanagerfactory java code source

Java Persistence createEntityManagerFactory(String persistenceUnitName, Map properties)

Java Persistence createEntityManagerFactory(String persistenceUnitName, Map properties) Create and return an EntityManagerFactory for the named persistence unit using the given properties.

Introduction

Syntax

The method createEntityManagerFactory() from Persistence is declared as:

public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) 

The method createEntityManagerFactory() has the following parameter:

  • StringpersistenceUnitName — the name of the persistence unit
  • Mapproperties — Additional properties to use when creating the factory. These properties may include properties to control schema generation. The values of these properties override any values that may have been configured elsewhere.

The method createEntityManagerFactory() returns the factory that creates EntityManagers configured according to the specified persistence unit.

Example

The following code shows how to use Persistence from javax.persistence.

Specifically, the code shows you how to use Java Persistence createEntityManagerFactory(String persistenceUnitName, Map properties)

import java.util.Properties; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class DatabaseGen < public static void main(String[] args) < Properties config = new Properties(); config.put("hibernate.hbm2ddl.auto", "create"); EntityManagerFactory factory = Persistence.createEntityManagerFactory("Aula12PU", config); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); em.getTransaction().commit(); > >
import java.util.HashMap; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.Persistence; import org.eclipse.persistence.config.PersistenceUnitProperties; public class CretaeTables < public static void main(String[] args) < MapString, String> properties = new HashMap<>(); properties.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.DROP_AND_CREATE); EntityManager em = Persistence.createEntityManagerFactory("BBDD", properties).createEntityManager(); System.out.println("-----" + em.find(User2.class, 1)); >// w w w .d e mo 2s . c o m >
/*/* w w w . d e m o 2s . c o m*/ * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.Properties; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; /** * * @author Felippe */ public class GeradorDeBanco < public static void main(String[] args) < Properties properties = new Properties(); EntityManagerFactory factory = Persistence.createEntityManagerFactory("ClinicaProjectPU", properties); factory.createEntityManager(); factory.close(); > >

  • Java ParameterMode REF_CURSOR
  • Java javax.persistence Persistence
  • Java Persistence tutorial with examples
  • Java Persistence createEntityManagerFactory(String persistenceUnitName, Map properties)
  • Java Persistence createEntityManagerFactory(String persistenceUnitName)
  • Java Persistence generateSchema(String persistenceUnitName, Map map)
  • Java Persistence getPersistenceUtil()

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Persistence createentitymanagerfactory java code source

Bootstrap class that is used to obtain an EntityManagerFactory in Java SE environments. It may also be used to cause schema generation to occur. The Persistence class is available in a Java EE container environment as well; however, support for the Java SE bootstrapping APIs is not required in container environments. The Persistence class is used to obtain a PersistenceUtil instance in both Java EE and Java SE environments.

Field Summary

Constructor Summary

Method Summary

Create database schemas and/or tables and/or create DDL scripts as determined by the supplied properties.

Methods inherited from class java.lang.Object

Field Detail

PERSISTENCE_PROVIDER

@Deprecated public static final String PERSISTENCE_PROVIDER

This final String is deprecated and should be removed and is only here for TCK backward compatibility

providers

@Deprecated protected static final SetPersistenceProvider> providers

This instance variable is deprecated and should be removed and is only here for TCK backward compatibility

Constructor Detail

Persistence

Method Detail

createEntityManagerFactory

public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName)

createEntityManagerFactory

public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties)

generateSchema

public static void generateSchema(String persistenceUnitName, Map map)

Create database schemas and/or tables and/or create DDL scripts as determined by the supplied properties. Called when schema generation is to occur as a separate phase from creation of the entity manager factory.

getPersistenceUtil

Источник

JPA EntityManagerFactory Interface With Example

Join the DZone community and get the full member experience.

The main role of an EntityManagerFactory instance is to support instantiation of EntityManager instances. An EntityManagerFactory is constructed for a specific database, and by managing resources efficiently (e.g. a pool of sockets), it provides an efficient way to construct multiple EntityManager instances for that database.

The instantiation of the EntityManagerFactory itself might be less efficient, but it is a one-time operation. Once constructed, it can serve the entire application. When the application has finished using the entity manager factory, and/or at application shutdown, the application should close the entity manager factory. Once an EntityManagerFactory has been closed, all its entity managers are considered to be in the closed state.

EntityManagerFactory Interface — Class Diagram

This class diagram shows a list of APIs that the EntityManagerFactory Interface provides.

EntityManagerFactory Interface — Method Summary

Here the list of important methods EntityManagerFactory interface for your reference:

  • void addNamedEntityGraph(String graphName, EntityGraph entityGraph) — This method add a named copy of the EntityGraph to the EntityManagerFactory .
  • void addNamedQuery(String name, Query query) — This method is used to define the query, typed query, or stored procedure query as a named query such that future query objects can be created from it using the createNamedQuery or createNamedStoredProcedureQuery method.
  • void close() — This method close the factory, releasing any resources that it holds.
  • EntityManager createEntityManager() — This creates a new application-managed EntityManager .
  • EntityManager createEntityManager(Map map) — This method is used to create a new application-managed EntityManager with the specified Map of properties.
  • EntityManager createEntityManager(SynchronizationType synchronizationType) — This method is used to create a new JTA application-managed EntityManager with the specified synchronization type.
  • EntityManager createEntityManager(SynchronizationType synchronizationType, Map map) — This method used to create a new JTA application-managed EntityManager with the specified synchronization type and map of properties.
  • Cache getCache() — Access the cache that is associated with the entity manager factory (the «second level cache»).
  • CriteriaBuilder getCriteriaBuilder() — Return an instance of CriteriaBuilder for the creation of CriteriaQuery objects.
  • Metamodel getMetamodel() — Return an instance of the Metamodel interface for access to the metamodel of the persistence unit.
  • PersistenceUnitUtil getPersistenceUnitUtil() This method return the interface, providing access to utility methods for the persistence unit.
  • Map getProperties() — This method get the properties and associated values that are in effect for the entity manager factory.
  • boolean isOpen() — Indicates whether the factory is open.
  • T unwrap(Class cls) Return an object of the specified type to allow access to the provider-specific API.

EntityManagerFactory Interface Example

Let’s demonstrates the important methods of the EntityManagerFactory Interface with an example. In this example, we will use the createEntityManager() method to create a new application-managed EntityManager.

Step 1: Creating an Entity Manager Factory Object

The EntityManagerFactory interface present in the java.persistence package is used to provide an entity manager.

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");
  • Persistence — The Persistence is a bootstrap class, which is used to obtain an EntityManagerFactory interface.
  • createEntityManagerFactory() method — The role of this method is to create and return an EntityManagerFactory for the named persistence unit. Thus, this method contains the name of the persistence unit passed in the Persistence.xml file.

Step 2: Obtaining an Entity Manager From a Factory

EntityManager entityManager = entityManagerFactory.createEntityManager();
  • EntityManager — An EntityManager is an interface.
  • createEntityManager() method — It creates a new application-managed EntityManager

Step 3: Intializing an Entity Manager

entityManager.getTransaction().begin();
  • getTransaction() method — This method returns the resource-level EntityTransaction object.
  • begin() method — This method is used to start the transaction.

Step 4: Persisting a Data Into the Relational Database

entityManager.persist(student); 
  • persist() — This method is used to make an instance managed and persistent. An entity instance is passed within this method.

Step 5: Closing the Transaction

entityManager.getTransaction().commit();

Step 6: Releasing the Factory Resources

entityManager.close(); entityManagerFactory.close();

Complete Example

private static void insertEntity()

A good resource to learn JPA at JPA Tutorial — Java Persistence API

Further Learning:

References

Published at DZone with permission of Ramesh Fadatare . See the original article here.

Opinions expressed by DZone contributors are their own.

Источник

Читайте также:  Ширина текстового поля
Оцените статью