use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class AbstractCollectionEventTest method testUpdateParentOneChildDiffCollectionSameChild.
@Test
public void testUpdateParentOneChildDiffCollectionSameChild() {
CollectionListeners listeners = new CollectionListeners(sessionFactory());
ParentWithCollection parent = createParentWithOneChild("parent", "child");
Child child = (Child) parent.getChildren().iterator().next();
listeners.clear();
assertEquals(1, parent.getChildren().size());
Session s = openSession();
Transaction tx = s.beginTransaction();
parent = (ParentWithCollection) s.get(parent.getClass(), parent.getId());
if (child instanceof Entity) {
child = (Child) s.get(child.getClass(), ((Entity) child).getId());
}
Collection oldCollection = parent.getChildren();
parent.newChildren(createCollection());
parent.addChild(child);
tx.commit();
s.close();
int index = 0;
if (((PersistentCollection) oldCollection).wasInitialized()) {
checkResult(listeners, listeners.getInitializeCollectionListener(), parent, oldCollection, index++);
}
if (child instanceof ChildWithBidirectionalManyToMany) {
ChildWithBidirectionalManyToMany childWithManyToMany = (ChildWithBidirectionalManyToMany) child;
if (((PersistentCollection) childWithManyToMany.getParents()).wasInitialized()) {
checkResult(listeners, listeners.getInitializeCollectionListener(), childWithManyToMany, index++);
}
}
checkResult(listeners, listeners.getPreCollectionRemoveListener(), parent, oldCollection, index++);
checkResult(listeners, listeners.getPostCollectionRemoveListener(), parent, oldCollection, index++);
if (child instanceof ChildWithBidirectionalManyToMany) {
// hmmm, the same parent was removed and re-added to the child's collection;
// should this be considered an update?
checkResult(listeners, listeners.getPreCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) child, index++);
checkResult(listeners, listeners.getPostCollectionUpdateListener(), (ChildWithBidirectionalManyToMany) child, index++);
}
checkResult(listeners, listeners.getPreCollectionRecreateListener(), parent, index++);
checkResult(listeners, listeners.getPostCollectionRecreateListener(), parent, index++);
checkNumberOfResults(listeners, index);
}
use of org.hibernate.collection.spi.PersistentCollection in project dhis2-core by dhis2.
the class DefaultLinkService method generateLink.
private <T> void generateLink(T object, String hrefBase, boolean deepScan) {
Schema schema = schemaService.getDynamicSchema(object.getClass());
if (schema == null) {
log.warn("Could not find schema for object of type " + object.getClass().getName() + ".");
return;
}
generateHref(object, hrefBase);
if (!deepScan) {
return;
}
for (Property property : schema.getProperties()) {
try {
// TODO should we support non-idObjects?
if (property.isIdentifiableObject()) {
Object propertyObject = property.getGetterMethod().invoke(object);
if (propertyObject == null) {
continue;
}
// unwrap hibernate PersistentCollection
if (PersistentCollection.class.isAssignableFrom(propertyObject.getClass())) {
PersistentCollection collection = (PersistentCollection) propertyObject;
propertyObject = collection.getValue();
}
if (!property.isCollection()) {
generateHref(propertyObject, hrefBase);
} else {
Collection<?> collection = (Collection<?>) propertyObject;
for (Object collectionObject : collection) {
generateHref(collectionObject, hrefBase);
}
}
}
} catch (InvocationTargetException | IllegalAccessException ignored) {
}
}
}
use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class AbstractCollectionMapper method mapCollectionChanges.
/**
* Map collection changes using hash identity.
*
* @param session The session.
* @param newColl The new persistent collection.
* @param oldColl The old collection.
* @param id The owning entity identifier.
* @return the persistent collection changes.
*/
@SuppressWarnings("unchecked")
private List<PersistentCollectionChangeData> mapCollectionChanges(SessionImplementor session, PersistentCollection newColl, Serializable oldColl, Serializable id) {
final List<PersistentCollectionChangeData> collectionChanges = new ArrayList<PersistentCollectionChangeData>();
// Comparing new and old collection content.
final Collection newCollection = getNewCollectionContent(newColl);
final Collection oldCollection = getOldCollectionContent(oldColl);
final Set<Object> added = new HashSet<>();
if (newColl != null) {
added.addAll(newCollection);
}
// removeAll in AbstractSet has an implementation that is hashcode-change sensitive (as opposed to addAll).
if (oldColl != null) {
added.removeAll(new HashSet(oldCollection));
}
addCollectionChanges(session, collectionChanges, added, RevisionType.ADD, id);
final Set<Object> deleted = new HashSet<>();
if (oldColl != null) {
deleted.addAll(oldCollection);
}
// The same as above - re-hashing new collection.
if (newColl != null) {
deleted.removeAll(new HashSet(newCollection));
}
addCollectionChanges(session, collectionChanges, deleted, RevisionType.DEL, id);
return collectionChanges;
}
use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class AbstractCollectionMapper method isFromNullToEmptyOrFromEmptyToNull.
private boolean isFromNullToEmptyOrFromEmptyToNull(PersistentCollection newColl, Serializable oldColl) {
// Comparing new and old collection content.
final Collection newCollection = getNewCollectionContent(newColl);
final Collection oldCollection = getOldCollectionContent(oldColl);
return oldCollection == null && newCollection != null && newCollection.isEmpty() || newCollection == null && oldCollection != null && oldCollection.isEmpty();
}
use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class AbstractCollectionMapper method mapCollectionChanges.
/**
* Map collection changes using the collection element type equality functionality.
*
* @param session The session.
* @param newColl The new persistent collection.
* @param oldColl The old collection.
* @param id The owning entity identifier.
* @param collectionPersister The collection persister.
* @return the persistent collection changes.
*/
private List<PersistentCollectionChangeData> mapCollectionChanges(SessionImplementor session, PersistentCollection newColl, Serializable oldColl, Serializable id, CollectionPersister collectionPersister) {
final List<PersistentCollectionChangeData> collectionChanges = new ArrayList<PersistentCollectionChangeData>();
// Comparing new and old collection content.
final Collection newCollection = getNewCollectionContent(newColl);
final Collection oldCollection = getOldCollectionContent(oldColl);
// take the new collection and remove any that exist in the old collection.
// take the resulting Set<> and generate ADD changes.
final Set<Object> added = new HashSet<>();
if (newColl != null) {
added.addAll(newCollection);
}
if (oldColl != null && collectionPersister != null) {
for (Object object : oldCollection) {
for (Iterator addedIt = added.iterator(); addedIt.hasNext(); ) {
Object object2 = addedIt.next();
if (collectionPersister.getElementType().isSame(object, object2)) {
addedIt.remove();
break;
}
}
}
}
addCollectionChanges(session, collectionChanges, added, RevisionType.ADD, id);
// take the old collection and remove any that exist in the new collection.
// take the resulting Set<> and generate DEL changes.
final Set<Object> deleted = new HashSet<>();
if (oldColl != null) {
deleted.addAll(oldCollection);
}
if (newColl != null && collectionPersister != null) {
for (Object object : newCollection) {
for (Iterator deletedIt = deleted.iterator(); deletedIt.hasNext(); ) {
Object object2 = deletedIt.next();
if (collectionPersister.getElementType().isSame(object, object2)) {
deletedIt.remove();
break;
}
}
}
}
addCollectionChanges(session, collectionChanges, deleted, RevisionType.DEL, id);
return collectionChanges;
}
Aggregations