List of usage examples for org.hibernate SessionFactory getCollectionMetadata
@Deprecated CollectionMetadata getCollectionMetadata(String roleName);
From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java
License:Open Source License
/** * Get the inverse of a one-to-many role * @param entityName The entity owning the role * @param rName The role name//from w w w . j a v a 2s.com * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role */ private String[] getInverseOfCollectionRole(String entityName, String rName) { String[] result = new String[2]; SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory(); AbstractCollectionPersister parentMeta = (AbstractCollectionPersister) sessFact .getCollectionMetadata(entityName + "." + rName); if (parentMeta == null) { // Could be inherited -- search through superclasses while (parentMeta == null) { Class<?> cls = null; if (getMetaEntity().getEntityType() == MetaEntity.EntityType.POJO) cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.POJO); else cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.MAP); Class<?> superCls = cls.getSuperclass(); if (superCls.getName().equals("java.lang.Object")) throw new RuntimeException( "Unable to retrieve Hibernate information for collection " + entityName + "." + rName); ClassMetadata clsMeta = sessFact.getClassMetadata(superCls); if (clsMeta == null) throw new RuntimeException("Unable to retrieve Hibernate information for collection " + entityName + "." + rName + ", even from superclass(es)"); entityName = clsMeta.getEntityName(); parentMeta = (AbstractCollectionPersister) sessFact.getCollectionMetadata(entityName + "." + rName); } } String[] colNames = parentMeta.getKeyColumnNames(); String childName = parentMeta.getElementType().getName(); AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(childName); String[] propNames = childMeta.getPropertyNames(); for (int i = 0; i < propNames.length; i++) { Type type = childMeta.getPropertyType(propNames[i]); if (!type.isEntityType()) continue; EntityType entType = (EntityType) type; if (!entType.getAssociatedEntityName().equals(entityName)) continue; String[] cnames = childMeta.getPropertyColumnNames(i); if (cnames.length != colNames.length) continue; boolean columnMatch = true; for (int j = 0; j < cnames.length; j++) { if (!cnames[j].equals(colNames[j])) { columnMatch = false; break; } } if (columnMatch) { result[0] = childName; result[1] = propNames[i]; return result; } } return null; }
From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java
License:Open Source License
/** * Get the inverse of a many-to-one role * @param entityName The entity owning the role * @param rName The role name// w ww .ja v a 2 s.co m * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role */ private String[] getInverseOfSingleRole(String entityName, String rName) { String[] result = new String[2]; SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory(); AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(entityName); int propIdx = childMeta.getPropertyIndex(rName); String[] cnames = childMeta.getPropertyColumnNames(propIdx); Type parentType = childMeta.getPropertyType(rName); if (parentType instanceof OneToOneType) return getInverseOfOneToOneRole(entityName, rName); if (!(parentType instanceof ManyToOneType)) throw new RuntimeException("Inverse of single-valued role " + entityName + "." + rName + " is neither single-valued not multi-valued"); ManyToOneType manyType = (ManyToOneType) parentType; String parentEntityName = manyType.getAssociatedEntityName(); AbstractEntityPersister parentMeta = (AbstractEntityPersister) sessFact.getClassMetadata(parentEntityName); String[] propNames = parentMeta.getPropertyNames(); for (int i = 0; i < propNames.length; i++) { Type type = parentMeta.getPropertyType(propNames[i]); if (!type.isCollectionType()) continue; CollectionType collType = (CollectionType) type; if (!collType.getAssociatedEntityName((SessionFactoryImplementor) sessFact).equals(entityName)) continue; AbstractCollectionPersister persister = (AbstractCollectionPersister) sessFact .getCollectionMetadata(parentEntityName + "." + propNames[i]); String[] colNames = persister.getKeyColumnNames(); if (cnames.length != colNames.length) continue; boolean columnMatch = true; for (int j = 0; j < cnames.length; j++) { if (!cnames[j].equals(colNames[j])) { columnMatch = false; break; } } if (columnMatch) { result[0] = parentEntityName; result[1] = propNames[i]; return result; } } return null; }
From source file:org.babyfish.hibernate.internal.SessionImplWrapper.java
License:Open Source License
@SuppressWarnings("unchecked") protected static void delete(XSessionImplementor sessionProxy, Object object) { if (object != null) { SessionFactory sessionFactory = sessionProxy.getRawSessionImpl().getSessionFactory(); PersistenceContext persistenceContext = ((org.hibernate.internal.SessionImpl) sessionProxy .getRawSessionImpl()).getPersistenceContext(); Map<PersistentCollection, CollectionEntry> collectionEntries = persistenceContext .getCollectionEntries(); for (Entry<PersistentCollection, CollectionEntry> entry : collectionEntries.entrySet()) { PersistentCollection persistentCollection = entry.getKey(); if (persistentCollection.wasInitialized()) { CollectionMetadata collectionMetadata = sessionFactory .getCollectionMetadata(persistentCollection.getRole()); Class<?> elementClass = collectionMetadata.getElementType().getReturnedClass(); if (elementClass.isAssignableFrom(object.getClass())) { if (persistentCollection instanceof Map<?, ?>) { ((Map<?, ?>) persistentCollection).values().remove(object); } else if (persistentCollection instanceof Collection<?>) { ((Collection<?>) persistentCollection).remove(object); }/*from w w w. j a va 2 s . c o m*/ } } } Class<?> clazz = object.getClass(); Collection<HbmReference> hbmReferences = null; NavigableMap<Class<?>, Collection<HbmReference>> hbmReferencesByTargetClass = (NavigableMap<Class<?>, Collection<HbmReference>>) ((XSessionFactoryImplementor) sessionProxy .getFactory()).getInternalData(SessionFactoryImplWrapper.IDK_HBM_REFERENCES_BY_TARGET_CLASS); for (Entry<Class<?>, Collection<HbmReference>> entry : hbmReferencesByTargetClass.descendingMap() .entrySet()) { if (entry.getKey().isAssignableFrom(clazz)) { hbmReferences = entry.getValue(); break; } } if (hbmReferences != null) { EntityReferenceComparator<? super Object> referenceComparator = EntityReferenceComparator .getInstance(); Entry<Object, EntityEntry>[] entityEntries = persistenceContext.reentrantSafeEntityEntries(); if (entityEntries != null) { for (Entry<Object, EntityEntry> entry : entityEntries) { Object entity = entry.getKey(); if (Hibernate.isInitialized(entity)) { EntityPersister persister = entry.getValue().getPersister(); ClassMetadata classMetadata = persister.getClassMetadata(); for (HbmReference hbmReference : hbmReferences) { if (hbmReference.ownerMetadata == classMetadata) { Object expectedObject = persister.getPropertyValue(entity, hbmReference.propertyIndex); if (referenceComparator.same(expectedObject, object)) { persister.setPropertyValue(entity, hbmReference.propertyIndex, null); } } } } } } } } sessionProxy.getRawSessionImpl().delete(object); }
From source file:org.beanfuse.entity.context.HibernateEntityContext.java
License:Open Source License
private CollectionType buildCollectionType(SessionFactory sessionFactory, Class collectionClass, String role) { CollectionMetadata cm = sessionFactory.getCollectionMetadata(role); org.hibernate.type.Type type = cm.getElementType(); EntityType elementType = null;/*from w w w . ja v a 2 s . c om*/ if (type.isEntityType()) { elementType = (EntityType) entityTypes.get(type.getName()); if (null == elementType) { elementType = buildEntityType(sessionFactory, type.getName()); } } else { elementType = new EntityType(type.getReturnedClass()); } CollectionType collectionType = new CollectionType(); collectionType.setElementType(elementType); collectionType.setArray(cm.isArray()); collectionType.setCollectionClass(collectionClass); if (!collectionTypes.containsKey(collectionType.getName())) { collectionTypes.put(collectionType.getName(), collectionType); } return collectionType; }
From source file:org.beangle.commons.orm.hibernate.internal.HibernateEntityContext.java
License:Open Source License
private CollectionType buildCollectionType(SessionFactory sessionFactory, Class<?> collectionClass, String role) {//w w w . ja v a 2 s .c om CollectionMetadata cm = sessionFactory.getCollectionMetadata(role); // FIXME buildCollectionType if (null == cm) return null; org.hibernate.type.Type type = cm.getElementType(); EntityType elementType = null; if (type.isEntityType()) { elementType = (EntityType) entityTypes.get(type.getName()); if (null == elementType) elementType = buildEntityType(sessionFactory, type.getName()); } else { elementType = new EntityType(type.getReturnedClass()); } CollectionType collectionType = new CollectionType(); collectionType.setElementType(elementType); collectionType.setArray(cm.isArray()); collectionType.setCollectionClass(collectionClass); if (!collectionTypes.containsKey(collectionType.getName())) { collectionTypes.put(collectionType.getName(), collectionType); } return collectionType; }
From source file:org.beangle.model.persist.hibernate.HibernateEntityContext.java
License:Open Source License
private CollectionType buildCollectionType(SessionFactory sessionFactory, Class<?> collectionClass, String role) {/* ww w. j av a2s. c o m*/ CollectionMetadata cm = sessionFactory.getCollectionMetadata(role); // FIXME buildCollectionType if (null == cm) { return null; } org.hibernate.type.Type type = cm.getElementType(); EntityType elementType = null; if (type.isEntityType()) { elementType = (EntityType) entityTypes.get(type.getName()); if (null == elementType) { elementType = buildEntityType(sessionFactory, type.getName()); } } else { elementType = new EntityType(type.getReturnedClass()); } CollectionType collectionType = new CollectionType(); collectionType.setElementType(elementType); collectionType.setArray(cm.isArray()); collectionType.setCollectionClass(collectionClass); if (!collectionTypes.containsKey(collectionType.getName())) { collectionTypes.put(collectionType.getName(), collectionType); } return collectionType; }
From source file:org.beangle.orm.hibernate.internal.HibernateEntityContext.java
License:Open Source License
private CollectionType buildCollectionType(SessionFactory sessionFactory, Class<?> collectionClass, String role) {/*from ww w . j ava 2s . co m*/ CollectionMetadata cm = sessionFactory.getCollectionMetadata(role); // FIXME buildCollectionType in class hierarchy if (null == cm) return null; org.hibernate.type.Type type = cm.getElementType(); EntityType elementType = null; if (type.isEntityType()) { elementType = (EntityType) entityTypes.get(type.getName()); if (null == elementType) elementType = buildEntityType(sessionFactory, type.getName()); } else { elementType = new EntityType(type.getReturnedClass()); } CollectionType collectionType = new CollectionType(); collectionType.setElementType(elementType); collectionType.setArray(cm.isArray()); collectionType.setCollectionClass(collectionClass); if (!collectionTypes.containsKey(collectionType.getName())) { collectionTypes.put(collectionType.getName(), collectionType); } return collectionType; }
From source file:org.openmrs.module.sync.api.db.hibernate.HibernateSyncInterceptor.java
License:Open Source License
/** * Utility method that recursively fetches the CollectionMetadata for the specified collection * property and class from given SessionFactory object * * @param clazz the class in which the collection is defined * @param collPropertyName the collection's property name * @param sf SessionFactory object/*from w ww. j a v a 2s . c om*/ * @return the CollectionMetadata if any */ private CollectionMetadata getCollectionMetadata(Class<?> clazz, String collPropertyName, SessionFactory sf) { CollectionMetadata cmd = sf.getCollectionMetadata(clazz.getName() + "." + collPropertyName); //Recursively check if there is collection metadata for the superclass if (cmd == null && clazz.getSuperclass() != null && !Object.class.equals(clazz.getSuperclass())) { return getCollectionMetadata(clazz.getSuperclass(), collPropertyName, sf); } return cmd; }