@Test public void createAwithB() { DynamicType simpleTypeA = helper.getType("SimpleA"); assertNotNull(simpleTypeA); DynamicType simpleTypeB = helper.getType("SimpleB"); assertNotNull(simpleTypeB); EntityManager em = emf.createEntityManager(); assertNotNull(JpaHelper.getServerSession(emf).getDescriptorForAlias("SimpleB")); DynamicEntity simpleInstanceB = simpleTypeB.newDynamicEntity(); simpleInstanceB.set("id", 1); simpleInstanceB.set("value1", "B2"); DynamicEntity simpleInstanceA = simpleTypeA.newDynamicEntity(); simpleInstanceA.set("id", 1); simpleInstanceA.set("value1", "A2"); simpleInstanceA.<Collection<DynamicEntity>>get("b").add(simpleInstanceB); simpleInstanceB.set("a", simpleInstanceA); em.getTransaction().begin(); em.persist(simpleInstanceB); em.persist(simpleInstanceA); em.getTransaction().commit(); int simpleCountB = ((Number) em.createQuery("SELECT COUNT(s) FROM SimpleB s").getSingleResult()).intValue(); assertEquals(1, simpleCountB); int simpleCountA = ((Number) em.createQuery("SELECT COUNT(s) FROM SimpleA s").getSingleResult()).intValue(); assertEquals(1, simpleCountA); em.close(); }
@Test public void verifyConfig() throws Exception { ClassDescriptor descriptorA = helper.getSession().getClassDescriptorForAlias("SimpleA"); assertNotNull("No descriptor found for alias='SimpleA'", descriptorA); DynamicType simpleTypeA = helper.getType("SimpleA"); assertNotNull("'SimpleA' EntityType not found", simpleTypeA); assertEquals(descriptorA, simpleTypeA.getDescriptor()); DirectToFieldMapping a_id = (DirectToFieldMapping) descriptorA.getMappingForAttributeName("id"); assertEquals(int.class, a_id.getAttributeClassification()); DirectToFieldMapping a_value1 = (DirectToFieldMapping) descriptorA.getMappingForAttributeName("value1"); assertEquals(String.class, a_value1.getAttributeClassification()); ClassDescriptor descriptorB = helper.getSession().getClassDescriptorForAlias("SimpleB"); assertNotNull("No descriptor found for alias='SimpleB'", descriptorB); DynamicType simpleTypeB = helper.getType("SimpleB"); assertNotNull("'SimpleB' EntityType not found", simpleTypeB); assertEquals(descriptorB, simpleTypeB.getDescriptor()); DirectToFieldMapping b_id = (DirectToFieldMapping) descriptorB.getMappingForAttributeName("id"); assertEquals(int.class, b_id.getAttributeClassification()); DirectToFieldMapping b_value1 = (DirectToFieldMapping) descriptorB.getMappingForAttributeName("value1"); assertEquals(String.class, b_value1.getAttributeClassification()); OneToManyMapping a_b = (OneToManyMapping) descriptorA.getMappingForAttributeName("b"); assertEquals(descriptorB, a_b.getReferenceDescriptor()); }
@Test public void removeAwithB_PrivateOwned() { createAwithB(); DynamicType simpleAType = helper.getType("SimpleA"); ((OneToManyMapping) simpleAType.getDescriptor().getMappingForAttributeName("b")) .setIsPrivateOwned(true); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); DynamicEntity a = (DynamicEntity) em.find(simpleAType.getJavaClass(), 1); assertNotNull(a); assertEquals(1, a.<Collection>get("b").size()); em.remove(a); // em.remove(a.get("b", List.class).get(0)); em.getTransaction().commit(); }
@Test public void createSimpleB() { DynamicType simpleTypeB = helper.getType("SimpleB"); assertNotNull(simpleTypeB); EntityManager em = emf.createEntityManager(); DynamicEntity simpleInstance = simpleTypeB.newDynamicEntity(); simpleInstance.set("id", 1); simpleInstance.set("value1", "B1"); em.getTransaction().begin(); em.persist(simpleInstance); em.getTransaction().commit(); int simpleCount = ((Number) em.createQuery("SELECT COUNT(s) FROM SimpleB s").getSingleResult()).intValue(); assertEquals(1, simpleCount); em.close(); }