use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.
the class EntityManagerJUnitTestSuite method testTransientMapping.
public void testTransientMapping() {
DatabaseSessionImpl session = getDatabaseSession();
ClassDescriptor descriptor = session.getClassDescriptor(Customer.class);
assertTrue("There should not be a mapping for transientField.", descriptor.getMappingForAttributeName("transientField") == null);
}
use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.
the class ComplexAggregateTestSuite method testNestedAggregatePrimaryKey.
public void testNestedAggregatePrimaryKey() {
clearCache();
Torso torso;
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = createEntityManager();
try {
Body body = new Body();
torso = new Torso();
Heart heart = new Heart();
heart.setSize(8);
torso.setHeart(heart);
body.setTorso(torso);
beginTransaction(em);
em.persist(body);
commitTransaction(em);
} catch (Exception e) {
throw new TestErrorException("Exception caught when persisting the new body: " + e.getMessage());
} finally {
closeEntityManager(em);
}
em = createEntityManager();
Exception m_testException = null;
DatabaseSessionImpl m_session = getDatabaseSession();
Body m_refreshedBody = null;
// Try to read the body back, clear the cache first.
try {
em.clear();
m_session.getIdentityMapAccessor().initializeAllIdentityMaps();
m_refreshedBody = em.find(Body.class, torso);
} catch (Exception e) {
m_testException = e;
}
if (m_testException != null) {
throw new TestErrorException("Exception caught reading back the persisted body: " + m_testException);
}
if (m_refreshedBody == null) {
throw new TestErrorException("Unable to read back the persisted body");
}
ClassDescriptor descriptor = getDatabaseSession().getClassDescriptor(Body.class);
Object pks = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(m_refreshedBody, m_session);
Torso createdTorso = (Torso) descriptor.getCMPPolicy().createPrimaryKeyInstanceFromId(pks, m_session);
assertTrue("PK's do not match.", m_refreshedBody.getTorso().equals(createdTorso));
PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
assertTrue("PersistenceUnitUtil returned incorrect identifier", torso.equals(util.getIdentifier(m_refreshedBody)));
}
use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.
the class ComplexAggregateTestSuite method testAggregateFieldAttributeOverrides.
public void testAggregateFieldAttributeOverrides() {
clearCache();
DatabaseSessionImpl m_session = getDatabaseSession();
EntityManager em = createEntityManager();
beginTransaction(em);
Place place = new Place();
place.setCountryCode("US");
place.setName("Nowhere");
place.setAddress1(new Location("12 Main Street", "US"));
place.setAddress2(new Location("34 Easy Street", "US"));
em.persist(place);
em.flush();
rollbackTransaction(em);
}
use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.
the class QueryOperation method addSimpleXMLFormatModelDescriptor.
protected void addSimpleXMLFormatModelDescriptor(XRServiceAdapter xrService) {
if (isSimpleXMLFormat()) {
Session oxSession = xrService.getOXSession();
XMLDescriptor simpleXMLFormatDescriptor = (XMLDescriptor) oxSession.getProject().getClassDescriptor(SimpleXMLFormatModel.class);
if (simpleXMLFormatDescriptor == null) {
simpleXMLFormatDescriptor = new XMLDescriptor();
simpleXMLFormatDescriptor.setJavaClass(SimpleXMLFormatModel.class);
simpleXMLFormatDescriptor.setAlias(DEFAULT_SIMPLE_XML_FORMAT_TAG);
simpleXMLFormatDescriptor.setDefaultRootElement(DEFAULT_SIMPLE_XML_FORMAT_TAG);
XMLFragmentCollectionMapping xmlTag = new XMLFragmentCollectionMapping();
xmlTag.setAttributeName(SIMPLEXML_STR);
xmlTag.setXPath(DEFAULT_SIMPLE_XML_TAG);
simpleXMLFormatDescriptor.addMapping(xmlTag);
NamespaceResolver nr = new NamespaceResolver();
simpleXMLFormatDescriptor.setNamespaceResolver(nr);
XMLSchemaURLReference schemaReference = new XMLSchemaURLReference(EMPTY_STR);
schemaReference.setSchemaContext(SIMPLEXML_FORMAT_STR);
schemaReference.setType(XMLSchemaReference.COMPLEX_TYPE);
simpleXMLFormatDescriptor.setSchemaReference(schemaReference);
oxSession.getProject().addDescriptor(simpleXMLFormatDescriptor);
((DatabaseSessionImpl) oxSession).initializeDescriptorIfSessionAlive(simpleXMLFormatDescriptor);
xrService.getXMLContext().storeXMLDescriptorByQName(simpleXMLFormatDescriptor);
}
}
}
use of org.eclipse.persistence.internal.sessions.DatabaseSessionImpl in project eclipselink by eclipse-ee4j.
the class QueryOperation method initialize.
@Override
public void initialize(XRServiceAdapter xrService) {
super.initialize(xrService);
if (queryHandler == null) {
// session query instead of named query
DatabaseQuery dq = xrService.getORSession().getQuery(name);
if (dq != null) {
queryHandler = new QueryHandler() {
@Override
public void initializeDatabaseQuery(XRServiceAdapter xrService, QueryOperation queryOperation) {
// do nothing
}
@Override
public void initializeArguments(XRServiceAdapter xrService, QueryOperation queryOperation, DatabaseQuery databaseQuery) {
// do nothing
}
@Override
public void initializeCall(XRServiceAdapter xrService, QueryOperation queryOperation, DatabaseQuery databaseQuery) {
// do nothing
}
};
queryHandler.setDatabaseQuery(dq);
}
}
if (queryHandler == null) {
throw DBWSException.couldNotLocateQueryForSession(name, xrService.getORSession().getName());
}
queryHandler.initialize(xrService, this);
Session oxSession = xrService.getOXSession();
QName resultType = result == null ? null : result.getType();
addSimpleXMLFormatModelDescriptor(xrService);
addValueObjectDescriptor(xrService);
if (resultType == null) {
if (isAttachment()) {
Attachment attachment = result.getAttachment();
XMLDescriptor descriptor = (XMLDescriptor) oxSession.getProject().getClassDescriptor(DataHandler.class);
if (descriptor == null) {
descriptor = new XMLDescriptor();
descriptor.setAlias(DATAHANDLER_STR);
descriptor.setJavaClass(DataHandler.class);
descriptor.setInstantiationPolicy(new DataHandlerInstantiationPolicy(attachment.getMimeType()));
XMLBinaryDataMapping mapping = new XMLBinaryDataMapping();
mapping.setAttributeName(RESULTS_STR);
mapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object) throws DescriptorException {
Object result = null;
DataHandler dataHandler = (DataHandler) object;
try {
result = dataHandler.getContent();
if (result instanceof InputStream) {
try (InputStream is = (InputStream) result) {
byte[] buf = new byte[2048];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead = is.read(buf);
while (bytesRead >= 0) {
baos.write(buf, 0, bytesRead);
bytesRead = is.read(buf);
}
result = baos.toByteArray();
}
}
} catch (IOException e) {
// ignore
}
return result;
}
@Override
public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
}
});
mapping.setXPath(DEFAULT_SIMPLE_XML_FORMAT_TAG + SLASH_CHAR + DEFAULT_SIMPLE_XML_TAG + ATTACHMENT_STR);
mapping.setSwaRef(true);
mapping.setShouldInlineBinaryData(false);
mapping.setMimeType(attachment.getMimeType());
descriptor.addMapping(mapping);
NamespaceResolver nr = new NamespaceResolver();
descriptor.setNamespaceResolver(nr);
oxSession.getProject().addDescriptor(descriptor);
((DatabaseSessionImpl) oxSession).initializeDescriptorIfSessionAlive(descriptor);
xrService.getXMLContext().storeXMLDescriptorByQName(descriptor);
}
}
}
}
Aggregations