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();
}
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));
}
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(", "))));
}
}
Aggregations