Class XPersistence
- java.lang.Object
-
- org.openxava.jpa.XPersistence
-
public class XPersistence extends java.lang.Object
Allows to work easily with EJB3 JPA inside OpenXava applications.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 methodgetManager()
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 created
Per 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 methodsetPersistenceUnitProperties(java.util.Map)
can be use in the same way thatsetPersistenceUnit(java.lang.String)
andsetDefaultSchema(java.lang.String)
.- Author:
- Javier Paniza
-
-
Constructor Summary
Constructors Constructor Description XPersistence()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
commit()
Commits changes and closes theEntityManager
associated to the current thread.static javax.persistence.EntityManager
createManager()
Create a newEntityManager
.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 theEntityManager
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.
-
-
-
Method Detail
-
getManager
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.
- 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 theEntityManager
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.
-
rollback
public static void rollback()
Rollback changes and closes theEntityManager
associated 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
public static java.lang.String 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
public static void setPersistenceUnit(java.lang.String persistenceUnitName)
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 then
default
is assumed.
-
getPersistenceUnitProperties
public static java.util.Map 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
public static void setPersistenceUnitProperties(java.util.Map persistenceUnitProperties)
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
public static java.lang.String 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
public static void setDefaultSchema(java.lang.String defaultSchema)
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.
-
-