List of usage examples for org.hibernate.type Type isCollectionType
boolean isCollectionType();
From source file:be.shad.tsqb.helper.TypeSafeQueryHelperImpl.java
License:Apache License
/** * Creates data based on the hibernate metadata for the given <code>property</code>. */// w w w .j av a 2s.c om TypeSafeQueryProxyData createChildData(TypeSafeQueryInternal query, TypeSafeQueryProxyData parent, String property) { Type propertyType = getTargetType(parent, property); Class<?> targetClass = getTargetEntityClass(propertyType); ClassMetadata metadata = getMetaData(targetClass); if (metadata == null && !propertyType.isComponentType()) { return query.getDataTree().createData(parent, property, targetClass); } TypeSafeQueryProxyType proxyType = null; if (metadata != null) { proxyType = propertyType.isCollectionType() ? EntityCollectionType : EntityType; } else { proxyType = propertyType instanceof ComponentType ? ComponentType : CompositeType; } TypeSafeQueryProxy proxy = (TypeSafeQueryProxy) proxyFactory.getProxy(targetClass, proxyType); TypeSafeQueryProxyData data = query.getDataTree().createData(parent, property, targetClass, proxyType, metadata == null ? null : metadata.getIdentifierPropertyName(), proxy); setEntityProxyMethodListener(query, proxy, data); return data; }
From source file:be.shad.tsqb.helper.TypeSafeQueryHelperImpl.java
License:Apache License
/** * */// ww w . java 2 s. c o m @Override public String getMappedByProperty(TypeSafeQueryProxyData child) { Type propertyType = getTargetType(child.getParent(), child.getPropertyPath()); if (!propertyType.isCollectionType()) { throw new IllegalArgumentException("Method not designed to fetch MappedByProperty " + "for a non-collection type. PropertyType was: " + propertyType); } CollectionMetadata collectionMetadata = (CollectionMetadata) metaModel .collectionPersister(((CollectionType) propertyType).getRole()); if (collectionMetadata instanceof OneToManyPersister) { OneToManyPersister persister = (OneToManyPersister) collectionMetadata; return persister.getMappedByProperty(); } // what about many to many? return null; }
From source file:com.autobizlogic.abl.logic.LogicSvcs.java
License:Open Source License
/** * /*from w w w . jav a 2s. c om*/ * @param aSourceHibernateBean * @param aLogicRunner - just for context * @return 2nd instance of aSourceHibernateBean, with non-collection properties */ public static Object beanCopyOf(Object aSourceHibernateBean, LogicRunner aLogicRunner) { Object rtnTargetHibernateBean = null; LogicTransactionContext context = aLogicRunner.getContext(); BigDecimal defaultValue = null; //new BigDecimal("0.0"); // an experiment Converter bdc = new BigDecimalConverter(defaultValue); ConvertUtils.register(bdc, BigDecimal.class); Class<?> hibernateDomainBeanClass = HibernateUtil.getEntityClassForBean(aSourceHibernateBean); ClassMetadata entityMeta = context.getSession().getSessionFactory() .getClassMetadata(hibernateDomainBeanClass); Type propertyTypes[] = entityMeta.getPropertyTypes(); String propertyNames[] = entityMeta.getPropertyNames(); // hope names/types share index... String className = hibernateDomainBeanClass.getName(); try { rtnTargetHibernateBean = ClassLoaderManager.getInstance().getClassFromName(className).newInstance(); } catch (Exception e) { throw new LogicException("Unable to instantatiate new Bean like: " + aSourceHibernateBean, e); } BeanMap rtnBeanMap = new BeanMap(rtnTargetHibernateBean); BeanMap srcBeanMap = new BeanMap(aSourceHibernateBean); Object eachValue = null; for (int i = 0; i < propertyNames.length; i++) { String propertyName = propertyNames[i]; try { Type type = propertyTypes[i]; if (!type.isCollectionType() && !type.isEntityType()) { // NB - not moving collections! eachValue = srcBeanMap.get(propertyName); rtnBeanMap.put(propertyName, eachValue); } else { BeanUtils.setProperty(rtnTargetHibernateBean, propertyNames[i], null); // prevent ClassCastException: HashSet cannot be cast to PersistentCollection } } catch (Exception e) { throw new LogicException("Cannot set Property: " + propertyNames[i] + " = " + eachValue + ", on " + rtnTargetHibernateBean); } } // TODO - IMPORTANT - set the key field(s), presumably using Hibernate meta data return rtnTargetHibernateBean; }
From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaEntity.java
License:Open Source License
/** * Read all the properties from Hibernate metadata *//*from w w w .j a va 2 s . com*/ private void loadAllProperties() { if (allPropsRetrieved) return; synchronized (this) { if (!allPropsRetrieved) { metaAttributes = new HashMap<String, MetaAttribute>(); metaRoles = new HashMap<String, MetaRole>(); ClassMetadata meta = persister.getClassMetadata(); String[] propNames = meta.getPropertyNames(); for (String propName : propNames) { Type type; try { type = persister.getClassMetadata().getPropertyType(propName); } catch (QueryException ex) { throw new RuntimeException("Unable to determine type for property " + propName + " of entity " + persister.getEntityName()); } if (type.isComponentType()) { // Do nothing } else if (type.isCollectionType() || type.isEntityType() || type.isAssociationType()) { boolean isParentToChild = type.isCollectionType(); MetaRole mr = new HibMetaRole(this, propName, isParentToChild); metaRoles.put(propName, mr); } else { MetaAttribute ma = new HibMetaAttribute(propName, type.getReturnedClass(), false); metaAttributes.put(propName, ma); } } // Often the primary attribute(s) is not returned by ClassMetadata.getPropertyNames // So we add it by hand here String pkName = meta.getIdentifierPropertyName(); if (pkName == null) { // Can happen for composite keys Type pkType = meta.getIdentifierType(); if (pkType.isComponentType()) { ComponentType ctype = (ComponentType) pkType; String[] pnames = ctype.getPropertyNames(); for (String pname : pnames) { MetaAttribute ma = new HibMetaAttribute(pname, meta.getPropertyType(pname).getReturnedClass(), false); metaAttributes.put(pname, ma); } } else throw new RuntimeException( "Unexpected: anonymous PK is not composite - class " + meta.getEntityName()); } else if (!metaAttributes.containsKey(pkName)) { MetaAttribute ma = new HibMetaAttribute(pkName, meta.getIdentifierType().getReturnedClass(), false); metaAttributes.put(pkName, ma); } allPropsRetrieved = true; } } }
From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java
License:Open Source License
/** * Get the MetaEntity at the other end of this role. *//*from ww w . ja v a 2 s .co m*/ @Override public HibMetaEntity getOtherMetaEntity() { if (otherMetaEntity != null) return otherMetaEntity; SessionFactoryImplementor sfi = (SessionFactoryImplementor) metaEntity.getMetaModel().getSessionFactory(); EntityPersister thisPers = metaEntity.getPersister(); Type type = thisPers.getPropertyType(roleName); if (type.isCollectionType() && !isCollection) throw new RuntimeException("Internal metadata inconsistency: role name " + roleName + " of " + metaEntity.getEntityName() + " is and isn't a collection"); else if (type.isEntityType() && isCollection) throw new RuntimeException("Internal metadata inconsistency: role name " + roleName + " of " + metaEntity.getEntityName() + " is and isn't an entity"); String otherEntityName = null; if (isCollection) { CollectionType ctype = (CollectionType) type; otherEntityName = ctype.getAssociatedEntityName(sfi); } else { EntityType etype = (EntityType) type; otherEntityName = etype.getAssociatedEntityName(sfi); } otherMetaEntity = (HibMetaEntity) metaEntity.getMetaModel().getMetaEntity(otherEntityName); if (otherMetaEntity == null) throw new RuntimeException("Unable to find entity " + otherEntityName + ", which is the value of role " + metaEntity.getEntityName() + "." + roleName); return otherMetaEntity; }
From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java
License:Open Source License
/** * Get the opposite role of the given role, if it exists * @param entityName The name of the entity who owns the role, e.g. com.foo.Customer * @param rName The role name, e.g. orders * @return Null if the role has no known inverse, or [entity name, role name] of the inverse *//* ww w.j av a 2 s.c o m*/ private String[] getInverseOfRole(String entityName, String rName) { SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory(); ClassMetadata classMeta = sessFact.getClassMetadata(entityName); Type propType = classMeta.getPropertyType(rName); if (propType.isCollectionType()) { return getInverseOfCollectionRole(entityName, rName); } if (propType.isEntityType()) { return getInverseOfSingleRole(entityName, rName); } log.debug("Role " + entityName + "." + rName + " is neither a collection type " + "nor an entity type, and will be assumed to have no inverse."); 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//from ww w .ja va 2 s .c o 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:com.ejushang.steward.common.genericdao.search.hibernate.HibernateEntityMetadata.java
License:Apache License
public Metadata getPropertyType(String property) { Type pType = metadata.getPropertyType(property); Class<?> pCollectionType = null; if (pType.isCollectionType()) { pType = ((CollectionType) pType).getElementType((SessionFactoryImplementor) sessionFactory); pCollectionType = pType.getReturnedClass(); }// w w w .j a va2 s.c o m if (pType.isEntityType()) { return new HibernateEntityMetadata(sessionFactory, sessionFactory.getClassMetadata(((EntityType) pType).getName()), pCollectionType); } else { return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType); } }
From source file:com.ejushang.steward.common.genericdao.search.hibernate.HibernateNonEntityMetadata.java
License:Apache License
public Metadata getPropertyType(String property) { if (!type.isComponentType()) return null; int i = getPropertyIndex(property); if (i == -1) { return null; } else {/* w w w.j av a 2s. com*/ Type pType = ((ComponentType) type).getSubtypes()[i]; Class<?> pCollectionType = null; if (pType.isCollectionType()) { pType = ((CollectionType) pType).getElementType((SessionFactoryImplementor) sessionFactory); pCollectionType = pType.getReturnedClass(); } if (pType.isEntityType()) { return new HibernateEntityMetadata(sessionFactory, sessionFactory.getClassMetadata(((EntityType) pType).getName()), pCollectionType); } else { return new HibernateNonEntityMetadata(sessionFactory, pType, pCollectionType); } } }
From source file:com.fiveamsolutions.nci.commons.audit.AuditLogInterceptor.java
License:Open Source License
private boolean needsAuditing(Object auditableObj, Type type, Object newValue, Object oldValue, String property) {/*from ww w. j av a 2 s . c om*/ if (type.isCollectionType()) { return collectionNeedsAuditing(auditableObj, newValue, oldValue, property); } if (type.isEntityType()) { if (newValue != null && !processor.isAuditableEntity(newValue)) { return false; } if (oldValue != null && !processor.isAuditableEntity(oldValue)) { return false; } } return !ObjectUtils.equals(newValue, oldValue); }