Package org.eclipse.persistence.jpa.rs
Class PersistenceContext
- java.lang.Object
-
- org.eclipse.persistence.jpa.rs.PersistenceContext
-
public class PersistenceContext extends Object
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
Fields Modifier and Type Field Description static StringCLASS_NAMEstatic StringJPARS_CONTEXTstatic StringSESSION_VERSION_PROPERTY
-
Constructor Summary
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description javax.persistence.QuerybuildQuery(Map<String,String> tenantId, String name, Map<?,?> parameters, Map<String,?> hints)Builds the query.voidcreate(Map<String,String> tenantId, Object entity)A part of the facade over the JPA API.voiddelete(Map<String,String> tenantId, String type, Object id)A part of the facade over the JPA API Delete the given entity in JPA and commit the changesbooleandoesExist(Map<String,String> tenantId, Object entity)Does exist.booleanequals(Object other)voidfinalize()Finalize.Objectfind(String entityName, Object id)A part of the facade over the JPA API Find an entity with the given name and id in JPAObjectfind(Map<String,String> tenantId, String entityName, Object id)A part of the facade over the JPA API Find an entity with the given name and id in JPAObjectfind(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 JPAURIgetBaseURI()Gets the base uri.Class<?>getClass(String entityName)Look-up the given entity name in the EntityManagerFactory and return the class is describesorg.eclipse.persistence.internal.sessions.AbstractSessiongetClientSession(javax.persistence.EntityManager em)Gets the client session.CollectionWrapperBuildergetCollectionWrapperBuilder()Getter for the collectionWrapperBuilder property with lazy initialization.ClassDescriptorgetDescriptor(String entityName)Lookup the descriptor for the given entity name.ClassDescriptorgetDescriptorForClass(Class clazz)Gets the descriptor for class.javax.persistence.EntityManagerFactorygetEmf()Gets the emf.JAXBContextgetJAXBContext()Gets the jAXB context.ClassDescriptorgetJAXBDescriptorForClass(Class clazz)Gets the jAXB descriptor for class.StringgetName()Gets the name.RestPageableQuerygetPageableQuery(String queryName)Gets REST pageable query details by query name.DatabaseSessiongetServerSession()Gets the jpa server session.ServiceVersiongetServiceVersion()Gets JPARS version.SessionLoggetSessionLog()FeatureSetgetSupportedFeatureSet()Gets the supported feature set.StringgetVersion()Gets the version as it appears in URI.inthashCode()booleanisQueryPageable(String queryName)Finds out is given query pageable or not.booleanisWeavingEnabled()Checks if is weaving enabled.voidmarshall(Object object, javax.ws.rs.core.MediaType mediaType, OutputStream output, boolean sendRelationships)Marshall an entity to either JSON or XML.voidmarshall(Object object, javax.ws.rs.core.MediaType mediaType, OutputStream output, boolean sendRelationships, FieldsFilter fieldsFilter)Marshall an entity to either JSON or XML.voidmarshallEntity(Object object, javax.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 relationshipsvoidmarshallEntity(Object object, FieldsFilter filter, javax.ws.rs.core.MediaType mediaType, OutputStream output)Marshall an entity to either JSON or XML.Objectmerge(Map<String,String> tenantId, Object entity)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 memberDynamicEntitynewEntity(String type)A convenience method to create a new dynamic entity of the given typeDynamicEntitynewEntity(Map<String,String> tenantId, String type)A convenience method to create a new dynamic entity of the given typeintqueryExecuteUpdate(Map<String,String> tenantId, String name, Map<?,?> parameters, Map<String,?> hints)Query execute update.ListqueryMultipleResults(Map<String,String> tenantId, String name, Map<?,?> parameters, Map<String,?> hints)Query multiple results.ObjectremoveAttribute(Map<String,String> tenantId, String entityName, Object id, String attribute, String listItemId, Object entity, String partner)Removes the attribute.voidsetBaseURI(URI baseURI)Sets the base uri.voidsetVersion(String version)Sets the version.voidstop()Stop the current application instanceStringtoString()To string.Objectunmarshal(Class type, javax.ws.rs.core.MediaType acceptedMediaType, InputStream in)Unmarshal.ObjectunmarshalEntity(String type, javax.ws.rs.core.MediaType acceptedMediaType, InputStream in)Unmarshal entity.ObjectupdateOrAddAttribute(Map<String,String> tenantId, String entityName, Object id, Map<String,Object> properties, String attribute, Object attributeValue, String partner)Update or add attribute.
-
-
-
Field Detail
-
JPARS_CONTEXT
public static final String JPARS_CONTEXT
- See Also:
- Constant Field Values
-
CLASS_NAME
public static final String CLASS_NAME
-
SESSION_VERSION_PROPERTY
public static final String SESSION_VERSION_PROPERTY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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 Detail
-
isWeavingEnabled
public boolean isWeavingEnabled()
Checks if is weaving enabled.- Returns:
- true, if is weaving enabled
-
getVersion
public String getVersion()
Gets the version as it appears in URI.- Returns:
- The version.
-
getServiceVersion
public ServiceVersion getServiceVersion()
Gets JPARS version.- Returns:
- JPARS version.
-
create
public void create(Map<String,String> tenantId, Object entity) throws Exception
A part of the facade over the JPA API. Persist an entity in JPA and commit.- Parameters:
tenantId-entity-- Throws:
Exception
-
delete
public void delete(Map<String,String> tenantId, String type, Object id)
A part of the facade over the JPA API Delete the given entity in JPA and commit the changes
-
doesExist
public boolean doesExist(Map<String,String> tenantId, Object entity)
Does exist.- Parameters:
tenantId- the tenant identity- the entity- Returns:
- true, if successful
-
finalize
public void finalize()
Finalize.
-
find
public Object find(String entityName, Object id)
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
public Object find(Map<String,String> tenantId, String entityName, Object id)
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
-
getBaseURI
public URI getBaseURI()
Gets the base uri.- Returns:
- the base uri
-
getClass
public Class<?> getClass(String entityName)
Look-up the given entity name in the EntityManagerFactory and return the class is describes- Parameters:
entityName-- Returns:
-
getServerSession
public DatabaseSession getServerSession()
Gets the jpa server session.- Returns:
- the jpa server session
-
getClientSession
public org.eclipse.persistence.internal.sessions.AbstractSession getClientSession(javax.persistence.EntityManager em)
Gets the client session.- Parameters:
em- the em- Returns:
- the client session
-
getDescriptor
public ClassDescriptor getDescriptor(String entityName)
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
public ClassDescriptor getDescriptorForClass(Class clazz)
Gets the descriptor for class.- Parameters:
clazz- the clazz- Returns:
- the descriptor for class
-
getJAXBDescriptorForClass
public ClassDescriptor getJAXBDescriptorForClass(Class clazz)
Gets the jAXB descriptor for class.- Parameters:
clazz- the clazz- Returns:
- the jAXB descriptor for class
-
getEmf
public javax.persistence.EntityManagerFactory getEmf()
Gets the emf.- Returns:
- the emf
-
getJAXBContext
public JAXBContext getJAXBContext()
Gets the jAXB context.- Returns:
- the jAXB context
-
getName
public String getName()
Gets the name.- Returns:
- the name
-
getSessionLog
public SessionLog getSessionLog()
-
merge
public Object merge(Map<String,String> tenantId, Object entity)
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
public DynamicEntity newEntity(String type)
A convenience method to create a new dynamic entity of the given type- Parameters:
type-- Returns:
-
newEntity
public DynamicEntity newEntity(Map<String,String> tenantId, String type)
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
-
buildQuery
public javax.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
-
stop
public void stop()
Stop the current application instance
-
toString
public String toString()
To string.
-
unmarshalEntity
public Object unmarshalEntity(String type, javax.ws.rs.core.MediaType acceptedMediaType, InputStream in) throws javax.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:
javax.xml.bind.JAXBException- the JAXB exception
-
unmarshal
public Object unmarshal(Class type, javax.ws.rs.core.MediaType acceptedMediaType, InputStream in) throws javax.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:
javax.xml.bind.JAXBException- the JAXB exception
-
marshallEntity
public void marshallEntity(Object object, javax.ws.rs.core.MediaType mediaType, OutputStream output) throws javax.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:
javax.xml.bind.JAXBException
-
marshallEntity
public void marshallEntity(Object object, FieldsFilter filter, javax.ws.rs.core.MediaType mediaType, OutputStream output) throws javax.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:
javax.xml.bind.JAXBException
-
marshall
public void marshall(Object object, javax.ws.rs.core.MediaType mediaType, OutputStream output, boolean sendRelationships) throws javax.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:
javax.xml.bind.JAXBException
-
marshall
public void marshall(Object object, javax.ws.rs.core.MediaType mediaType, OutputStream output, boolean sendRelationships, FieldsFilter fieldsFilter) throws javax.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:
javax.xml.bind.JAXBException
-
getSupportedFeatureSet
public FeatureSet getSupportedFeatureSet()
Gets the supported feature set.- Returns:
- the supported feature set.
-
isQueryPageable
public boolean isQueryPageable(String queryName)
Finds out is given query pageable or not.- Parameters:
queryName- named query to check.- Returns:
- true if pageable, false if not.
-
getPageableQuery
public RestPageableQuery getPageableQuery(String queryName)
Gets REST pageable query details by query name.- Parameters:
queryName- named query name.- Returns:
- RestPageableQuery or null if query couldn't be found.
-
setVersion
public void setVersion(String version)
Sets the version.- Parameters:
version- the new version.
-
setBaseURI
public void setBaseURI(URI baseURI)
Sets the base uri.- Parameters:
baseURI- the new base uri
-
getCollectionWrapperBuilder
public CollectionWrapperBuilder getCollectionWrapperBuilder()
Getter for the collectionWrapperBuilder property with lazy initialization.- Returns:
- the collectionWrapperBuilder.
-
-