persistence.xml different transaction-type attributes

Defaults Default to JTA in a JavaEE environment and to RESOURCE_LOCAL in a JavaSE environment. RESOURCE_LOCAL With <persistence-unit transaction-type=”RESOURCE_LOCAL”> you are responsible for EntityManager (PersistenceContext/Cache) creating and tracking You must use the EntityManagerFactory to get an EntityManager The resulting EntityManager instance is a PersistenceContext/Cache An EntityManagerFactory can be injected via the @PersistenceUnit annotation only (not … Read more

Changing Persistence Unit dynamically – JPA

Keep the persistence unit file (Persistence.xml) as it’s. You can override the properties in it as follows. EntityManagerFactory managerFactory = null; Map<String, String> persistenceMap = new HashMap<String, String>(); persistenceMap.put(“javax.persistence.jdbc.url”, “<url>”); persistenceMap.put(“javax.persistence.jdbc.user”, “<username>”); persistenceMap.put(“javax.persistence.jdbc.password”, “<password>”); persistenceMap.put(“javax.persistence.jdbc.driver”, “<driver>”); managerFactory = Persistence.createEntityManagerFactory(“<current persistence unit>”, persistenceMap); manager = managerFactory.createEntityManager();

No Persistence provider for EntityManager named X

You must move persistence.xml file to an appropriate location. More specifically, add META-INF/persistence.xml file to the root of a source folder. In this case, the following is an appropriate location: src\main\java\META-INF\persistence.xml Here are the details: (taken from the JPA spec) A persistence.xml file defines a persistence unit. The persistence.xml file is located in the META-INF … Read more

Why do I need to configure the SQL dialect of a data source?

Dialect means “the variant of a language”. Hibernate, as we know, is database agnostic. It can work with different databases. However, databases have proprietary extensions/native SQL variations, and set/sub-set of SQL standard implementations. Therefore at some point hibernate has to use database specific SQL. Hibernate uses “dialect” configuration to know which database you are using … Read more

Create JPA EntityManager without persistence.xml configuration file

Is there a way to initialize the EntityManager without a persistence unit defined? You should define at least one persistence unit in the persistence.xml deployment descriptor. Can you give all the required properties to create an Entitymanager? The name attribute is required. The other attributes and elements are optional. (JPA specification). So this should be … Read more