Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/SDO/JPA

< EclipseLink‎ | Examples‎ | SDO
Revision as of 13:02, 30 January 2009 by Blaise.doughan.oracle.com (Talk | contribs) (New page: ==Querying== When developing a service that will return SDO DataObject instances from the relational database the developer will issue queries against the database using JPA (PK Find, JP ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Querying

When developing a service that will return SDO DataObject instances from the relational database the developer will issue queries against the database using JPA (PK Find, JP QL or Native ORM queries in JPA 1.0). The resulting entities will then be wrapped by DataObject using a provided helper class.

EntityManagerFactory emf = Persistence.createEntityManagerFactory("CustomerExample");      
EntityManager em = emf.createEntityManager();
List<MyEntity> entities = em.createQuery("SELECT e FROM MyEntity e WHERE ...").getResultList();
 
JAXBContext jaxbContext = JAXBContext.newInstance("com.example.customer");
JAXBHelperContext jaxbHelperContext = new JAXBHelperConext(jaxbContext);
jaxbHelperContext.getXSDHelper().define(customerXSD);
 
List<DataObject> dataObjects = jaxbHelperContext.wrap(entities);

Creating (JPA persist)

Creating new entities in the database can be done by explicitly requesting the new data to be persisted. If the DataObject being persisted combines new objects with updates to existing objects then the Updating scenario below will be used.

EntityManagerFactory emf = Persistence.createEntityManagerFactory("CustomerExample");      
EntityManager em = emf.createEntityManager();
 
JAXBContext jaxbContext = JAXBContext.newInstance("com.example.customer");
JAXBHelperContext jaxbHelperContext = new JAXBHelperConext(jaxbContext);
jaxbHelperContext.getXSDHelper().define(customerXSD);
 
Type customerType = jaxbHelperContext.getType(Customer.class);
DataObject customerDO = jaxbHelperContext.getDataFactory().create(customerType);
customerDO.setString("firstName", "Jane");
customerDO.setString("lastName", "Doe");
DataObject addressDO customerDO.create("address");
addressDO.setString("street", "123 Any Street");
addressDO.setString("city", "Any Town");
 
em.getTransaction().begin();
em.persist(jaxbHelperContext.unwrap(customerDO));
em.getTransaction().commit();

Note: The cascade persist configuration on the underlying entity's relationship mappings will define the scope of which new instances in the graph will be persisted.

Updating (JPA merge)

In the case of updates to existing entities as well as addition of new entities through relationships the merge call on the helper will be used.

EntityManagerFactory emf = Persistence.createEntityManagerFactory("CustomerExample");      
EntityManager em = emf.createEntityManager();
 
JAXBContext jaxbContext = JAXBContext.newInstance("com.example.customer");
JAXBHelperContext jaxbHelperContext = new JAXBHelperConext(jaxbContext);
jaxbHelperContext.getXSDHelper().define(customerXSD);
 
em.getTransaction().begin();
em.merge(jaxbHelperContext.unwrap(dataObject));
// ... Other operations within the same TX
em.getTransaction().commit();

Note: The creation of new objects and the removal of existing objects in the graph result in database operations based on the cascade and private-owned configurations in the JPA model's relationship mappings.

Deleting (JPA remove)

Entities can be removed from the database using the helper's remove call:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("CustomerExample");      
EntityManager em = emf.createEntityManager();
 
JAXBContext jaxbContext = JAXBContext.newInstance("com.example.customer");
JAXBHelperContext jaxbHelperContext = new JAXBHelperConext(jaxbContext);
jaxbHelperContext.getXSDHelper().define(customerXSD);
 
em.getTransaction().begin();
em.remove(jaxbHelperContext.unwrap(dataObject));
em.getTransaction().commit();

Copyright © Eclipse Foundation, Inc. All Rights Reserved.