Search in sources :

Example 26 with DatabaseSessionImpl

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);
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)

Example 27 with DatabaseSessionImpl

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)));
}
Also used : Torso(org.eclipse.persistence.testing.models.jpa.complexaggregate.Torso) Heart(org.eclipse.persistence.testing.models.jpa.complexaggregate.Heart) EntityManager(jakarta.persistence.EntityManager) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) PersistenceUnitUtil(jakarta.persistence.PersistenceUnitUtil) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) Body(org.eclipse.persistence.testing.models.jpa.complexaggregate.Body) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException)

Example 28 with DatabaseSessionImpl

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);
}
Also used : EntityManager(jakarta.persistence.EntityManager) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) Place(org.eclipse.persistence.testing.models.jpa.complexaggregate.Place) Location(org.eclipse.persistence.testing.models.jpa.complexaggregate.Location)

Example 29 with DatabaseSessionImpl

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);
        }
    }
}
Also used : XMLFragmentCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLFragmentCollectionMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) XMLSchemaURLReference(org.eclipse.persistence.oxm.schema.XMLSchemaURLReference) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) SimpleXMLFormatModel(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatModel) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session)

Example 30 with DatabaseSessionImpl

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);
            }
        }
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) QName(javax.xml.namespace.QName) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) InputStream(java.io.InputStream) DataHandler(jakarta.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session)

Aggregations

DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)64 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)18 EntityManager (jakarta.persistence.EntityManager)15 HashMap (java.util.HashMap)14 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)14 Map (java.util.Map)13 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)13 Project (org.eclipse.persistence.sessions.Project)12 StringReader (java.io.StringReader)9 StreamSource (javax.xml.transform.stream.StreamSource)9 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)9 MetadataProcessor (org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor)9 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)9 XMLContext (org.eclipse.persistence.oxm.XMLContext)9 DatasourceLogin (org.eclipse.persistence.sessions.DatasourceLogin)9 JAXBContext (jakarta.xml.bind.JAXBContext)8 JAXBException (jakarta.xml.bind.JAXBException)8 Unmarshaller (jakarta.xml.bind.Unmarshaller)8 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)8 XRDynamicClassLoader (org.eclipse.persistence.internal.xr.XRDynamicClassLoader)8