use of org.eclipse.persistence.sessions.DatabaseSession in project eclipselink by eclipse-ee4j.
the class PersistenceContext method 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.
*/
public ClassDescriptor getDescriptor(String entityName) {
DatabaseSession session = getServerSession();
ClassDescriptor descriptor = session.getDescriptorForAlias(entityName);
if (descriptor == null) {
for (Object ajaxBSession : getJAXBContext().getXMLContext().getSessions()) {
descriptor = ((Session) ajaxBSession).getClassDescriptorForAlias(entityName);
if (descriptor != null) {
break;
}
}
}
return descriptor;
}
use of org.eclipse.persistence.sessions.DatabaseSession in project eclipselink by eclipse-ee4j.
the class AuctionPersistenceContextTests method verifyJPAConfig.
@Test
public void verifyJPAConfig() {
assertNotNull(context);
EntityManagerFactory emf = context.getEmf();
assertNotNull(emf);
DatabaseSession session = JpaHelper.getServerSession(emf);
assertEquals(3, session.getDescriptors().size());
}
use of org.eclipse.persistence.sessions.DatabaseSession in project eclipselink by eclipse-ee4j.
the class AbstractEntityResource method findAttributeInternal.
protected Response findAttributeInternal(String version, String persistenceUnit, String type, String id, String attribute, HttpHeaders headers, UriInfo uriInfo) {
JPARSLogger.entering(CLASS_NAME, "findAttributeInternal", new Object[] { "GET", version, persistenceUnit, type, id, attribute, uriInfo.getRequestUri().toASCIIString() });
EntityManager em = null;
try {
PersistenceContext context = getPersistenceContext(persistenceUnit, type, uriInfo.getBaseUri(), version, null);
Object entityId = IdHelper.buildId(context, type, id);
em = context.getEmf().createEntityManager(getMatrixParameters(uriInfo, persistenceUnit));
Object entity = em.find(context.getClass(type), entityId, getQueryParameters(uriInfo));
DatabaseSession serverSession = context.getServerSession();
ClassDescriptor descriptor = serverSession.getClassDescriptor(context.getClass(type));
if (descriptor == null) {
throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(type, persistenceUnit);
}
DatabaseMapping attributeMapping = descriptor.getMappingForAttributeName(attribute);
if ((attributeMapping == null) || (entity == null)) {
throw JPARSException.databaseMappingCouldNotBeFoundForEntityAttribute(attribute, type, id, persistenceUnit);
}
if (!attributeMapping.isCollectionMapping()) {
Object result = attributeMapping.getRealAttributeValueFromAttribute(attributeMapping.getAttributeValueFromObject(entity), entity, (AbstractSession) serverSession);
if (result == null) {
JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_entity_for_attribute", new Object[] { attribute, type, id, persistenceUnit });
throw JPARSException.attributeCouldNotBeFoundForEntity(attribute, type, id, persistenceUnit);
}
final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, getQueryParameters(uriInfo), headers, uriInfo, responseBuilder, null);
}
ReadQuery query = (ReadQuery) ((((ForeignReferenceMapping) attributeMapping).getSelectionQuery()).clone());
if (query == null) {
throw JPARSException.selectionQueryForAttributeCouldNotBeFoundForEntity(attribute, type, id, persistenceUnit);
}
final FeatureSet featureSet = context.getSupportedFeatureSet();
final AbstractSession clientSession = context.getClientSession(em);
// Fields filtering
FieldsFilter fieldsFilter = null;
if (context.getSupportedFeatureSet().isSupported(Feature.FIELDS_FILTERING)) {
final FieldsFilteringValidator fieldsFilteringValidator = new FieldsFilteringValidator(uriInfo);
if (fieldsFilteringValidator.isFeatureApplicable()) {
fieldsFilter = fieldsFilteringValidator.getFilter();
}
}
// Pagination
if (featureSet.isSupported(Feature.PAGING)) {
final PageableFieldValidator validator = new PageableFieldValidator(entity.getClass(), attribute, uriInfo);
if (validator.isFeatureApplicable()) {
// Adding extra one to detect are there more rows or not. It will be removed later
// on in response processor.
query.setMaxRows(validator.getLimit() + validator.getOffset() + 1);
query.setFirstResult(validator.getOffset());
// We need to add limit and offset to query parameters because request builder reads it from there
final Map<String, Object> queryParams = getQueryParameters(uriInfo);
queryParams.put(QueryParameters.JPARS_PAGING_LIMIT, String.valueOf(validator.getLimit()));
queryParams.put(QueryParameters.JPARS_PAGING_OFFSET, String.valueOf(validator.getOffset()));
// check orderBy, and generate a warning if there is none
checkOrderBy(context, query);
final Object result = clientSession.executeQuery(query, descriptor.getObjectBuilder().buildRow(entity, clientSession, WriteType.INSERT));
final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.PAGING);
return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, queryParams, headers, uriInfo, responseBuilder, fieldsFilter);
}
}
final Object result = clientSession.executeQuery(query, descriptor.getObjectBuilder().buildRow(entity, clientSession, WriteType.INSERT));
final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, getQueryParameters(uriInfo), headers, uriInfo, responseBuilder, fieldsFilter);
} catch (Exception ex) {
throw JPARSException.exceptionOccurred(ex);
} finally {
if (em != null) {
if (em.isOpen()) {
em.close();
}
}
}
}
use of org.eclipse.persistence.sessions.DatabaseSession in project eclipselink by eclipse-ee4j.
the class SQLAsCollectionTestSuite method buildSessions.
@Override
public void buildSessions() {
XRDynamicClassLoader xrdecl = new XRDynamicClassLoader(parentClassLoader);
DatasourceLogin login = new DatabaseLogin();
login.setUserName(username);
login.setPassword(password);
((DatabaseLogin) login).setConnectionString(url);
((DatabaseLogin) login).setDriverClassName(DATABASE_PLATFORM);
Platform platform = builder.getDatabasePlatform();
ConversionManager conversionManager = platform.getConversionManager();
if (conversionManager != null) {
conversionManager.setLoader(xrdecl);
}
login.setDatasourcePlatform(platform);
((DatabaseLogin) login).bindAllParameters();
((DatabaseLogin) login).setUsesStreamsForBinding(true);
Project orProject = null;
if (DBWS_OR_STREAM.size() != 0) {
MetadataProcessor processor = new MetadataProcessor(new XRPersistenceUnitInfo(xrdecl), new DatabaseSessionImpl(login), xrdecl, false, true, false, false, false, null, null);
processor.setMetadataSource(new JPAMetadataSource(xrdecl, new StringReader(DBWS_OR_STREAM.toString())));
PersistenceUnitProcessor.processORMetadata(processor, true, PersistenceUnitProcessor.Mode.ALL);
processor.addNamedQueries();
orProject = processor.getProject().getProject();
} else {
orProject = new Project();
}
orProject.setName(builder.getProjectName().concat(OR_PRJ_SUFFIX));
orProject.setDatasourceLogin(login);
DatabaseSession databaseSession = orProject.createDatabaseSession();
if ("off".equalsIgnoreCase(builder.getLogLevel())) {
databaseSession.dontLogMessages();
} else {
databaseSession.setLogLevel(AbstractSessionLog.translateStringToLoggingLevel(builder.getLogLevel()));
}
xrService.setORSession(databaseSession);
orProject.convertClassNamesToClasses(xrdecl);
Project oxProject = null;
Map<String, OXMMetadataSource> metadataMap = new HashMap<String, OXMMetadataSource>();
StreamSource xml = new StreamSource(new StringReader(DBWS_OX_STREAM.toString()));
try {
JAXBContext jc = JAXBContext.newInstance(XmlBindingsModel.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<XmlBindingsModel> jaxbElt = unmarshaller.unmarshal(xml, XmlBindingsModel.class);
XmlBindingsModel model = jaxbElt.getValue();
for (XmlBindings xmlBindings : model.getBindingsList()) {
metadataMap.put(xmlBindings.getPackageName(), new OXMMetadataSource(xmlBindings));
}
} catch (JAXBException jaxbex) {
jaxbex.printStackTrace();
}
Map<String, Map<String, OXMMetadataSource>> properties = new HashMap<String, Map<String, OXMMetadataSource>>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataMap);
try {
org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext jCtx = org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromOXM(parentClassLoader, properties);
oxProject = jCtx.getXMLContext().getSession(0).getProject();
} catch (JAXBException e) {
e.printStackTrace();
}
((XMLLogin) oxProject.getDatasourceLogin()).setPlatformClassName(DOM_PLATFORM_CLASSNAME);
((XMLLogin) oxProject.getDatasourceLogin()).setEqualNamespaceResolvers(false);
prepareDescriptors(oxProject, orProject, xrdecl);
ProjectHelper.fixOROXAccessors(orProject, oxProject);
xrService.setORSession(databaseSession);
xrService.setXMLContext(new XMLContext(oxProject));
xrService.setOXSession(xrService.getXMLContext().getSession(0));
}
use of org.eclipse.persistence.sessions.DatabaseSession in project eclipselink by eclipse-ee4j.
the class SecondarySQLTestSuite method buildSessions.
@Override
public void buildSessions() {
XRDynamicClassLoader xrdecl = new XRDynamicClassLoader(parentClassLoader);
DatasourceLogin login = new DatabaseLogin();
login.setUserName(username);
login.setPassword(password);
((DatabaseLogin) login).setConnectionString(url);
((DatabaseLogin) login).setDriverClassName(DATABASE_PLATFORM);
Platform platform = builder.getDatabasePlatform();
ConversionManager conversionManager = platform.getConversionManager();
if (conversionManager != null) {
conversionManager.setLoader(xrdecl);
}
login.setDatasourcePlatform(platform);
((DatabaseLogin) login).bindAllParameters();
((DatabaseLogin) login).setUsesStreamsForBinding(true);
Project orProject = null;
if (DBWS_OR_STREAM.size() != 0) {
MetadataProcessor processor = new MetadataProcessor(new XRPersistenceUnitInfo(xrdecl), new DatabaseSessionImpl(login), xrdecl, false, true, false, false, false, null, null);
processor.setMetadataSource(new JPAMetadataSource(xrdecl, new StringReader(DBWS_OR_STREAM.toString())));
PersistenceUnitProcessor.processORMetadata(processor, true, PersistenceUnitProcessor.Mode.ALL);
processor.addNamedQueries();
orProject = processor.getProject().getProject();
} else {
orProject = new Project();
}
orProject.setName(builder.getProjectName().concat(OR_PRJ_SUFFIX));
orProject.setDatasourceLogin(login);
DatabaseSession databaseSession = orProject.createDatabaseSession();
if ("off".equalsIgnoreCase(builder.getLogLevel())) {
databaseSession.dontLogMessages();
} else {
databaseSession.setLogLevel(AbstractSessionLog.translateStringToLoggingLevel(builder.getLogLevel()));
}
xrService.setORSession(databaseSession);
orProject.convertClassNamesToClasses(xrdecl);
Project oxProject = null;
Map<String, OXMMetadataSource> metadataMap = new HashMap<String, OXMMetadataSource>();
StreamSource xml = new StreamSource(new StringReader(DBWS_OX_STREAM.toString()));
try {
JAXBContext jc = JAXBContext.newInstance(XmlBindingsModel.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<XmlBindingsModel> jaxbElt = unmarshaller.unmarshal(xml, XmlBindingsModel.class);
XmlBindingsModel model = jaxbElt.getValue();
for (XmlBindings xmlBindings : model.getBindingsList()) {
metadataMap.put(xmlBindings.getPackageName(), new OXMMetadataSource(xmlBindings));
}
} catch (JAXBException jaxbex) {
jaxbex.printStackTrace();
}
Map<String, Map<String, OXMMetadataSource>> properties = new HashMap<String, Map<String, OXMMetadataSource>>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataMap);
try {
org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext jCtx = org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromOXM(parentClassLoader, properties);
oxProject = jCtx.getXMLContext().getSession(0).getProject();
} catch (JAXBException e) {
e.printStackTrace();
}
((XMLLogin) oxProject.getDatasourceLogin()).setPlatformClassName(DOM_PLATFORM_CLASSNAME);
((XMLLogin) oxProject.getDatasourceLogin()).setEqualNamespaceResolvers(false);
prepareDescriptors(oxProject, orProject, xrdecl);
ProjectHelper.fixOROXAccessors(orProject, oxProject);
xrService.setORSession(databaseSession);
xrService.setXMLContext(new XMLContext(oxProject));
xrService.setOXSession(xrService.getXMLContext().getSession(0));
}
Aggregations