public class XPersistence
extends java.lang.Object
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.
getManager()
creates a session and transaction the first time
in the thread, and the OpenXava close the manager at the end of action execution.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.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.
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.
setPersistenceUnitProperties(java.util.Map)
can be use in the same way
that setPersistenceUnit(java.lang.String)
and setDefaultSchema(java.lang.String)
. Constructor and Description |
---|
XPersistence() |
Modifier and Type | Method and Description |
---|---|
static void |
commit()
Commits changes and closes the
EntityManager associated to
the current thread. |
static javax.persistence.EntityManager |
createManager()
Create a new
EntityManager . |
static java.lang.String |
getDefaultSchema()
The default schema used by JPA persistence in the current thread.
|
static javax.persistence.EntityManager |
getManager()
EntityManager associated to current thread. |
static java.lang.String |
getPersistenceUnit()
The name of persistence unit in persistence.xml file.
|
static java.util.Map |
getPersistenceUnitProperties()
The properties sent to Persistence (a JPA class) in order to create a
EntityManagerFactory.
|
static void |
reset()
Reset the info associated to the current thread.
|
static void |
resetEntityManagerFactory() |
static void |
rollback()
Rollback changes and closes the
EntityManager associated to
the current thread. |
static void |
setDefaultSchema(java.lang.String defaultSchema)
Change the default schema used by JPA persistence in the current thread.
|
static void |
setPersistenceUnit(java.lang.String persistenceUnitName)
The name of persistence unit in persistence.xml file.
|
static void |
setPersistenceUnitProperties(java.util.Map persistenceUnitProperties)
Set the properties to send to Persistence (a JPA class) in order to create a
EntityManagerFactory.
|
public static javax.persistence.EntityManager getManager()
EntityManager
associated to current thread.
If no manager exists or it's closed, then create a new one, and start a transaction.
public static javax.persistence.EntityManager createManager()
EntityManager
. This manager is not associated with the current thread, and no transaction is started.
public static void commit()
EntityManager
associated 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.
public static void rollback()
EntityManager
associated to
the current thread.
If no manager or it is closed this method does nothing.
public static void resetEntityManagerFactory()
public static java.lang.String getPersistenceUnit()
By default is default
.
The value of this properties may be different for each thread.
public static void setPersistenceUnit(java.lang.String persistenceUnitName)
By default is default
.
If you change this value it only take effect for the current thread.
If you sent a null then
default
is assumed.
public static java.util.Map getPersistenceUnitProperties()
The value can be different for each execution thread.
public static void setPersistenceUnitProperties(java.util.Map persistenceUnitProperties)
This only apply to the current execution thread.
public static java.lang.String getDefaultSchema()
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.
public static void setDefaultSchema(java.lang.String defaultSchema)
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.
public static void reset()
After call this method XPersistence works as default,
all previous call to setPersistenceUnit(java.lang.String)
,
setPersistenceUnitProperties(java.util.Map)
or setDefaultSchema(java.lang.String)
are annulled.