| 13 | |
| 14 | === Example === |
| 15 | |
| 16 | An example of a test case verifying that a !RegistryObject is stored, both by querying on !RegistryObject and polymorphically on its base class Identifiable, is this: |
| 17 | |
| 18 | {{{ |
| 19 | |
| 20 | public void testPersistingRegistryObject(){ |
| 21 | |
| 22 | // create transient object |
| 23 | RegistryObject transientRegistryObject = RegistryObjectFactory.createRegistryObject(); |
| 24 | URN businessKey = transientRegistryObject.getId(); |
| 25 | |
| 26 | // make transient object persistent |
| 27 | Transaction tx = hibernateSession.beginTransaction(); |
| 28 | hibernateSession.saveOrUpdate(transientRegistryObject); |
| 29 | tx.commit(); |
| 30 | |
| 31 | // verify database state |
| 32 | Query registryObjectQuery = hibernateSession.createQuery("from RegistryObject o where o.id = '" + businessKey + "'"); |
| 33 | List<RegistryObject> registryObjectResult = registryObjectQuery.list(); |
| 34 | RegistryObject persistentRegistryObject = registryObjectResult.get(0); |
| 35 | assertEquals(transientRegistryObject, persistentRegistryObject); |
| 36 | |
| 37 | // verify database state using a polymorphic query |
| 38 | Query identifiableQuery = hibernateSession.createQuery("from Identifiable o where o.id = '" + businessKey + "'"); |
| 39 | List<Identifiable> identifiableResult = identifiableQuery.list(); |
| 40 | Identifiable identifiable = identifiableResult.get(0); |
| 41 | assertEquals(transientRegistryObject, identifiable); |
| 42 | } |
| 43 | |
| 44 | }}} |