Search in sources :

Example 16 with StatelessSession

use of org.hibernate.StatelessSession in project dhis2-core by dhis2.

the class DeletedObjectPostInsertEventListener method onPostInsert.

@Override
public void onPostInsert(PostInsertEvent event) {
    if (IdentifiableObject.class.isInstance(event.getEntity()) && MetadataObject.class.isInstance(event.getEntity()) && !EmbeddedObject.class.isInstance(event.getEntity())) {
        StatelessSession session = event.getPersister().getFactory().openStatelessSession();
        session.beginTransaction();
        try {
            List<DeletedObject> deletedObjects = deletedObjectService.getDeletedObjects(new DeletedObjectQuery((IdentifiableObject) event.getEntity()));
            deletedObjects.forEach(session::delete);
            knownTransactionsService.registerEvent(event);
            session.getTransaction().commit();
        } catch (Exception ex) {
            log.error("Failed to delete DeletedObject for:" + event.getEntity());
            session.getTransaction().rollback();
        } finally {
            session.close();
        }
    }
}
Also used : StatelessSession(org.hibernate.StatelessSession) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) DeletedObject(org.hisp.dhis.deletedobject.DeletedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 17 with StatelessSession

use of org.hibernate.StatelessSession in project dhis2-core by dhis2.

the class DeletedObjectPostDeleteEventListener method onPostDelete.

@Override
public void onPostDelete(PostDeleteEvent event) {
    if (IdentifiableObject.class.isInstance(event.getEntity()) && MetadataObject.class.isInstance(event.getEntity()) && !EmbeddedObject.class.isInstance(event.getEntity())) {
        IdentifiableObject identifiableObject = (IdentifiableObject) event.getEntity();
        DeletedObject deletedObject = new DeletedObject(identifiableObject);
        deletedObject.setDeletedBy(getUsername());
        StatelessSession session = event.getPersister().getFactory().openStatelessSession();
        session.beginTransaction();
        knownTransactionsService.registerEvent(event);
        try {
            session.insert(deletedObject);
            session.getTransaction().commit();
        } catch (Exception ex) {
            log.error("Failed to save DeletedObject: " + deletedObject);
            session.getTransaction().rollback();
        } finally {
            session.close();
        }
    }
}
Also used : StatelessSession(org.hibernate.StatelessSession) DeletedObject(org.hisp.dhis.deletedobject.DeletedObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

StatelessSession (org.hibernate.StatelessSession)17 Test (org.junit.Test)11 Transaction (org.hibernate.Transaction)7 SkipForDialect (org.hibernate.testing.SkipForDialect)3 Date (java.util.Date)2 ScrollableResults (org.hibernate.ScrollableResults)2 Session (org.hibernate.Session)2 SessionFactory (org.hibernate.SessionFactory)2 TestForIssue (org.hibernate.testing.TestForIssue)2 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)2 DeletedObject (org.hisp.dhis.deletedobject.DeletedObject)2 Serializable (java.io.Serializable)1 EntityTransaction (javax.persistence.EntityTransaction)1 HibernateException (org.hibernate.HibernateException)1 NativeQuery (org.hibernate.query.NativeQuery)1 Triggerable (org.hibernate.testing.logger.Triggerable)1 Person (org.hibernate.userguide.model.Person)1 InvalidIdentifierReferenceException (org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)1 DeletedObjectQuery (org.hisp.dhis.deletedobject.DeletedObjectQuery)1