Package org.eclipse.persistence.jpa.rs
Class PersistenceContext
java.lang.Object
org.eclipse.persistence.jpa.rs.PersistenceContext
A wrapper around the JPA and JAXB artifacts used to persist an application.
A PersistenceContext provides the capability of using the same persistence unit in JPA to
to interact with a Database or other JPA-capable data source and in JAXB to interact with either
XML or JSON.
A PersistenceContext can wrap either an existing persistence unit (EntityManagerFactory), or it can be used to bootstrap a
fully dynamic persistence unit.
- Author:
- douglas.clarke, tom.ware
-
Field Summary
Modifier and TypeFieldDescriptionprotected List<jakarta.xml.bind.annotation.adapters.XmlAdapter<?,
?>> protected URI
The URI of the Persistence context.static final String
protected jakarta.persistence.EntityManagerFactory
The EntityManagerFactory used to interact using JPAprotected JAXBContext
The JAXBConext used to produce JSON or XMLstatic final String
protected String
The name of the persistence context is used to look it up.static final String
protected TransactionWrapper
-
Constructor Summary
ModifierConstructorDescriptionprotected
PersistenceContext
(String emfName, org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl emf, URI defaultURI) Instantiates a new persistence context.PersistenceContext
(String emfName, org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl emf, URI defaultURI, ServiceVersion version) Instantiates a new persistence context. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addDynamicXMLMetadataSources
(List<Object> metadataSources, org.eclipse.persistence.internal.sessions.AbstractSession session) This method is used to help construct a JAXBContext from an existing EntityManagerFactory.jakarta.persistence.Query
Builds the query.protected jakarta.persistence.Query
constructQuery
(jakarta.persistence.EntityManager em, String name, Map<?, ?> parameters, Map<String, ?> hints) void
A part of the facade over the JPA API.protected JAXBContext
createDynamicJAXBContext
(org.eclipse.persistence.internal.sessions.AbstractSession session) Create a JAXBContext based on the EntityManagerFactory for this PersistenceContext.protected jakarta.persistence.EntityManager
createEntityManager
(String tenantId) A part of the facade over the JPA API Create an EntityManager from the EntityManagerFactory wrapped by this persistence contextprotected org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl
createEntityManagerFactory
(jakarta.persistence.spi.PersistenceUnitInfo info, Map<String, ?> properties) A part of the facade over the JPA API.createJAXBProperties
(org.eclipse.persistence.internal.sessions.AbstractSession session) Build the set of properties used to create the JAXBContext based on the EntityManagerFactory that this PersistenceContext wrapsvoid
A part of the facade over the JPA API Delete the given entity in JPA and commit the changesboolean
Does exist.boolean
protected void
finalize()
Finalize.A part of the facade over the JPA API Find an entity with the given name and id in JPAA part of the facade over the JPA API Find an entity with the given name and id in JPAA part of the facade over the JPA API Find an entity with the given name and id in JPAprotected List<jakarta.xml.bind.annotation.adapters.XmlAdapter<?,
?>> Gets the base uri.Class<?>
Look-up the given entity name in the EntityManagerFactory and return the class is describesorg.eclipse.persistence.internal.sessions.AbstractSession
getClientSession
(jakarta.persistence.EntityManager em) Gets the client session.Getter for the collectionWrapperBuilder property with lazy initialization.getDescriptor
(String entityName) Lookup the descriptor for the given entity name.getDescriptorForClass
(Class clazz) Gets the descriptor for class.jakarta.persistence.EntityManagerFactory
getEmf()
Gets the emf.Gets the jAXB context.getJAXBDescriptorForClass
(Class clazz) Gets the jAXB descriptor for class.getName()
Gets the name.getPageableQuery
(String queryName) Gets REST pageable query details by query name.Gets the jpa server session.Gets JPARS version.Gets the supported feature set.Gets the version as it appears in URI.int
hashCode()
boolean
isQueryPageable
(String queryName) Finds out is given query pageable or not.boolean
Checks if is weaving enabled.void
marshall
(Object object, jakarta.ws.rs.core.MediaType mediaType, OutputStream output, boolean sendRelationships) Marshall an entity to either JSON or XML.void
marshall
(Object object, jakarta.ws.rs.core.MediaType mediaType, OutputStream output, boolean sendRelationships, FieldsFilter fieldsFilter) Marshall an entity to either JSON or XML.void
marshallEntity
(Object object, jakarta.ws.rs.core.MediaType mediaType, OutputStream output) Marshall an entity to either JSON or XML Calling this method, will treat relationships as unfetched in the XML/JSON and marshall them as links rather than attempting to marshall the data in those relationshipsvoid
marshallEntity
(Object object, FieldsFilter filter, jakarta.ws.rs.core.MediaType mediaType, OutputStream output) Marshall an entity to either JSON or XML.A part of the facade over the JPA API Call jpa merge on the given object and commit If the passed object is a list, we will iterate through the list and merge each memberA convenience method to create a new dynamic entity of the given typeA convenience method to create a new dynamic entity of the given typeprotected void
postMarshallEntity
(Object object) protected void
preMarshallEntity
(Object object) Process an entity and add any additional data that needs to be added prior to marshalling This method will both single entities and lists of entitiesprotected void
preMarshallIndividualEntity
(Object entity) Add any data required prior to marshalling an entity to XML or JSON In general, this will only affect fields that have been weaved into the objectint
queryExecuteUpdate
(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) Query execute update.queryMultipleResults
(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) Query multiple results.removeAttribute
(Map<String, String> tenantId, String entityName, Object id, String attribute, String listItemId, Object entity, String partner) Removes the attribute.protected void
removeMappingValueFromObject
(Object object, Object attributeValue, DatabaseMapping mapping, DatabaseMapping partner) void
setBaseURI
(URI baseURI) Sets the base uri.protected void
setMappingValueInObject
(Object object, Object attributeValue, DatabaseMapping mapping, DatabaseMapping partner) void
setVersion
(String version) Sets the version.void
stop()
Stop the current application instancetoString()
To string.unmarshal
(Class type, jakarta.ws.rs.core.MediaType acceptedMediaType, InputStream in) Unmarshal.unmarshalEntity
(String type, jakarta.ws.rs.core.MediaType acceptedMediaType, InputStream in) Unmarshal entity.updateOrAddAttribute
(Map<String, String> tenantId, String entityName, Object id, Map<String, Object> properties, String attribute, Object attributeValue, String partner) Update or add attribute.protected Object
Make adjustments to an unmarshalled entity based on what is found in the weaved fields
-
Field Details
-
JPARS_CONTEXT
- See Also:
-
CLASS_NAME
-
SESSION_VERSION_PROPERTY
- See Also:
-
adapters
-
name
The name of the persistence context is used to look it up. By default it will be the persistence unit name of the JPA persistence unit. -
emf
protected jakarta.persistence.EntityManagerFactory emfThe EntityManagerFactory used to interact using JPA -
jaxbContext
The JAXBConext used to produce JSON or XML -
baseURI
The URI of the Persistence context. This is used to build Links in JSON and XML -
transaction
-
-
Constructor Details
-
PersistenceContext
protected PersistenceContext() -
PersistenceContext
public PersistenceContext(String emfName, org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl emf, URI defaultURI) Instantiates a new persistence context.- Parameters:
emfName
- the emf nameemf
- the emfdefaultURI
- the default uri
-
PersistenceContext
public PersistenceContext(String emfName, org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl emf, URI defaultURI, ServiceVersion version) Instantiates a new persistence context.- Parameters:
emfName
- the emf nameemf
- the emfdefaultURI
- the default uriversion
- REST service version
-
-
Method Details
-
isWeavingEnabled
public boolean isWeavingEnabled()Checks if is weaving enabled.- Returns:
- true, if is weaving enabled
-
getVersion
Gets the version as it appears in URI.- Returns:
- The version.
-
getServiceVersion
Gets JPARS version.- Returns:
- JPARS version.
-
addDynamicXMLMetadataSources
protected void addDynamicXMLMetadataSources(List<Object> metadataSources, org.eclipse.persistence.internal.sessions.AbstractSession session) This method is used to help construct a JAXBContext from an existing EntityManagerFactory. For each package in the EntityManagerFactory, a MetadataSource that is capable of building a JAXBContext that creates the same mappings in JAXB is created. These MetadataSources are used to constuct the JAXContext that is used for JSON and XML translation.- Parameters:
metadataSources
-session
-
-
create
A part of the facade over the JPA API. Persist an entity in JPA and commit.- Parameters:
tenantId
-entity
-- Throws:
Exception
-
createDynamicJAXBContext
protected JAXBContext createDynamicJAXBContext(org.eclipse.persistence.internal.sessions.AbstractSession session) throws jakarta.xml.bind.JAXBException, IOException Create a JAXBContext based on the EntityManagerFactory for this PersistenceContext.- Parameters:
session
-- Returns:
- Throws:
jakarta.xml.bind.JAXBException
IOException
-
createEntityManagerFactory
protected org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl createEntityManagerFactory(jakarta.persistence.spi.PersistenceUnitInfo info, Map<String, ?> properties) A part of the facade over the JPA API. Create an EntityManagerFactory using the given PersistenceUnitInfo and properties.- Parameters:
info
-properties
-- Returns:
-
createEntityManager
A part of the facade over the JPA API Create an EntityManager from the EntityManagerFactory wrapped by this persistence context- Parameters:
tenantId
-- Returns:
-
createJAXBProperties
protected Map<String,Object> createJAXBProperties(org.eclipse.persistence.internal.sessions.AbstractSession session) throws IOException Build the set of properties used to create the JAXBContext based on the EntityManagerFactory that this PersistenceContext wraps- Parameters:
session
-- Returns:
- Throws:
IOException
-
delete
A part of the facade over the JPA API Delete the given entity in JPA and commit the changes -
doesExist
Does exist.- Parameters:
tenantId
- the tenant identity
- the entity- Returns:
- true, if successful
-
finalize
Finalize. -
find
A part of the facade over the JPA API Find an entity with the given name and id in JPA- Parameters:
entityName
-id
-- Returns:
-
find
A part of the facade over the JPA API Find an entity with the given name and id in JPA- Parameters:
tenantId
-entityName
-id
-- Returns:
-
find
public Object find(Map<String, String> tenantId, String entityName, Object id, Map<String, Object> properties) A part of the facade over the JPA API Find an entity with the given name and id in JPA- Parameters:
tenantId
-entityName
-id
-properties
- - query hints used on the find- Returns:
-
updateOrAddAttribute
public Object updateOrAddAttribute(Map<String, String> tenantId, String entityName, Object id, Map<String, Object> properties, String attribute, Object attributeValue, String partner) Update or add attribute.- Parameters:
tenantId
- the tenant identityName
- the entity nameid
- the idproperties
- the propertiesattribute
- the attributeattributeValue
- the attribute valuepartner
- the partner- Returns:
- the object
-
removeAttribute
public Object removeAttribute(Map<String, String> tenantId, String entityName, Object id, String attribute, String listItemId, Object entity, String partner) Removes the attribute.- Parameters:
tenantId
- the tenant identityName
- the entity nameid
- the idattribute
- the attributelistItemId
-entity
-partner
- the partner- Returns:
- the object
-
removeMappingValueFromObject
protected void removeMappingValueFromObject(Object object, Object attributeValue, DatabaseMapping mapping, DatabaseMapping partner) -
getBaseURI
Gets the base uri.- Returns:
- the base uri
-
getClass
Look-up the given entity name in the EntityManagerFactory and return the class is describes- Parameters:
entityName
-- Returns:
-
getServerSession
Gets the jpa server session.- Returns:
- the jpa server session
-
getClientSession
public org.eclipse.persistence.internal.sessions.AbstractSession getClientSession(jakarta.persistence.EntityManager em) Gets the client session.- Parameters:
em
- the em- Returns:
- the client session
-
getDescriptor
Lookup the descriptor for the given entity name. This method will look first in the EntityManagerFactory wrapped by this persistence context and return that descriptor. If one does not exist, it search the JAXBContext and return a descriptor from there.- Parameters:
entityName
-- Returns:
-
getDescriptorForClass
Gets the descriptor for class.- Parameters:
clazz
- the clazz- Returns:
- the descriptor for class
-
getJAXBDescriptorForClass
Gets the jAXB descriptor for class.- Parameters:
clazz
- the clazz- Returns:
- the jAXB descriptor for class
-
getEmf
public jakarta.persistence.EntityManagerFactory getEmf()Gets the emf.- Returns:
- the emf
-
getJAXBContext
Gets the jAXB context.- Returns:
- the jAXB context
-
getName
Gets the name.- Returns:
- the name
-
getSessionLog
-
merge
A part of the facade over the JPA API Call jpa merge on the given object and commit If the passed object is a list, we will iterate through the list and merge each member- Parameters:
tenantId
-entity
-- Returns:
-
newEntity
A convenience method to create a new dynamic entity of the given type- Parameters:
type
-- Returns:
-
newEntity
A convenience method to create a new dynamic entity of the given type- Parameters:
tenantId
-type
-- Returns:
-
queryExecuteUpdate
public int queryExecuteUpdate(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) Query execute update.- Parameters:
tenantId
- the tenant idname
- the nameparameters
- the parametershints
- the hints- Returns:
- the int
-
queryMultipleResults
public List queryMultipleResults(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) Query multiple results.- Parameters:
tenantId
- the tenant idname
- the nameparameters
- the parametershints
- the hints- Returns:
- the list
-
constructQuery
-
buildQuery
public jakarta.persistence.Query buildQuery(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) Builds the query.- Parameters:
tenantId
- the tenant idname
- the nameparameters
- the parametershints
- the hints- Returns:
- the query
-
setMappingValueInObject
protected void setMappingValueInObject(Object object, Object attributeValue, DatabaseMapping mapping, DatabaseMapping partner) -
stop
public void stop()Stop the current application instance -
toString
To string. -
unmarshalEntity
public Object unmarshalEntity(String type, jakarta.ws.rs.core.MediaType acceptedMediaType, InputStream in) throws jakarta.xml.bind.JAXBException Unmarshal entity.- Parameters:
type
- the type of the entity to unmarshalacceptedMediaType
- the accepted media typein
- the input stream to unmarshal- Returns:
- the object
- Throws:
jakarta.xml.bind.JAXBException
- the JAXB exception
-
unmarshal
public Object unmarshal(Class type, jakarta.ws.rs.core.MediaType acceptedMediaType, InputStream in) throws jakarta.xml.bind.JAXBException Unmarshal.- Parameters:
type
- the type of the entity to unmarshalacceptedMediaType
- the accepted media typein
- the input stream to unmarshal- Returns:
- the object
- Throws:
jakarta.xml.bind.JAXBException
- the JAXB exception
-
wrap
Make adjustments to an unmarshalled entity based on what is found in the weaved fields- Parameters:
entity
-- Returns:
-
marshallEntity
public void marshallEntity(Object object, jakarta.ws.rs.core.MediaType mediaType, OutputStream output) throws jakarta.xml.bind.JAXBException Marshall an entity to either JSON or XML Calling this method, will treat relationships as unfetched in the XML/JSON and marshall them as links rather than attempting to marshall the data in those relationships- Parameters:
object
-mediaType
-output
-- Throws:
jakarta.xml.bind.JAXBException
-
marshallEntity
public void marshallEntity(Object object, FieldsFilter filter, jakarta.ws.rs.core.MediaType mediaType, OutputStream output) throws jakarta.xml.bind.JAXBException Marshall an entity to either JSON or XML.- Parameters:
object
- the object to marshal.filter
- the filter (included/excluded fields) to use.mediaType
- the media type (XML/JSON).output
- the result.- Throws:
jakarta.xml.bind.JAXBException
-
marshall
public void marshall(Object object, jakarta.ws.rs.core.MediaType mediaType, OutputStream output, boolean sendRelationships) throws jakarta.xml.bind.JAXBException Marshall an entity to either JSON or XML.- Parameters:
object
-mediaType
-output
-sendRelationships
- if this is set to true, relationships will be sent as links instead of sending. the actual objects in the relationships- Throws:
jakarta.xml.bind.JAXBException
-
marshall
public void marshall(Object object, jakarta.ws.rs.core.MediaType mediaType, OutputStream output, boolean sendRelationships, FieldsFilter fieldsFilter) throws jakarta.xml.bind.JAXBException Marshall an entity to either JSON or XML.- Parameters:
object
- the object to marshal.mediaType
- the media type (XML/JSON).output
- the result.sendRelationships
- if this is set to true, relationships will be sent as links instead of sending the actual objects in the relationships.fieldsFilter
- Specifies fields to include/exclude from the response.- Throws:
jakarta.xml.bind.JAXBException
-
preMarshallEntity
Process an entity and add any additional data that needs to be added prior to marshalling This method will both single entities and lists of entities- Parameters:
object
-
-
preMarshallIndividualEntity
Add any data required prior to marshalling an entity to XML or JSON In general, this will only affect fields that have been weaved into the object- Parameters:
entity
-
-
postMarshallEntity
-
getAdapters
protected List<jakarta.xml.bind.annotation.adapters.XmlAdapter<?,?>> getAdapters() throws JPARSException- Throws:
JPARSException
-
getSupportedFeatureSet
Gets the supported feature set.- Returns:
- the supported feature set.
-
isQueryPageable
Finds out is given query pageable or not.- Parameters:
queryName
- named query to check.- Returns:
- true if pageable, false if not.
-
getPageableQuery
Gets REST pageable query details by query name.- Parameters:
queryName
- named query name.- Returns:
- RestPageableQuery or null if query couldn't be found.
-
setVersion
Sets the version.- Parameters:
version
- the new version.
-
setBaseURI
Sets the base uri.- Parameters:
baseURI
- the new base uri
-
getCollectionWrapperBuilder
Getter for the collectionWrapperBuilder property with lazy initialization.- Returns:
- the collectionWrapperBuilder.
-
hashCode
public int hashCode() -
equals
-