List of usage examples for org.hibernate.mapping OneToMany getAssociatedClass
public PersistentClass getAssociatedClass()
From source file:gov.nih.nci.system.util.ClassCache.java
License:BSD License
@SuppressWarnings("unchecked") private Map<String, List<Object>> processIfAssociationType(Property property, String fieldName, Configuration cfg) {/*w w w .j a v a2 s .co m*/ Map<String, List<Object>> associationPsFields = new HashMap<String, List<Object>>(); org.hibernate.mapping.Set childAssociationType = (org.hibernate.mapping.Set) property.getValue(); Object element = childAssociationType.getElement(); Class<? extends Value> elementClass = childAssociationType.getElement().getClass(); if (Component.class.isAssignableFrom(elementClass)) { Component associationComponent = (Component) element; Iterator<Property> propertiesIterator = associationComponent.getPropertyIterator(); String assoChildCompClassName = associationComponent.getComponentClassName(); String key = fieldName + "<" + assoChildCompClassName + ">"; List<Object> isoPersistentFields = new ArrayList<Object>(); while (propertiesIterator.hasNext()) { Property tempProperty = propertiesIterator.next(); List<Object> tempPersistentFields = getPersistentFieldsForISOObject(tempProperty); Class<? extends Value> tempPropertyClass = tempProperty.getValue().getClass(); if (Component.class.isAssignableFrom(tempPropertyClass)) { Map<String, List<Object>> nestedComponent = new HashMap<String, List<Object>>(); nestedComponent.put(tempProperty.getName(), tempPersistentFields); isoPersistentFields.add(nestedComponent); } else { isoPersistentFields.addAll(tempPersistentFields); } } associationPsFields.put(key, isoPersistentFields); } else if (element instanceof ManyToOne) { ManyToOne manyToOne = (ManyToOne) childAssociationType.getElement(); String many2OnePClassName = manyToOne.getReferencedEntityName(); if (!many2OnePClassName.startsWith("_xxEntityxx_gov_nih_nci_cacoresdk_domain_other_datatype")) { return associationPsFields; } PersistentClass many2OnePClass = cfg.getClassMapping(many2OnePClassName); Map<String, List<Object>> map = getISOPropertiesForObject(many2OnePClass, cfg); Iterator<String> keyItr = map.keySet().iterator(); String key = fieldName + "<" + many2OnePClass.getClassName() + ">"; List<Object> isoPersistentFields = new ArrayList<Object>(); isoPersistentFields.add(map); associationPsFields.put(key, isoPersistentFields); } else if (element instanceof OneToMany) { OneToMany oneToMany = (OneToMany) element;//prop.getValue(); String oneToManyPClassName = oneToMany.getReferencedEntityName(); if (!oneToManyPClassName.startsWith("_xxEntityxx_gov_nih_nci_cacoresdk_domain_other_datatype")) { return associationPsFields; } PersistentClass oneToManyPClass = cfg.getClassMapping(oneToManyPClassName); Map<String, List<Object>> map = getISOPropertiesForObject(oneToManyPClass, cfg); Iterator<String> keyItr = map.keySet().iterator(); String key = fieldName + "<" + oneToMany.getAssociatedClass().getClassName() + ">"; List<Object> isoPersistentFields = new ArrayList<Object>(); isoPersistentFields.add(map); associationPsFields.put(key, isoPersistentFields); } else { log.info("ignoring :::" + elementClass.getName()); } return associationPsFields; }
From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java
License:Open Source License
/** * Extra lazy mapping for lists needs a real property for the list index and * a real inverse for the other side as well. * /*from ww w .j av a2 s . c o m*/ * This method iterates over all associations and adds an inverse for the * list and set mappings. */ protected void addExtraLazyInverseProperties() { final Map<String, PersistentClass> persistentClasses = new HashMap<String, PersistentClass>(); for (Iterator<?> pcs = getClassMappings(); pcs.hasNext();) { final PersistentClass pc = (PersistentClass) pcs.next(); if (isClassOrSuperClassEAVMapped(pc)) { continue; } persistentClasses.put(pc.getEntityName(), pc); } for (Iterator<?> pcs = getClassMappings(); pcs.hasNext();) { final PersistentClass pc = (PersistentClass) pcs.next(); // copy to prevent concurrent modification final Iterator<?> propIt = pc.getPropertyIterator(); final List<Property> props = new ArrayList<Property>(); while (propIt.hasNext()) { final Property prop = (Property) propIt.next(); props.add(prop); } for (Property prop : props) { EClass eClass = null; if (pc.getMetaAttribute(HbMapperConstants.FEATUREMAP_META) == null) { if (pc.getEntityName() != null) { eClass = getEntityNameStrategy().toEClass(pc.getEntityName()); } else { eClass = EModelResolver.instance().getEClass(pc.getMappedClass()); } } final EStructuralFeature ef = eClass == null ? null : StoreUtil.getEStructuralFeature(eClass, prop.getName()); if (ef != null && ef instanceof EReference && prop.getValue() instanceof Collection) { final Collection collection = (Collection) prop.getValue(); final EReference eReference = (EReference) ef; // only work for extra lazy if (!collection.isExtraLazy()) { continue; } final Value elementValue = collection.getElement(); final PersistentClass elementPC; if (elementValue instanceof OneToMany) { final OneToMany oneToMany = (OneToMany) elementValue; elementPC = oneToMany.getAssociatedClass(); } else if (elementValue instanceof ManyToOne) { final ManyToOne mto = (ManyToOne) elementValue; elementPC = persistentClasses.get(mto.getReferencedEntityName()); } else { continue; } if (isClassOrSuperClassEAVMapped(elementPC)) { continue; } collection.setInverse(true); // and add an eopposite if (eReference.getEOpposite() == null) { final Table collectionTable = collection.getCollectionTable(); if (isClassOrSuperClassEAVMapped(elementPC)) { continue; } final Property inverseRefProperty = new Property(); inverseRefProperty.setName(StoreUtil.getExtraLazyInversePropertyName(ef)); final Map<Object, Object> metas = new HashMap<Object, Object>(); final MetaAttribute metaAttribute = new MetaAttribute( HbConstants.SYNTHETIC_PROPERTY_INDICATOR); metaAttribute.addValue("true"); metas.put(HbConstants.SYNTHETIC_PROPERTY_INDICATOR, metaAttribute); inverseRefProperty.setMetaAttributes(metas); inverseRefProperty.setNodeName(inverseRefProperty.getName()); inverseRefProperty.setPropertyAccessorName(SyntheticPropertyHandler.class.getName()); inverseRefProperty.setLazy(false); final ManyToOne mto = new ManyToOne(getMappings(), collectionTable); mto.setReferencedEntityName(pc.getEntityName()); mto.setLazy(false); mto.setFetchMode(FetchMode.SELECT); inverseRefProperty.setValue(mto); final Iterator<?> it = collection.getKey().getColumnIterator(); while (it.hasNext()) { final Column originalColumn = (Column) it.next(); // final Column newColumn = new // Column(originalColumn.getName()); mto.addColumn(originalColumn); } mto.createForeignKey(); // now determine if a join should be created if (collectionTable.getName().equalsIgnoreCase(elementPC.getTable().getName())) { elementPC.addProperty(inverseRefProperty); } else { // create a join final Join join = new Join(); join.setPersistentClass(elementPC); join.setTable(collectionTable); join.addProperty(inverseRefProperty); final ManyToOne keyValue = new ManyToOne(getMappings(), collectionTable); join.setKey(keyValue); @SuppressWarnings("unchecked") final Iterator<Column> keyColumns = collection.getElement().getColumnIterator(); while (keyColumns.hasNext()) { keyValue.addColumn(keyColumns.next()); } keyValue.setReferencedEntityName(elementPC.getEntityName()); keyValue.setTable(collectionTable); keyValue.createForeignKey(); elementPC.addJoin(join); } } // add an opposite index if (collection.isIndexed() && !collection.isMap()) { Table collectionTable = collection.getCollectionTable(); IndexedCollection indexedCollection = (IndexedCollection) collection; final Column column = (Column) indexedCollection.getIndex().getColumnIterator().next(); final Property indexProperty = new Property(); indexProperty.setName(StoreUtil.getExtraLazyInverseIndexPropertyName(ef)); final Map<Object, Object> metas = new HashMap<Object, Object>(); final MetaAttribute metaAttribute = new MetaAttribute( HbConstants.SYNTHETIC_PROPERTY_INDICATOR); metaAttribute.addValue("true"); metas.put(HbConstants.SYNTHETIC_PROPERTY_INDICATOR, metaAttribute); indexProperty.setMetaAttributes(metas); indexProperty.setNodeName(indexProperty.getName()); indexProperty.setPropertyAccessorName(SyntheticPropertyHandler.class.getName()); // always make this nullable, nullability is controlled // by the main property indexProperty.setOptional(true); Join join = null; @SuppressWarnings("unchecked") final Iterator<Join> it = (Iterator<Join>) elementPC.getJoinIterator(); while (it.hasNext()) { final Join foundJoin = it.next(); if (foundJoin.getTable().getName().equalsIgnoreCase(collectionTable.getName())) { join = foundJoin; collectionTable = join.getTable(); break; } } final SimpleValue sv = new SimpleValue(getMappings(), indexedCollection.getIndex().getTable()); sv.setTypeName("integer"); // final Column svColumn = new Column(column.getName()); sv.addColumn(column); // checkColumnExists(collectionTable, // svColumn)); indexProperty.setValue(sv); if (join != null) { join.addProperty(indexProperty); } else { elementPC.addProperty(indexProperty); } } } } } }
From source file:org.wintersleep.codeviz.uml.model.HibernateModelFactory.java
License:Apache License
private void addRelations(CodeModel model) throws ClassNotFoundException { Iterator<PersistentClass> pci = configuration.getClassMappings(); while (pci.hasNext()) { PersistentClass persistentClass = pci.next(); final ModelClass mc = model.findModelClass(persistentClass.getMappedClass()); Iterator<Property> pi = persistentClass.getPropertyIterator(); while (pi.hasNext()) { Property property = pi.next(); System.out.println(property); Value value = property.getValue(); if (value instanceof ToOne) { ToOne toOne = (ToOne) value; ModelClass toClass = model.findModelClass(Class.forName(toOne.getReferencedEntityName())); RelationEndpoint fromEndpoint = new RelationEndpoint(mc, property.getName()); fromEndpoint.setMinCardinality(toOne.isNullable() ? 0 : 1); RelationEndpoint toEndpoint = new RelationEndpoint(toClass, toOne.getReferencedPropertyName()); toEndpoint.setMinCardinality(0); toEndpoint.setMaxCardinality(RelationEndpoint.MANY_CARDINALITY); if (value instanceof OneToOne) { fromEndpoint.setMaxCardinality(1); } else if (value instanceof ManyToOne) { fromEndpoint.setMaxCardinality(1); }//w w w.j a va 2 s . co m mc.addRelationTo(fromEndpoint, toEndpoint); } else if (value instanceof OneToMany) { OneToMany oneToMany = (OneToMany) value; oneToMany.getAssociatedClass(); ModelClass toClass = model.findModelClass(oneToMany.getAssociatedClass().getMappedClass()); RelationEndpoint fromEndpoint = new RelationEndpoint(mc, property.getName()); fromEndpoint.setMinCardinality(0); fromEndpoint.setMaxCardinality(RelationEndpoint.MANY_CARDINALITY); RelationEndpoint toEndpoint = new RelationEndpoint(toClass, "TODO other"); toEndpoint.setMinCardinality(oneToMany.isNullable() ? 0 : 1); toEndpoint.setMaxCardinality(1); mc.addRelationTo(fromEndpoint, toEndpoint); } } } }