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.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected List<XmlAdapter<?, ?>> protected URIThe URI of the Persistence context.static final Stringprotected EntityManagerFactoryThe EntityManagerFactory used to interact using JPAprotected JAXBContextThe JAXBConext used to produce JSON or XMLstatic final Stringprotected StringThe name of the persistence context is used to look it up.static final String
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedPersistenceContext(String emfName, EntityManagerFactory emf, URI defaultURI) Instantiates a new persistence context.PersistenceContext(String emfName, EntityManagerFactory emf, URI defaultURI, ServiceVersion version) Instantiates a new persistence context.
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidaddDynamicXMLMetadataSources(List<Object> metadataSources, Session session) This method is used to help construct a JAXBContext from an existing EntityManagerFactory.Builds the query.protected QueryconstructQuery(EntityManager em, String name, Map<?, ?> parameters, Map<String, ?> hints) voidA part of the facade over the JPA API.protected JAXBContextcreateDynamicJAXBContext(Session session) Create a JAXBContext based on the EntityManagerFactory for this PersistenceContext.protected EntityManagercreateEntityManager(String tenantId) A part of the facade over the JPA API Create an EntityManager from the EntityManagerFactory wrapped by this persistence contextprotected EntityManagerFactorycreateEntityManagerFactory(PersistenceUnitInfo info, Map<String, ?> properties) A part of the facade over the JPA API.createJAXBProperties(Session session) Build the set of properties used to create the JAXBContext based on the EntityManagerFactory that this PersistenceContext wrapsvoidA part of the facade over the JPA API Delete the given entity in JPA and commit the changesbooleanDoes exist.booleanprotected voidfinalize()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<XmlAdapter<?, ?>> Gets the base uri.Class<?> Look-up the given entity name in the EntityManagerFactory and return the class is describesGets the client session.getDescriptor(String entityName) Lookup the descriptor for the given entity name.getDescriptorForClass(Class clazz) Gets the descriptor for class.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.inthashCode()booleanisQueryPageable(String queryName) Finds out is given query pageable or not.booleanChecks if is weaving enabled.voidmarshall(Object object, MediaType mediaType, OutputStream output, boolean sendRelationships) Marshall an entity to either JSON or XML.voidmarshall(Object object, MediaType mediaType, OutputStream output, boolean sendRelationships, FieldsFilter fieldsFilter) Marshall an entity to either JSON or XML.voidmarshallEntity(Object object, 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 relationshipsvoidmarshallEntity(Object object, FieldsFilter filter, 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 voidpostMarshallEntity(Object object) protected voidpreMarshallEntity(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 voidpreMarshallIndividualEntity(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 objectintqueryExecuteUpdate(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 voidremoveMappingValueFromObject(Object object, Object attributeValue, DatabaseMapping mapping, DatabaseMapping partner) voidsetBaseURI(URI baseURI) Sets the base uri.protected voidsetMappingValueInObject(Object object, Object attributeValue, DatabaseMapping mapping, DatabaseMapping partner) voidsetVersion(String version) Sets the version.voidstop()Stop the current application instancetoString()To string.unmarshal(Class type, MediaType acceptedMediaType, InputStream in) Unmarshal.unmarshalEntity(String type, 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 ObjectMake 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
- 
nameThe 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.
- 
emfThe EntityManagerFactory used to interact using JPA
- 
jaxbContextThe JAXBConext used to produce JSON or XML
- 
baseURIThe URI of the Persistence context. This is used to build Links in JSON and XML
 
- 
- 
Constructor Details- 
PersistenceContextprotected PersistenceContext()
- 
PersistenceContextInstantiates a new persistence context.- Parameters:
- emfName- the emf name
- emf- the emf
- defaultURI- the default uri
 
- 
PersistenceContextpublic PersistenceContext(String emfName, EntityManagerFactory emf, URI defaultURI, ServiceVersion version) Instantiates a new persistence context.- Parameters:
- emfName- the emf name
- emf- the emf
- defaultURI- the default uri
- version- REST service version
 
 
- 
- 
Method Details- 
isWeavingEnabledpublic boolean isWeavingEnabled()Checks if is weaving enabled.- Returns:
- true, if is weaving enabled
 
- 
getVersionGets the version as it appears in URI.- Returns:
- The version.
 
- 
getServiceVersionGets JPARS version.- Returns:
- JPARS version.
 
- 
addDynamicXMLMetadataSourcesThis 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.
- 
createA part of the facade over the JPA API. Persist an entity in JPA and commit.- Throws:
- Exception
 
- 
createDynamicJAXBContextCreate a JAXBContext based on the EntityManagerFactory for this PersistenceContext.- Throws:
- JAXBException
- IOException
 
- 
createEntityManagerFactoryprotected EntityManagerFactory createEntityManagerFactory(PersistenceUnitInfo info, Map<String, ?> properties) A part of the facade over the JPA API. Create an EntityManagerFactory using the given PersistenceUnitInfo and properties.
- 
createEntityManagerA part of the facade over the JPA API Create an EntityManager from the EntityManagerFactory wrapped by this persistence context
- 
createJAXBPropertiesBuild the set of properties used to create the JAXBContext based on the EntityManagerFactory that this PersistenceContext wraps- Throws:
- IOException
 
- 
deleteA part of the facade over the JPA API Delete the given entity in JPA and commit the changes
- 
doesExistDoes exist.- Parameters:
- tenantId- the tenant id
- entity- the entity
- Returns:
- true, if successful
 
- 
finalizeFinalize.
- 
findA part of the facade over the JPA API Find an entity with the given name and id in JPA
- 
findA part of the facade over the JPA API Find an entity with the given name and id in JPA
- 
findpublic 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:
- properties- - query hints used on the find
 
- 
updateOrAddAttributepublic 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 id
- entityName- the entity name
- id- the id
- properties- the properties
- attribute- the attribute
- attributeValue- the attribute value
- partner- the partner
- Returns:
- the object
 
- 
removeAttributepublic 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 id
- entityName- the entity name
- id- the id
- attribute- the attribute
- partner- the partner
- Returns:
- the object
 
- 
removeMappingValueFromObjectprotected void removeMappingValueFromObject(Object object, Object attributeValue, DatabaseMapping mapping, DatabaseMapping partner) 
- 
getBaseURIGets the base uri.- Returns:
- the base uri
 
- 
getClassLook-up the given entity name in the EntityManagerFactory and return the class is describes
- 
getServerSessionGets the jpa server session.- Returns:
- the jpa server session
 
- 
getClientSessionGets the client session.- Parameters:
- em- the em
- Returns:
- the client session
 
- 
getDescriptorLookup 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.
- 
getDescriptorForClassGets the descriptor for class.- Parameters:
- clazz- the clazz
- Returns:
- the descriptor for class
 
- 
getJAXBDescriptorForClassGets the jAXB descriptor for class.- Parameters:
- clazz- the clazz
- Returns:
- the jAXB descriptor for class
 
- 
getEmfGets the emf.- Returns:
- the emf
 
- 
getJAXBContextGets the jAXB context.- Returns:
- the jAXB context
 
- 
getNameGets the name.- Returns:
- the name
 
- 
getSessionLog
- 
mergeA 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
- 
newEntityA convenience method to create a new dynamic entity of the given type
- 
newEntityA convenience method to create a new dynamic entity of the given type
- 
queryExecuteUpdatepublic int queryExecuteUpdate(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) Query execute update.- Parameters:
- tenantId- the tenant id
- name- the name
- parameters- the parameters
- hints- the hints
- Returns:
- the int
 
- 
queryMultipleResultspublic List queryMultipleResults(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) Query multiple results.- Parameters:
- tenantId- the tenant id
- name- the name
- parameters- the parameters
- hints- the hints
- Returns:
- the list
 
- 
constructQuery
- 
buildQuerypublic Query buildQuery(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) Builds the query.- Parameters:
- tenantId- the tenant id
- name- the name
- parameters- the parameters
- hints- the hints
- Returns:
- the query
 
- 
setMappingValueInObjectprotected void setMappingValueInObject(Object object, Object attributeValue, DatabaseMapping mapping, DatabaseMapping partner) 
- 
stoppublic void stop()Stop the current application instance
- 
toStringTo string.
- 
unmarshalEntitypublic Object unmarshalEntity(String type, MediaType acceptedMediaType, InputStream in) throws JAXBException Unmarshal entity.- Parameters:
- type- the type of the entity to unmarshal
- acceptedMediaType- the accepted media type
- in- the input stream to unmarshal
- Returns:
- the object
- Throws:
- JAXBException- the JAXB exception
 
- 
unmarshalpublic Object unmarshal(Class type, MediaType acceptedMediaType, InputStream in) throws JAXBException Unmarshal.- Parameters:
- type- the type of the entity to unmarshal
- acceptedMediaType- the accepted media type
- in- the input stream to unmarshal
- Returns:
- the object
- Throws:
- JAXBException- the JAXB exception
 
- 
wrapMake adjustments to an unmarshalled entity based on what is found in the weaved fields
- 
marshallEntitypublic void marshallEntity(Object object, MediaType mediaType, OutputStream output) throws 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- Throws:
- JAXBException
 
- 
marshallEntitypublic void marshallEntity(Object object, FieldsFilter filter, MediaType mediaType, OutputStream output) throws 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:
- JAXBException
 
- 
marshallpublic void marshall(Object object, MediaType mediaType, OutputStream output, boolean sendRelationships) throws JAXBException Marshall an entity to either JSON or XML.- Parameters:
- sendRelationships- if this is set to true, relationships will be sent as links instead of sending. the actual objects in the relationships
- Throws:
- JAXBException
 
- 
marshallpublic void marshall(Object object, MediaType mediaType, OutputStream output, boolean sendRelationships, FieldsFilter fieldsFilter) throws 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:
- JAXBException
 
- 
preMarshallEntityProcess 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
- 
preMarshallIndividualEntityAdd 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
- 
postMarshallEntity
- 
getAdapters- Throws:
- JPARSException
 
- 
getSupportedFeatureSetGets the supported feature set.- Returns:
- the supported feature set.
 
- 
isQueryPageableFinds out is given query pageable or not.- Parameters:
- queryName- named query to check.
- Returns:
- true if pageable, false if not.
 
- 
getPageableQueryGets REST pageable query details by query name.- Parameters:
- queryName- named query name.
- Returns:
- RestPageableQuery or null if query couldn't be found.
 
- 
setVersionSets the version.- Parameters:
- version- the new version.
 
- 
setBaseURISets the base uri.- Parameters:
- baseURI- the new base uri
 
- 
hashCodepublic int hashCode()
- 
equals
 
-