Search in sources :

Example 1 with PersistentBag

use of org.hibernate.collection.internal.PersistentBag in project hibernate-orm by hibernate.

the class PersistentBagTest method testWriteMethodDirtying.

@Test
public void testWriteMethodDirtying() {
    BagOwner parent = new BagOwner("root");
    BagOwner child = new BagOwner("c1");
    parent.getChildren().add(child);
    child.setParent(parent);
    BagOwner otherChild = new BagOwner("c2");
    Session session = openSession();
    session.beginTransaction();
    session.save(parent);
    session.flush();
    // at this point, the list on parent has now been replaced with a PersistentBag...
    PersistentBag children = (PersistentBag) parent.getChildren();
    assertFalse(children.remove(otherChild));
    assertFalse(children.isDirty());
    ArrayList otherCollection = new ArrayList();
    otherCollection.add(child);
    assertFalse(children.retainAll(otherCollection));
    assertFalse(children.isDirty());
    otherCollection = new ArrayList();
    otherCollection.add(otherChild);
    assertFalse(children.removeAll(otherCollection));
    assertFalse(children.isDirty());
    children.clear();
    session.delete(child);
    assertTrue(children.isDirty());
    session.flush();
    children.clear();
    assertFalse(children.isDirty());
    session.delete(parent);
    session.getTransaction().commit();
    session.close();
}
Also used : PersistentBag(org.hibernate.collection.internal.PersistentBag) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with PersistentBag

use of org.hibernate.collection.internal.PersistentBag in project droolsjbpm-integration by kiegroup.

the class HibernateXStreamMarshallerExtensionTest method testMarshallDummyHibernatePersistenceBag.

@Test
public void testMarshallDummyHibernatePersistenceBag() {
    SharedSessionContractImplementor session = Mockito.mock(SharedSessionContractImplementor.class);
    Mockito.when(session.isOpen()).thenReturn(true);
    Mockito.when(session.isConnected()).thenReturn(true);
    PersistentBag bag = new PersistentBag(session, new ArrayList<String>());
    String expectedOutput = "<list/>";
    Marshaller marshaller = MarshallerFactory.getMarshaller(MarshallingFormat.XSTREAM, getClass().getClassLoader());
    Assert.assertEquals(expectedOutput, marshaller.marshall(bag));
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) PersistentBag(org.hibernate.collection.internal.PersistentBag) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Test(org.junit.Test)

Example 3 with PersistentBag

use of org.hibernate.collection.internal.PersistentBag in project axelor-open-suite by axelor.

the class GlobalAuditTracker method addCollectionModification.

@SuppressWarnings("unchecked")
protected void addCollectionModification(Object collection, Long id) {
    if (collection instanceof AbstractPersistentCollection) {
        AbstractPersistentCollection newValues = null;
        Collection<AuditableModel> oldValues = null;
        if (collection instanceof PersistentSet) {
            // MANY-TO-MANY
            newValues = (PersistentSet) collection;
            oldValues = (Collection<AuditableModel>) ((Map<?, ?>) newValues.getStoredSnapshot()).keySet();
        } else if (collection instanceof PersistentBag) {
            // ONE-TO-MANY
            newValues = (PersistentBag) collection;
            oldValues = (Collection<AuditableModel>) newValues.getStoredSnapshot();
        }
        if (newValues == null) {
            return;
        }
        Object owner = newValues.getOwner();
        if (owner == null || Arrays.asList(GlobalAuditInterceptor.BACKLISTED_CLASSES).contains(owner.getClass()) || !(owner instanceof AuditableModel)) {
            return;
        }
        String fieldName = newValues.getRole().replace(owner.getClass().getCanonicalName() + ".", "");
        GlobalTrackingLog log = LOGS.get().stream().filter(l -> l.getRelatedId().equals(id) && l.getMetaModelName().equals(owner.getClass().getSimpleName())).findFirst().orElse(addLog((AuditableModel) owner, GlobalTrackingLogRepository.TYPE_UPDATE));
        List<Long> previousIdList = new ArrayList<>();
        List<Long> newIdList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(oldValues)) {
            for (AuditableModel oldValue : oldValues) {
                if (oldValue != null) {
                    previousIdList.add(oldValue.getId());
                }
            }
        }
        for (AuditableModel newValue : (Collection<AuditableModel>) newValues) {
            if (newValue != null) {
                newIdList.add(newValue.getId());
            }
        }
        GlobalTrackingLogLine line = log.getGlobalTrackingLogLineList().stream().filter(l -> l.getMetaFieldName().equals(fieldName)).findFirst().orElse(null);
        if (line == null) {
            line = new GlobalTrackingLogLine();
            line.setMetaFieldName(fieldName);
            line.setGlobalTrackingLog(log);
            line.setPreviousValue(String.format("[%s]", previousIdList.stream().map(String::valueOf).collect(Collectors.joining(", "))));
            log.addGlobalTrackingLogLineListItem(line);
        }
        line.setNewValue(String.format("[%s]", newIdList.stream().map(String::valueOf).collect(Collectors.joining(", "))));
    }
}
Also used : GlobalTrackingLog(com.axelor.apps.base.db.GlobalTrackingLog) ArrayList(java.util.ArrayList) GlobalTrackingLogLine(com.axelor.apps.base.db.GlobalTrackingLogLine) AuditableModel(com.axelor.auth.db.AuditableModel) PersistentSet(org.hibernate.collection.internal.PersistentSet) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) PersistentBag(org.hibernate.collection.internal.PersistentBag) Collection(java.util.Collection) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PersistentBag (org.hibernate.collection.internal.PersistentBag)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 GlobalTrackingLog (com.axelor.apps.base.db.GlobalTrackingLog)1 GlobalTrackingLogLine (com.axelor.apps.base.db.GlobalTrackingLogLine)1 AuditableModel (com.axelor.auth.db.AuditableModel)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Session (org.hibernate.Session)1 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)1 PersistentSet (org.hibernate.collection.internal.PersistentSet)1 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)1 Marshaller (org.kie.server.api.marshalling.Marshaller)1