List of usage examples for org.hibernate.collection.internal PersistentList PersistentList
@Deprecated
public PersistentList(SessionImplementor session, List list)
From source file:com.corvid.json.hibernate.usertype.JsonListUserType.java
License:Apache License
@Override public PersistentCollection wrap(SessionImplementor session, Object collection) { return new PersistentList(session, (List<?>) collection); }
From source file:org.granite.hibernate4.HibernateExternalizer.java
License:Open Source License
protected PersistentCollection newHibernateCollection(AbstractExternalizablePersistentCollection value, Property field) {/*w w w . ja v a 2s .c om*/ 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.PersistentListCodec.java
License:Open Source License
public PersistentList 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 PersistentList(null, new ArrayList<Object>()) : new PersistentList(null)); }
From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/* w ww. j a v a 2 s .co m*/ public void testSimpleChangesList() throws Exception { initPersistence(); open(); Order2 o = new Order2(null, null, "O1"); o.setDescription("zozo"); o.setLineItemsList(new PersistentList(null, new ArrayList<LineItemList2>())); LineItemList2 i1 = new LineItemList2(null, null, "LI1"); i1.setDescription("item 1"); o.getLineItemsList().add(i1); open(); o = save(o); flush(); Long orderId = o.getId(); close(); open(); 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>()); ChangeSet changeSet = new ChangeSet(); Change change = new Change(Order2.class.getName(), orderId, o.getVersion(), "O1"); LineItemList2 i2u = new LineItemList2(null, null, "LI2"); i2u.setDescription("item 2"); change.getChanges().put("lineItemsList", new CollectionChanges(new CollectionChange[] { new CollectionChange(1, 1, i2u) })); changeSet.setChanges(new Change[] { change }); new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet); flush(); o = find(Order2.class, orderId); Assert.assertEquals("List updated", 2, o.getLineItemsList().size()); Assert.assertEquals("List elt 1", "item 1", o.getLineItemsList().get(0).getDescription()); Assert.assertEquals("List elt 2", "item 2", o.getLineItemsList().get(1).getDescription()); close(); }
From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/* w w w .ja va 2 s . c o m*/ public void testSimpleChangesList2() throws Exception { initPersistence(); open(); Phone2 ph1 = new Phone2(null, null, "PH1"); ph1.setPhone("01"); ph1 = save(ph1); Phone2 ph2 = new Phone2(null, null, "PH2"); ph2.setPhone("02"); ph2 = save(ph2); Phone2 ph3 = new Phone2(null, null, "PH3"); ph3.setPhone("03"); ph3 = save(ph3); Phone2 ph4 = new Phone2(null, null, "PH4"); ph4.setPhone("04"); ph4 = save(ph4); flush(); close(); Person2 p = new Person2(null, null, "P1"); p.setFirstName("test"); p.setLastName("test"); p.setContacts(new PersistentList(null, new ArrayList<Contact2>())); Contact2 c1 = new Contact2(null, null, "C1"); c1.setPerson(p); p.getContacts().add(c1); c1.setPhones(new PersistentList(null, new ArrayList<Phone2>())); c1.getPhones().add(ph1); c1.getPhones().add(ph2); open(); p = save(p); flush(); Long personId = p.getId(); close(); open(); 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>()); ChangeSet changeSet = new ChangeSet(); Change change = new Change(Person2.class.getName(), personId, p.getVersion(), "P1"); Person2 p2 = new Person2(personId, p.getVersion(), "P1"); p2.setFirstName("test"); p2.setLastName("test"); p2.setContacts(new PersistentList()); Contact2 c2 = new Contact2(null, null, "C2"); c2.setPerson(p2); c2.setPhones(new PersistentList(null, new ArrayList<Phone2>())); ph3 = find(Phone2.class, ph3.getId()); ph4 = find(Phone2.class, ph4.getId()); c2.getPhones().add(ph3); c2.getPhones().add(ph4); change.getChanges().put("contacts", new CollectionChanges(new CollectionChange[] { new CollectionChange(1, 1, c2) })); changeSet.setChanges(new Change[] { change }); new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet); flush(); p = find(Person2.class, personId); Assert.assertEquals("List updated", 2, p.getContacts().size()); Assert.assertEquals("List elt 2 phones", 2, p.getContacts().get(1).getPhones().size()); Assert.assertEquals("List elt 2 phone 2", "04", p.getContacts().get(1).getPhones().get(1).getPhone()); close(); }
From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java
License:Open Source License
@Test @SuppressWarnings("unchecked") public void testProxyChanges() throws Exception { initPersistence();/* ww w.j a v a 2 s . c o m*/ open(); Classification cl2 = new Classification(null, null, "CL2"); cl2.setCode("CL2"); cl2 = save(cl2); flush(); Long id2 = cl2.getId(); Long v2 = cl2.getVersion(); close(); ProxyFactory pf = new ProxyFactory(JavassistLazyInitializer.class.getName()); cl2 = (Classification) pf.getProxyInstance(Classification.class.getName(), Classification.class.getName(), id2); open(); 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>()); Classification cl1 = new Classification(null, null, "CL1"); cl1.setCode("CL1"); cl1.setSubClassifications(new PersistentList(null, new ArrayList<Classification>())); cl1.setSuperClassifications(new PersistentSet(null, new HashSet<Classification>())); cl1.getSubClassifications().add(cl2); ChangeSet changeSet = new ChangeSet(); Change change1 = new Change(Classification.class.getName(), id2, v2, "CL2"); CollectionChanges collChanges1 = new CollectionChanges(); CollectionChange collChange1 = new CollectionChange(1, null, cl1); collChanges1.setChanges(new CollectionChange[] { collChange1 }); change1.getChanges().put("superClassifications", collChanges1); changeSet.setChanges(new Change[] { change1 }); new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet); flush(); close(); open(); cl2 = find(Classification.class, id2); Assert.assertEquals("Collection change applied", 1, cl2.getSuperClassifications().size()); Assert.assertSame("Collection change applied 2", cl2, cl2.getSuperClassifications().iterator().next().getSubClassifications().get(0)); close(); }
From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/*from www .j a va 2 s. c o m*/ public void testReorderList() throws Exception { initPersistence(); open(); VitalSignTest3 test = new VitalSignTest3(null, null, "T1"); test.setVitalSignObservations(new PersistentList(null, new ArrayList<VitalSignObservation3>())); VitalSignObservation3 obs1 = new VitalSignObservation3(null, null, "O1"); obs1.setVitalSignTest(test); VitalSignObservation3 obs2 = new VitalSignObservation3(null, null, "O2"); obs2.setVitalSignTest(test); VitalSignObservation3 obs3 = new VitalSignObservation3(null, null, "O3"); obs3.setVitalSignTest(test); test.getVitalSignObservations().add(obs1); test.getVitalSignObservations().add(obs2); test.getVitalSignObservations().add(obs3); test = save(test); flush(); Long testId = test.getId(); // Long obs2Id = test.getVitalSignObservations().get(1).getId(); Long obs3Id = test.getVitalSignObservations().get(2).getId(); close(); open(); 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(null, null, PublishMode.MANUAL); Change change = new Change(VitalSignTest3.class.getName(), testId, test.getVersion(), test.getUid()); CollectionChange collChange1 = new CollectionChange(-1, 2, new ChangeRef(VitalSignObservation3.class.getName(), "O3", new Integer(obs3Id.intValue()))); CollectionChange collChange2 = new CollectionChange(1, 1, new ChangeRef(VitalSignObservation3.class.getName(), "O3", new Integer(obs3Id.intValue()))); CollectionChanges collChanges = new CollectionChanges(new CollectionChange[] { collChange1, collChange2 }); change.getChanges().put("vitalSignObservations", collChanges); ChangeSet changeSet = new ChangeSet(new Change[] { change }); new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet); flush(); close(); open(); VitalSignTest3 test2 = find(VitalSignTest3.class, testId); Assert.assertEquals("Observations", 3, test2.getVitalSignObservations().size()); Assert.assertEquals("Obs1", "O1", test2.getVitalSignObservations().get(0).getUid()); Assert.assertEquals("Obs2", "O3", test2.getVitalSignObservations().get(1).getUid()); Assert.assertEquals("Obs3", "O2", test2.getVitalSignObservations().get(2).getUid()); close(); }
From source file:org.granite.test.tide.hibernate4.data.TestHibernate4ChangeSetPublisher.java
License:Open Source License
@SuppressWarnings("unchecked") @Test/*ww w . j a va 2 s . co m*/ public void testListChanges() throws Exception { initPersistence(); Order2 o = new Order2(null, null, "O1"); o.setLineItemsList(new PersistentList(null, new ArrayList<LineItemList2>())); o.setDescription("order 1"); LineItemList2 i1 = new LineItemList2(null, null, "I1"); i1.setDescription("item 1"); o.getLineItemsList().add(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(Order2.class, orderId); LineItemList2 i2 = new LineItemList2(null, null, "I2"); i2.setDescription("item 2"); o.getLineItemsList().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(Order2.class.getName())) { update = u; break; } } Assert.assertNotNull("Updates order", update); CollectionChanges collChanges = (CollectionChanges) ((Change) update.entity).getChanges() .get("lineItemsList"); 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 LineItemList2); Assert.assertEquals("Update coll index", 1, collChange.getKey()); Assert.assertEquals("Update coll value", i2.getDescription(), ((LineItemList2) collChange.getValue()).getDescription()); DataContext.remove(); DataContext.init(new DefaultDataDispatcher(null, "testTopic", DefaultDataTopicParams.class), PublishMode.MANUAL); open(); o = find(Order2.class, orderId); LineItemList2 i3 = new LineItemList2(null, null, "I3"); i3.setDescription("item 3"); o.getLineItemsList().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(Order2.class.getName())) { update = u; break; } } Assert.assertNotNull("Updates order", update); collChanges = (CollectionChanges) ((Change) update.entity).getChanges().get("lineItemsList"); 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 LineItemList2); Assert.assertEquals("Update coll index", 1, collChange.getKey()); Assert.assertEquals("Update coll value", i3.getDescription(), ((LineItemList2) collChange.getValue()).getDescription()); DataContext.remove(); DataContext.init(new DefaultDataDispatcher(null, "testTopic", DefaultDataTopicParams.class), PublishMode.MANUAL); open(); o = find(Order2.class, orderId); o.getLineItemsList().remove(1); flush(); close(); updates = DataContext.get().getDataUpdates(); update = null; for (EntityUpdate u : updates) { if (u.entity instanceof Change && ((Change) u.entity).getClassName().equals(Order2.class.getName())) { update = u; break; } } Assert.assertNotNull("Updates order", update); collChanges = (CollectionChanges) ((Change) update.entity).getChanges().get("lineItemsList"); 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 LineItemList2); Assert.assertEquals("Update coll index", 1, collChange.getKey()); Assert.assertEquals("Update coll value", i3.getDescription(), ((LineItemList2) collChange.getValue()).getDescription()); DataContext.remove(); DataContext.init(new DefaultDataDispatcher(null, "testTopic", DefaultDataTopicParams.class), PublishMode.MANUAL); // Cannot make this work with Hibernate 3.6.5.Final // JDBC Exception: failed batch ??? // open(); // o = find(Order2.class, orderId); // LineItemList2 i4 = new LineItemList2(null, null, "I4"); // i4.setDescription("item 4"); // o.getLineItemsList().add(i4); // o.getLineItemsList().remove(0); // flush(); // close(); // // updates = DataContext.get().getDataUpdates(); // update = null; // for (EntityUpdate u : updates) { // if (u.entity instanceof Change && ((Change)u.entity).getClassName().equals(Order2.class.getName())) { // update = u; // break; // } // } // Assert.assertNotNull("Updates order", update); // collChanges = (CollectionChanges)((Change)update.entity).getChanges().get("lineItemsList"); // Assert.assertEquals("Update collection", 2, collChanges.getChanges().length); // collChange = collChanges.getChanges()[0]; // Assert.assertEquals("Update coll remove", -1, collChange.getType()); // Assert.assertTrue("Update coll type", collChange.getValue() instanceof LineItemList2); // Assert.assertEquals("Update coll index", 0, collChange.getKey()); // Assert.assertEquals("Update coll value", i1.getDescription(), ((LineItemList2)collChange.getValue()).getDescription()); // collChange = collChanges.getChanges()[1]; // Assert.assertEquals("Update coll add", 1, collChange.getType()); // Assert.assertTrue("Update coll type", collChange.getValue() instanceof LineItemList2); // Assert.assertEquals("Update coll index", 1, collChange.getKey()); // Assert.assertEquals("Update coll value", i4.getDescription(), ((LineItemList2)collChange.getValue()).getDescription()); }
From source file:org.granite.test.tide.hibernate4.data.TestHibernate4ChangeSetPublisher.java
License:Open Source License
@SuppressWarnings("unchecked") @Test// w ww.ja v a 2s. c o m public void testReorderList2() throws Exception { initPersistence(); open(); Classification p = new Classification(null, null, "P1"); p.setCode("P1"); p.setSubClassifications(new PersistentList(null, new ArrayList<Classification>())); Classification c1 = new Classification(null, null, "C1"); c1.setCode("C1"); Classification c2 = new Classification(null, null, "C2"); c2.setCode("C2"); Classification c3 = new Classification(null, null, "C3"); c3.setCode("C3"); Classification c4 = new Classification(null, null, "C4"); c4.setCode("C4"); p.getSubClassifications().add(c1); p.getSubClassifications().add(c2); p.getSubClassifications().add(c3); p.getSubClassifications().add(c4); p = save(p); flush(); Long pId = p.getId(); Long c4Id = p.getSubClassifications().get(3).getId(); close(); open(); InputStream is = Thread.currentThread().getContextClassLoader() .getResourceAsStream("org/granite/test/tide/data/enterprise/granite-config.xml"); 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(null, null, PublishMode.MANUAL); Change change = new Change(Classification.class.getName(), pId, p.getVersion(), p.getUid()); CollectionChange collChange1 = new CollectionChange(-1, 3, new ChangeRef(Classification.class.getName(), "C4", new Integer(c4Id.intValue()))); CollectionChange collChange2 = new CollectionChange(1, 2, new ChangeRef(Classification.class.getName(), "C4", new Integer(c4Id.intValue()))); CollectionChanges collChanges = new CollectionChanges(new CollectionChange[] { collChange1, collChange2 }); change.getChanges().put("subClassifications", collChanges); ChangeSet changeSet = new ChangeSet(new Change[] { change }); new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet); flush(); close(); List<EntityUpdate> updates = DataContext.get().getDataUpdates(); Assert.assertEquals("1 update", 1, updates.size()); Assert.assertTrue("Change update", updates.get(0).entity instanceof Change); Change ch = (Change) updates.get(0).entity; CollectionChanges cch = (CollectionChanges) ch.getChanges().get("subClassifications"); Assert.assertEquals("Coll changes", 2, cch.getChanges().length); Assert.assertEquals("Coll change 1 del", -1, cch.getChanges()[0].getType()); Assert.assertEquals("Coll change 2 add", 1, cch.getChanges()[1].getType()); }
From source file:org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController.java
License:Open Source License
/** * This method wraps transient collections in the equivalent hibernate ones. * {@inheritDoc}/*from w ww .jav a 2 s . c om*/ */ @SuppressWarnings("unchecked") @Override protected <E> Collection<E> wrapDetachedCollection(IEntity owner, Collection<E> detachedCollection, Collection<E> snapshotCollection, String role) { Collection<E> varSnapshotCollection = snapshotCollection; if (!(detachedCollection instanceof PersistentCollection)) { String collectionRoleName = HibernateHelper.getHibernateRoleName(getComponentContract(owner), role); if (collectionRoleName == null) { // it is not an hibernate managed collection (e.g. "detachedEntities") return detachedCollection; } if (detachedCollection instanceof Set) { PersistentSet persistentSet = new PersistentSet(null, (Set<?>) detachedCollection); changeCollectionOwner(persistentSet, owner); HashMap<Object, Object> snapshot = new HashMap<>(); if (varSnapshotCollection == null) { persistentSet.clearDirty(); varSnapshotCollection = detachedCollection; } for (Object snapshotCollectionElement : varSnapshotCollection) { snapshot.put(snapshotCollectionElement, snapshotCollectionElement); } persistentSet.setSnapshot(owner.getId(), collectionRoleName, snapshot); return persistentSet; } else if (detachedCollection instanceof List) { PersistentList persistentList = new PersistentList(null, (List<?>) detachedCollection); changeCollectionOwner(persistentList, owner); ArrayList<Object> snapshot = new ArrayList<>(); if (varSnapshotCollection == null) { persistentList.clearDirty(); varSnapshotCollection = detachedCollection; } for (Object snapshotCollectionElement : varSnapshotCollection) { snapshot.add(snapshotCollectionElement); } persistentList.setSnapshot(owner.getId(), collectionRoleName, snapshot); return persistentList; } } else { if (varSnapshotCollection == null) { ((PersistentCollection) detachedCollection).clearDirty(); } else { ((PersistentCollection) detachedCollection).dirty(); } } return detachedCollection; }