Example usage for org.hibernate.collection.internal PersistentBag PersistentBag

List of usage examples for org.hibernate.collection.internal PersistentBag PersistentBag

Introduction

In this page you can find the example usage for org.hibernate.collection.internal PersistentBag PersistentBag.

Prototype

@Deprecated
public PersistentBag(SessionImplementor session, Collection coll) 

Source Link

Document

Constructs a PersistentBag

Usage

From source file:org.granite.hibernate4.HibernateExternalizer.java

License:Open Source License

protected PersistentCollection newHibernateCollection(AbstractExternalizablePersistentCollection value,
        Property field) {/*from   w w  w  . j ava2s  . c o m*/
    final Type target = field.getType();
    final boolean initialized = value.isInitialized();
    final String metadata = value.getMetadata();
    final boolean dirty = value.isDirty();
    final boolean sorted = (SortedSet.class.isAssignableFrom(TypeUtil.classOfType(target))
            || SortedMap.class.isAssignableFrom(TypeUtil.classOfType(target)));

    Comparator<?> comparator = null;
    if (sorted && field.isAnnotationPresent(Sort.class)) {
        Sort sort = field.getAnnotation(Sort.class);
        if (sort.type() == SortType.COMPARATOR) {
            try {
                comparator = TypeUtil.newInstance(sort.comparator(), Comparator.class);
            } catch (Exception e) {
                throw new RuntimeException("Could not create instance of Comparator: " + sort.comparator());
            }
        }
    }

    PersistentCollection coll = null;
    if (value instanceof ExternalizablePersistentSet) {
        if (initialized) {
            Set<?> set = ((ExternalizablePersistentSet) value).getContentAsSet(target, comparator);
            coll = (sorted ? new PersistentSortedSet(null, (SortedSet<?>) set) : new PersistentSet(null, set));
        } else
            coll = (sorted ? new PersistentSortedSet() : new PersistentSet());
    } else if (value instanceof ExternalizablePersistentBag) {
        if (initialized) {
            List<?> bag = ((ExternalizablePersistentBag) value).getContentAsList(target);
            coll = new PersistentBag(null, bag);
        } else
            coll = new PersistentBag();
    } else if (value instanceof ExternalizablePersistentList) {
        if (initialized) {
            List<?> list = ((ExternalizablePersistentList) value).getContentAsList(target);
            coll = new PersistentList(null, list);
        } else
            coll = new PersistentList();
    } else if (value instanceof ExternalizablePersistentMap) {
        if (initialized) {
            Map<?, ?> map = ((ExternalizablePersistentMap) value).getContentAsMap(target, comparator);
            coll = (sorted ? new PersistentSortedMap(null, (SortedMap<?, ?>) map)
                    : new PersistentMap(null, map));
        } else
            coll = (sorted ? new PersistentSortedMap() : new PersistentMap());
    } else
        throw new RuntimeException("Illegal externalizable persitent class: " + value);

    if (metadata != null && serializeMetadata != SerializeMetadata.NO
            && (serializeMetadata == SerializeMetadata.YES || !initialized)) {
        String[] toks = metadata.split(":", 3);
        if (toks.length != 3)
            throw new RuntimeException("Invalid collection metadata: " + metadata);
        Serializable key = deserializeSerializable(StringUtil.hexStringToBytes(toks[0]));
        Serializable snapshot = deserializeSerializable(StringUtil.hexStringToBytes(toks[1]));
        String role = toks[2];
        coll.setSnapshot(key, role, snapshot);
    }

    if (initialized && dirty)
        coll.dirty();

    return coll;
}

From source file:org.granite.hibernate4.jmf.PersistentBagCodec.java

License:Open Source License

public PersistentBag newInstance(ExtendedObjectInput in, String className)
        throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        InvocationTargetException, SecurityException, NoSuchMethodException {

    PersistentCollectionSnapshot snapshot = new JMFPersistentCollectionSnapshot(null);
    snapshot.readInitializationData(in);
    return (snapshot.isInitialized() ? new PersistentBag(null, new ArrayList<Object>())
            : new PersistentBag(null));
}

From source file:org.granite.test.tide.hibernate4.data.TestHibernate4ChangeSetPublisher.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from   w w w.j  a  v a 2s  .c o m
public void testInverseBagChanges() throws Exception {
    initPersistence();

    Order o = new Order(null, null, "O1");
    o.setLineItemsBag(new PersistentBag(null, new ArrayList<LineItemBag>()));
    o.setDescription("order 1");
    LineItemBag i1 = new LineItemBag(null, null, "I1");
    i1.setDescription("item 1");
    i1.setOrder(o);
    o.getLineItemsBag().add(0, i1);
    open();
    o = save(o);
    flush();
    Long orderId = o.getId();
    close();

    DataContext.remove();
    DataContext.init(new DefaultDataDispatcher(null, "testTopic", DefaultDataTopicParams.class),
            PublishMode.MANUAL);

    open();
    o = find(Order.class, orderId);
    LineItemBag i2 = new LineItemBag(null, null, "I2");
    i2.setDescription("item 2");
    i2.setOrder(o);
    o.getLineItemsBag().add(i2);
    flush();
    close();

    List<EntityUpdate> updates = DataContext.get().getDataUpdates();
    EntityUpdate update = null;
    for (EntityUpdate u : updates) {
        if (u.entity instanceof Change && ((Change) u.entity).getClassName().equals(Order.class.getName())) {
            update = u;
            break;
        }
    }
    Assert.assertNotNull("Updates order", update);
    CollectionChanges collChanges = (CollectionChanges) ((Change) update.entity).getChanges()
            .get("lineItemsBag");
    Assert.assertEquals("Update collection", 1, collChanges.getChanges().length);
    CollectionChange collChange = collChanges.getChanges()[0];
    Assert.assertEquals("Update coll add", 1, collChange.getType());
    Assert.assertTrue("Update coll type", collChange.getValue() instanceof LineItemBag);
    Assert.assertEquals("Update coll value", i2.getDescription(),
            ((LineItemBag) collChange.getValue()).getDescription());

    DataContext.remove();
    DataContext.init(new DefaultDataDispatcher(null, "testTopic", DefaultDataTopicParams.class),
            PublishMode.MANUAL);

    open();
    o = find(Order.class, orderId);
    LineItemBag i3 = new LineItemBag(null, null, "I3");
    i3.setDescription("item 3");
    i3.setOrder(o);
    o.getLineItemsBag().add(1, i3);
    flush();
    close();

    updates = DataContext.get().getDataUpdates();
    update = null;
    for (EntityUpdate u : updates) {
        if (u.entity instanceof Change && ((Change) u.entity).getClassName().equals(Order.class.getName())) {
            update = u;
            break;
        }
    }
    Assert.assertNotNull("Updates order", update);
    collChanges = (CollectionChanges) ((Change) update.entity).getChanges().get("lineItemsBag");
    Assert.assertEquals("Update collection", 1, collChanges.getChanges().length);
    collChange = collChanges.getChanges()[0];
    Assert.assertEquals("Update coll add", 1, collChange.getType());
    Assert.assertTrue("Update coll type", collChange.getValue() instanceof LineItemBag);
    Assert.assertEquals("Update coll index", 1, collChange.getKey());
    Assert.assertEquals("Update coll value", i3.getDescription(),
            ((LineItemBag) collChange.getValue()).getDescription());

    DataContext.remove();
    DataContext.init(new DefaultDataDispatcher(null, "testTopic", DefaultDataTopicParams.class),
            PublishMode.MANUAL);

    open();
    o = find(Order.class, orderId);
    o.getLineItemsBag().remove(1);
    flush();
    close();

    updates = DataContext.get().getDataUpdates();
    update = null;
    for (EntityUpdate u : updates) {
        if (u.entity instanceof Change && ((Change) u.entity).getClassName().equals(Order.class.getName())) {
            update = u;
            break;
        }
    }
    Assert.assertNotNull("Updates order", update);
    collChanges = (CollectionChanges) ((Change) update.entity).getChanges().get("lineItemsBag");
    Assert.assertEquals("Update collection", 1, collChanges.getChanges().length);
    collChange = collChanges.getChanges()[0];
    Assert.assertEquals("Update coll add", -1, collChange.getType());
    Assert.assertTrue("Update coll type", collChange.getValue() instanceof LineItemBag);
    Assert.assertEquals("Update coll index", 1, collChange.getKey());

    DataContext.remove();
    DataContext.init(new DefaultDataDispatcher(null, "testTopic", DefaultDataTopicParams.class),
            PublishMode.MANUAL);

    open();
    o = find(Order.class, orderId);
    LineItemBag i4 = new LineItemBag(null, null, "I4");
    i4.setDescription("item 4");
    o.getLineItemsBag().add(1, i4);
    o.getLineItemsBag().remove(0);
    flush();
    close();

    updates = DataContext.get().getDataUpdates();
    update = null;
    for (EntityUpdate u : updates) {
        if (u.entity instanceof Change && ((Change) u.entity).getClassName().equals(Order.class.getName())) {
            update = u;
            break;
        }
    }
    Assert.assertNotNull("Updates order", update);
    collChanges = (CollectionChanges) ((Change) update.entity).getChanges().get("lineItemsBag");
    Assert.assertEquals("Update collection", 1, collChanges.getChanges().length);
    collChange = collChanges.getChanges()[0];
    Assert.assertEquals("Update coll update", 0, collChange.getType());
    Assert.assertTrue("Update coll type", collChange.getValue() instanceof LineItemBag);
    Assert.assertEquals("Update coll index", 0, collChange.getKey());
    Assert.assertEquals("Update coll value", i4.getDescription(),
            ((LineItemBag) collChange.getValue()).getDescription());
}

From source file:org.kie.server.services.jbpm.xstream.HibernateXStreamMarshallerExtensionTest.java

License:Apache License

@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));
}