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

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

Introduction

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

Prototype

@Deprecated
public PersistentSet(SessionImplementor session) 

Source Link

Document

Instantiates a lazy set (the underlying set is un-initialized).

Usage

From source file:com.erinors.hpb.server.handler.SetHandler.java

License:Apache License

@Override
public Object merge(MergingContext context, Object object) {
    if (!(object instanceof Set)) {
        return null;
    }/* w ww.  j ava2s . c o  m*/

    Set<?> source = (Set<?>) object;
    Set<?> result;

    if (source instanceof UninitializedPersistentSet) {
        result = new PersistentSet(context.getSessionImplementor());
        context.addProcessedObject(object, result);
    } else if (source instanceof com.erinors.hpb.shared.impl.PersistentSet) {
        PersistentSet set = new PersistentSet(context.getSessionImplementor(), new HashSet<Object>());
        context.addProcessedObject(object, set);

        for (Object element : source) {
            set.add(context.merge(element));
        }

        if (((com.erinors.hpb.shared.impl.PersistentSet<?>) source).isDirty()) {
            set.dirty();
        } else {
            set.clearDirty();
        }

        result = set;
    } else {
        Set<Object> set = new HashSet<Object>(source.size());
        context.addProcessedObject(object, set);

        for (Object element : source) {
            set.add(context.merge(element));
        }

        result = set;
    }

    return result;
}

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

License:Open Source License

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

    Person1 p = new Person1(null, null, "P1");
    p.setFirstName("test");
    p.setLastName("test");
    open();
    p = save(p);
    flush();
    Long personId = p.getId();
    close();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());
    DataContext.init("topic", DefaultDataTopicParams.class, PublishMode.MANUAL);

    Change change = new Change(Person1.class.getName(), personId, 0L, "P1");
    change.getChanges().put("lastName", "toto");
    ChangeSet changeSet = new ChangeSet(new Change[] { change });

    Person1 proxy = (Person1) graniteConfig.getConverters().convert(changeSet, Person1.class);

    open();

    merge(proxy);

    flush();
    close();

    open();

    p = find(Person1.class, personId);

    Assert.assertEquals("Person name changed", "toto", p.getLastName());

    close();

    GraniteContext.release();
    DataContext.remove();

    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());
    DataContext.init("topic", DefaultDataTopicParams.class, PublishMode.MANUAL);

    Person1 p2 = new Person1(personId, 1L, "P1");
    p2.setContacts(new PersistentSet(null));
    Address a2 = new Address(null, null, "A1");
    Contact1 c2 = new Contact1(null, null, "C1");
    c2.setPerson(p2);
    c2.setEmail("test@test.net");
    c2.setAddress(a2);
    Change change2 = new Change(Person1.class.getName(), personId, 1L, "P1");
    CollectionChanges ccs2 = new CollectionChanges(
            new CollectionChange[] { new CollectionChange(1, null, c2) });
    change2.getChanges().put("contacts", ccs2);
    ChangeSet changeSet2 = new ChangeSet(new Change[] { change2 });

    Person1 proxy2 = (Person1) graniteConfig.getConverters().convert(changeSet2, Person1.class);

    open();

    merge(proxy2);

    flush();
    close();

    open();

    p = find(Person1.class, personId);

    Assert.assertEquals("Person name changed", "toto", p.getLastName());

    close();

}

From source file:org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController.java

License:Open Source License

/**
 * Hibernate related cloning.//from  w  w w  .  j a v a2s. c om
 * <p/>
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
protected <E> E cloneUninitializedProperty(Object owner, E propertyValue) {
    E clonedPropertyValue = propertyValue;
    if (isInitialized(owner)) {
        if (propertyValue instanceof PersistentCollection) {
            if (unwrapProxy((((PersistentCollection) propertyValue).getOwner())) != unwrapProxy(owner)) {
                if (propertyValue instanceof PersistentSet) {
                    clonedPropertyValue = (E) new PersistentSet(
                            // Must reset the session.
                            // See bug #902
                            /* ((PersistentSet) propertyValue).getSession() */null);
                } else if (propertyValue instanceof PersistentList) {
                    clonedPropertyValue = (E) new PersistentList(
                            // Must reset the session.
                            // See bug #902
                            /* ((PersistentList) propertyValue).getSession() */null);
                }
                changeCollectionOwner((Collection<?>) clonedPropertyValue, owner);
                ((PersistentCollection) clonedPropertyValue).setSnapshot(
                        ((PersistentCollection) propertyValue).getKey(),
                        ((PersistentCollection) propertyValue).getRole(), null);
            }
        } else {
            if (propertyValue instanceof HibernateProxy) {
                return (E) getHibernateSession().load(
                        ((HibernateProxy) propertyValue).getHibernateLazyInitializer().getEntityName(),
                        ((IEntity) propertyValue).getId());
            }
        }
    }
    return clonedPropertyValue;
}