Class XPersistence
You can use this class in any part of your OpenXava application:
calculators, validators, actions, junit tests, etc.
To use JPA with this class you can write something as:
Invoice invoice = new Invoice(); XPersistence.getManager().persist(invoice);And no more.
The method
getManager() creates a session and transaction the first time
in the thread, and the OpenXava close the manager at the end of action execution.Also the next code is legal:
Invoice invoice = new Invoice(); XPersistence.getManager().persist(invoice); XPersistence.commit(); // In this way you commit and close manually. ... XPersistence.getManager().persist(customer); // As manager has been closed, a new one is createdPer thread configuration
XPersistence allows you to change the configuration in runtime affecting only to the current thread of execution.
For example, if you want to have two configuration: 'config1' and 'config2' in your persistence.xml, and you want to change from one to other, it's easy:
XPersistence.setPersistenceUnit("config1");
XPersistence.getManager().persist(invoice); // Using config1
...
XPersistence.commit();
XPersistence.setPersistenceUnit("config2");
XPersistence.getManager().persist(invoice); // Using config2
...
XPersistence.commit();
And this change only affect to the current execution thread.Or if you want to change the default schema you can write:
XPersistence.setDefaultSchema("COMPANY1");
XPersistence.getManager().persist(invoice); // Save in INVOICE table of COMPANY1 Schema
...
XPersistence.commit();
XPersistence.setDefaultSchema("COMPANY2");
XPersistence.getManager().persist(invoice); // Save in INVOICE table of COMPANY2 Schema
...
XPersistence.commit();
Also this only affect to the current thread.The method
setPersistenceUnitProperties(java.util.Map) can be use in the same way
that setPersistenceUnit(java.lang.String) and setDefaultSchema(java.lang.String). - Author:
- Javier Paniza
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcommit()Commits changes and closes theEntityManagerassociated to the current thread.static javax.persistence.EntityManagerCreate a newEntityManager.static StringThe default schema used by JPA persistence in the current thread.static javax.persistence.EntityManagerEntityManagerassociated to current thread.static StringThe name of persistence unit in persistence.xml file.static MapThe properties sent to Persistence (a JPA class) in order to create a EntityManagerFactory.static voidreset()Reset the info associated to the current thread.static voidstatic voidrollback()Rollback changes and closes theEntityManagerassociated to the current thread.static voidsetDefaultSchema(String defaultSchema) Change the default schema used by JPA persistence in the current thread.static voidsetPersistenceUnit(String persistenceUnitName) The name of persistence unit in persistence.xml file.static voidsetPersistenceUnitProperties(Map persistenceUnitProperties) Set the properties to send to Persistence (a JPA class) in order to create a EntityManagerFactory.
-
Constructor Details
-
XPersistence
public XPersistence()
-
-
Method Details
-
getManager
public static javax.persistence.EntityManager getManager()EntityManagerassociated to current thread.If no manager exists or it's closed, then create a new one, and start a transaction.
- Returns:
- Not null
-
createManager
public static javax.persistence.EntityManager createManager()Create a newEntityManager.This manager is not associated with the current thread, and no transaction is started.
- Returns:
- Not null
-
commit
public static void commit()Commits changes and closes theEntityManagerassociated to the current thread.If no manager or it is closed this method does nothing.
In most cases this method is called by OpenXava automatically, hence the programmer that uses JPA APIs does not need to call it. -
rollback
public static void rollback()Rollback changes and closes theEntityManagerassociated to the current thread.If no manager or it is closed this method does nothing.
-
resetEntityManagerFactory
public static void resetEntityManagerFactory()- Since:
- 5.7.1
-
getPersistenceUnit
The name of persistence unit in persistence.xml file.By default is
default.
The value of this properties may be different for each thread. -
setPersistenceUnit
The name of persistence unit in persistence.xml file.By default is
default.
If you change this value it only take effect for the current thread.If you sent a
null thendefaultis assumed. -
getPersistenceUnitProperties
The properties sent to Persistence (a JPA class) in order to create a EntityManagerFactory.The value can be different for each execution thread.
- Returns:
- Not null
-
setPersistenceUnitProperties
Set the properties to send to Persistence (a JPA class) in order to create a EntityManagerFactory.This only apply to the current execution thread.
-
getDefaultSchema
The default schema used by JPA persistence in the current thread.For example, if you use 'COMPANYA' as default schema, and you OX component or EJB3 entity is mapping to a table named 'ISSUE' when OX and JPA engine try to execute SQL they will use 'COMPANYA.ISSUE' as table name.
-
setDefaultSchema
Change the default schema used by JPA persistence in the current thread.For example, if you use 'COMPANYA' as default schema, and you OX component or EJB3 entity is mapping to a table named 'ISSUE' when OX and JPA engine try to execute SQL they will use 'COMPANYA.ISSUE' as table name.
-
reset
public static void reset()Reset the info associated to the current thread.After call this method XPersistence works as default, all previous call to
setPersistenceUnit(java.lang.String),setPersistenceUnitProperties(java.util.Map)orsetDefaultSchema(java.lang.String)are annulled.
-