List of usage examples for org.hibernate.type Type isComponentType
boolean isComponentType();
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 2 s. co m*/ 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:com.autobizlogic.abl.metadata.hibernate.HibMetaEntity.java
License:Open Source License
/** * Read all the properties from Hibernate metadata */// ww w . j av a 2s . c o m 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.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java
License:Apache License
@Override public Map<String, String> getWritableMappedByMappings(EntityType<?> inverseType, EntityType<?> ownerType, String attributeName, String inverseAttribute) { AbstractEntityPersister entityPersister = getEntityPersister(ownerType); int propertyIndex = entityPersister.getEntityMetamodel().getPropertyIndex(attributeName); Type propertyType = entityPersister.getPropertyTypes()[propertyIndex]; org.hibernate.type.EntityType ownerPropertyType; if (propertyType instanceof CollectionType) { QueryableCollection persister = getCollectionPersister(ownerType, attributeName); AbstractEntityPersister inversePersister = getEntityPersister(inverseType); if (!persister.isInverse() && persister.getTableName().equals(inversePersister.getTableName())) { // We have a one-to-many relationship that has just join columns // Find properties for element columns in entityPersister // Map to properties for key columns of inverseType Set<String> elementAttributes = getColumnMatchingAttributeNames(entityPersister, Arrays.asList((entityPersister.toColumns(attributeName)))); Set<String> keyAttributes = removeIdentifierAccess(ownerType, elementAttributes, inverseType, getColumnMatchingAttributeNames(inversePersister, Arrays.asList(persister.getKeyColumnNames()))); Map<String, String> mapping = new HashMap<>(); Iterator<String> elemAttrIter = elementAttributes.iterator(); Iterator<String> keyAttrIter = keyAttributes.iterator(); while (elemAttrIter.hasNext()) { mapping.put(elemAttrIter.next(), keyAttrIter.next()); }/* w ww. j ava2 s. c om*/ if (mapping.isEmpty()) { throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#" + attributeName + "' must be writable or the column must be part of the id!"); } return mapping; } else { // We only support detection when the inverse collection is writable if (entityPersister.getEntityMetamodel().getPropertyInsertability()[propertyIndex]) { return null; } throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#" + attributeName + "' must be writable!"); } } else { // Either the mapped by property is writable if (entityPersister.getEntityMetamodel().getPropertyInsertability()[propertyIndex]) { return null; } // Or the columns of the mapped by property are part of the target id ownerPropertyType = (org.hibernate.type.EntityType) propertyType; } AbstractEntityPersister sourceType = (AbstractEntityPersister) entityPersisters .get(ownerPropertyType.getAssociatedEntityName()); Type identifierType = ownerPropertyType.getIdentifierOrUniqueKeyType(entityPersister.getFactory()); String sourcePropertyPrefix; String[] sourcePropertyNames; if (identifierType.isComponentType()) { ComponentType componentType = (ComponentType) identifierType; sourcePropertyPrefix = sourceType.getIdentifierPropertyName() + "."; sourcePropertyNames = componentType.getPropertyNames(); } else { sourcePropertyPrefix = ""; sourcePropertyNames = new String[] { sourceType.getIdentifierPropertyName() }; } String[] targetColumnNames = entityPersister.getPropertyColumnNames(propertyIndex); Type targetIdType = entityPersister.getIdentifierType(); if (targetIdType.isComponentType()) { ComponentType targetIdentifierType = (ComponentType) entityPersister.getIdentifierType(); String targetPropertyPrefix = entityPersister.getIdentifierPropertyName() + "."; String[] identifierColumnNames = entityPersister.getIdentifierColumnNames(); String[] targetIdentifierTypePropertyNames = targetIdentifierType.getPropertyNames(); Map<String, String> mapping = new HashMap<>(); for (int i = 0; i < targetColumnNames.length; i++) { for (int j = 0; j < identifierColumnNames.length; j++) { if (targetColumnNames[i].equals(identifierColumnNames[j])) { mapping.put(sourcePropertyPrefix + sourcePropertyNames[i], targetPropertyPrefix + targetIdentifierTypePropertyNames[j]); break; } } } if (mapping.isEmpty()) { throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#" + attributeName + "' must be writable or the column must be part of the id!"); } return mapping; } else { String targetIdColumnName = entityPersister.getIdentifierColumnNames()[0]; if (!targetIdColumnName.equals(targetColumnNames[0])) { throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#" + attributeName + "' must be writable or the column must be part of the id!"); } Map<String, String> mapping = new HashMap<>(); mapping.put(sourcePropertyPrefix + sourcePropertyNames[0], entityPersister.getIdentifierPropertyName()); return mapping; } }
From source file:com.googlecode.hibernate.audit.listener.AuditSessionFactoryObserver.java
License:Open Source License
private AuditTypeField initializeAuditField(Session session, String auditTypeClassName, AuditType auditType, String propertyName, Type type) { AuditTypeField auditField = HibernateAudit.getAuditField(session, auditTypeClassName, propertyName); if (auditField == null) { auditField = new AuditTypeField(); auditField.setName(propertyName); auditField.setOwnerType(auditType); auditType.getAuditFields().add(auditField); AuditType auditFieldType = null; if (type.isCollectionType()) { auditFieldType = initializePrimitiveAuditType(session, type); Type elementType = ((CollectionType) type) .getElementType((SessionFactoryImplementor) session.getSessionFactory()); if (elementType.isCollectionType()) { // do nothing.. } else if (elementType.isComponentType()) { initializeComponentAuditType(session, (ComponentType) elementType); } else if (elementType.isEntityType()) { // do nothing .. it will be handled during the entity model // traverse } else { initializePrimitiveAuditType(session, elementType); }/*from w w w . j a v a2 s . c om*/ } else if (type.isComponentType()) { auditFieldType = initializeComponentAuditType(session, (CompositeType) type); } else if (type.isEntityType()) { auditFieldType = initializeEntityAuditType(session, ((EntityType) type).getName(), false); } else { auditFieldType = initializePrimitiveAuditType(session, type); } auditField.setFieldType(auditFieldType); session.save(auditField); updateMetaModel(session); } return auditField; }
From source file:com.googlecode.hibernate.audit.synchronization.work.AbstractAuditWorkUnit.java
License:Open Source License
protected void processProperty(Session session, AuditConfiguration auditConfiguration, AuditEvent auditEvent, Object object, String propertyName, Object propertyValue, Type propertyType, AuditObject auditObject) { if (!auditConfiguration.getExtensionManager().getAuditableInformationProvider().isAuditable(getEntityName(), propertyName)) {//from ww w .ja v a 2s. c o m return; } AuditObjectProperty property = null; if (propertyType.isEntityType()) { property = processEntityProperty(session, auditConfiguration, object, propertyName, propertyValue, propertyType, auditObject); } else if (propertyType.isCollectionType()) { // collection will be handled by collection event listeners } else if (propertyType.isComponentType()) { property = processComponentValue(session, auditConfiguration, auditEvent, auditObject, getEntityName(), object, propertyName, propertyValue, (CompositeType) propertyType); } else { property = createSimpleValue(session, auditConfiguration, auditObject, getEntityName(), object, propertyName, propertyType, propertyValue); } if (property != null) { AuditType auditType = null; if (propertyValue != null) { auditType = HibernateAudit.getAuditType(session, propertyValue.getClass().getName()); if (auditType == null) { // subclass that was not registered in the audit metadata - // use the base class auditType = property.getAuditField().getFieldType(); } } property.setAuditType(auditType); auditObject.getAuditObjectProperties().add(property); } }
From source file:com.googlecode.hibernate.audit.synchronization.work.AbstractCollectionAuditWorkUnit.java
License:Open Source License
protected void processElement(Session session, AuditConfiguration auditConfiguration, Object entityOwner, Object element, Type elementType, String propertyName, long index, EntityAuditObject auditObject, AuditEvent auditEvent) {/*from w w w. j ava2s .com*/ AuditTypeField auditField = HibernateAudit .getAuditField(session, auditConfiguration.getExtensionManager().getAuditableInformationProvider() .getAuditTypeClassName(auditConfiguration.getMetadata(), getEntityName()), propertyName); AuditObjectProperty property = null; if (elementType.isEntityType()) { Serializable id = null; if (element != null) { id = session.getSessionFactory() .getClassMetadata(((EntityType) elementType).getAssociatedEntityName()) .getIdentifier(element, (SessionImplementor) session); } property = new EntityObjectProperty(); property.setAuditObject(auditObject); property.setAuditField(auditField); property.setIndex(new Long(index)); ((EntityObjectProperty) property).setTargetEntityId( auditConfiguration.getExtensionManager().getPropertyValueConverter().toString(null, id)); } else if (elementType.isComponentType()) { CompositeType componentType = (CompositeType) elementType; property = new ComponentObjectProperty(); property.setAuditObject(auditObject); property.setAuditField(auditField); property.setIndex(new Long(index)); ComponentAuditObject targetComponentAuditObject = null; if (element != null) { targetComponentAuditObject = new ComponentAuditObject(); targetComponentAuditObject.setAuditEvent(auditEvent); targetComponentAuditObject.setParentAuditObject(auditObject); AuditType auditComponentType = HibernateAudit.getAuditType(session, auditConfiguration.getExtensionManager().getAuditableInformationProvider() .getAuditTypeClassName(auditConfiguration.getMetadata(), elementType)); targetComponentAuditObject.setAuditType(auditComponentType); for (int j = 0; j < componentType.getPropertyNames().length; j++) { String componentPropertyName = componentType.getPropertyNames()[j]; Type componentPropertyType = componentType.getSubtypes()[j]; Object componentPropertyValue = componentType.getPropertyValue(element, j, (SessionImplementor) session); processProperty(session, auditConfiguration, auditEvent, element, componentPropertyName, componentPropertyValue, componentPropertyType, targetComponentAuditObject); } } ((ComponentObjectProperty) property).setTargetComponentAuditObject(targetComponentAuditObject); } else if (elementType.isCollectionType()) { // collection of collections } else { property = new SimpleObjectProperty(); property.setAuditObject(auditObject); property.setAuditField(auditField); property.setIndex(new Long(index)); ((SimpleObjectProperty) property).setValue( auditConfiguration.getExtensionManager().getPropertyValueConverter().toString(null, element)); } if (property != null) { AuditType auditType = null; if (element != null) { auditType = HibernateAudit.getAuditType(session, element.getClass().getName()); if (auditType == null) { // subclass that was not registered in the audit metadata - use the base class auditType = property.getAuditField().getFieldType(); } } property.setAuditType(auditType); auditObject.getAuditObjectProperties().add(property); } }
From source file:com.javaetmoi.core.persistence.hibernate.LazyLoadingUtil.java
License:Apache License
@SuppressWarnings({ "rawtypes" }) private static void deepInflateProperty(Object propertyValue, Type propertyType, Session currentSession, Set<String> recursiveGuard) { if (propertyValue == null) { return; // null guard }//ww w . ja va 2 s . c o m if (propertyType.isEntityType()) { deepInflateEntity(currentSession, propertyValue, recursiveGuard); } else if (propertyType.isCollectionType()) { // Handle PersistentBag, PersistentList and PersistentIndentifierBag if (propertyValue instanceof List) { deepInflateCollection(currentSession, recursiveGuard, (List) propertyValue); } else if (propertyValue instanceof Map) { deepInflateMap(currentSession, recursiveGuard, (Map) propertyValue); } else if (propertyValue instanceof Set) { deepInflateCollection(currentSession, recursiveGuard, (Set) propertyValue); } else { throw new UnsupportedOperationException( "Unsupported collection type: " + propertyValue.getClass().getSimpleName()); } } else if (propertyType.isComponentType()) { if (propertyType instanceof ComponentType) { // i.e. @Embeddable annotation (see // https://github.com/arey/hibernate-hydrate/issues/1) deepInflateComponent(currentSession, propertyValue, (ComponentType) propertyType, recursiveGuard); } } }
From source file:com.miranteinfo.seam.hibernate.HibernateCascade.java
License:Open Source License
/** * Cascade an action to the child or children *//*ww w . ja v a 2s . co m*/ private void cascadeProperty(final Object child, final Type type, final CascadeStyle style, final Object anything, final boolean isCascadeDeleteEnabled) throws HibernateException { if (child != null) { if (type.isAssociationType()) { AssociationType associationType = (AssociationType) type; if (cascadeAssociationNow(associationType)) { cascadeAssociation(child, type, style, anything, isCascadeDeleteEnabled); } } else if (type.isComponentType()) { cascadeComponent(child, (AbstractComponentType) type, anything); } } }
From source file:com.miranteinfo.seam.hibernate.HibernateCascade.java
License:Open Source License
/** * Cascade an action to a collection/*from ww w .ja v a 2 s. c o m*/ */ private void cascadeCollection(final Object child, final CascadeStyle style, final Object anything, final CollectionType type) { CollectionPersister persister = eventSource.getFactory().getCollectionPersister(type.getRole()); Type elemType = persister.getElementType(); final int oldCascadeTo = cascadeTo; if (cascadeTo == AFTER_INSERT_BEFORE_DELETE) { cascadeTo = AFTER_INSERT_BEFORE_DELETE_VIA_COLLECTION; } //cascade to current collection elements if (elemType.isEntityType() || elemType.isAnyType() || elemType.isComponentType()) { cascadeCollectionElements(child, type, style, elemType, anything, persister.isCascadeDeleteEnabled()); } cascadeTo = oldCascadeTo; }
From source file:com.reignite.parser.Join.java
License:Open Source License
/** * Gets all the basic fields of the criteria entity * /*from www . ja v a 2 s . co m*/ * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws IntrospectionException */ public void populateFields() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IntrospectionException { Class<?> clazz = Class.forName(entity); Method getter = clazz.getMethod("get" + Character.toUpperCase(name.charAt(0)) + name.substring(1)); Class<?> joined = (Class<?>) ((ParameterizedType) getter.getGenericReturnType()) .getActualTypeArguments()[0]; ClassMetadata metadata = session.getSessionFactory().getClassMetadata(joined); expectedFields.add(name + "." + metadata.getIdentifierPropertyName()); projections.add(Projections.property(name + "." + metadata.getIdentifierPropertyName())); for (String fieldName : metadata.getPropertyNames()) { Type type = metadata.getPropertyType(fieldName); if (!type.isAnyType() && !type.isAssociationType() && !type.isComponentType() && !type.isCollectionType()) { String field = name + "." + fieldName; expectedFields.add(field); projections.add(Projections.property(field)); } } }