@Override public Class getPropertyType(String name) { PersistentProperty property = persistentEntity.getPropertyByName(name); if(property != null) { return property.getType(); } return null; }
Object resolvePropertyValue(PersistentEntity entity, String property, Object value) { PersistentProperty persistentProperty = entity.getPropertyByName(property); Object resolved; if(persistentProperty instanceof Embedded) { resolved = value; } else { resolved = resolveIdIfEntity(value); } return resolved; }
private void buildUpdateStatement(StringBuilder queryString, Map<String, Object> propertiesToUpdate, List parameters, boolean hibernateCompatible) { queryString.append(SPACE).append("SET"); // keys need to be sorted before query is built Set<String> keys = new TreeSet<String>(propertiesToUpdate.keySet()); Iterator<String> iterator = keys.iterator(); while (iterator.hasNext()) { String propertyName = iterator.next(); PersistentProperty prop = entity.getPropertyByName(propertyName); if (prop == null) throw new InvalidDataAccessResourceUsageException("Property '"+propertyName+"' of class '"+entity.getName()+"' specified in update does not exist"); parameters.add(propertiesToUpdate.get(propertyName)); queryString.append(SPACE).append(logicalName).append(DOT).append(propertyName).append('='); queryString.append(PARAMETER_PREFIX).append(parameters.size()); if (iterator.hasNext()) { queryString.append(COMMA); } } }
@Override public void convertArguments(PersistentEntity persistentEntity) { ConversionService conversionService = persistentEntity .getMappingContext().getConversionService(); String propertyName = this.propertyName; PersistentProperty<?> prop = persistentEntity .getPropertyByName(propertyName); Object[] arguments = this.arguments; convertArgumentsForProp(persistentEntity, prop, propertyName, arguments, conversionService); } }
@Override public void convertArguments(PersistentEntity persistentEntity) { ConversionService conversionService = persistentEntity .getMappingContext().getConversionService(); PersistentProperty<?> prop = persistentEntity .getPropertyByName(propertyName); convertArgumentsForProp(persistentEntity, prop, propertyName, arguments, conversionService); }
protected void validatePropertyName(String propertyName, String methodName) { if (persistentEntity == null) return; if (propertyName == null) { throw new IllegalArgumentException("Cannot use [" + methodName + "] restriction with null property name"); } PersistentProperty property = persistentEntity.getPropertyByName(propertyName); if (property == null && persistentEntity.getIdentity().getName().equals(propertyName)) { property = persistentEntity.getIdentity(); } if (property == null && !queryCreator.isSchemaless()) { throw new IllegalArgumentException("Property [" + propertyName + "] is not a valid property of class [" + persistentEntity + "]"); } }
protected void createKeyForProps(PersistentProperty grailsProp, String path, Table table, String columnName, List<?> propertyNames, String sessionFactoryBeanName) { List<Column> keyList = new ArrayList<Column>(); keyList.add(new Column(columnName)); for (Object po : propertyNames) { String propertyName = (String) po; PersistentProperty otherProp = grailsProp.getOwner().getPropertyByName(propertyName); String otherColumnName = getColumnNameForPropertyAndPath(otherProp, path, null, sessionFactoryBeanName); keyList.add(new Column(otherColumnName)); } createUniqueKeyForColumns(table, columnName, keyList); }
private int calculateForeignKeyColumnCount(PersistentEntity refDomainClass, String[] propertyNames) { int expectedForeignKeyColumnLength = 0; for (String propertyName : propertyNames) { PersistentProperty referencedProperty = refDomainClass.getPropertyByName(propertyName); if(referencedProperty instanceof ToOne) { ToOne toOne = (ToOne) referencedProperty; PersistentProperty[] compositeIdentity = toOne.getAssociatedEntity().getCompositeIdentity(); if(compositeIdentity != null) { expectedForeignKeyColumnLength += compositeIdentity.length; } else { expectedForeignKeyColumnLength++; } } else { expectedForeignKeyColumnLength++; } } return expectedForeignKeyColumnLength; }
@SuppressWarnings("unchecked") static void doTypeConversionIfNeccessary(PersistentEntity entity, PropertyCriterion pc) { if (pc.getClass().getSimpleName().startsWith(SIZE_CONSTRAINT_PREFIX)) { return; } String property = pc.getProperty(); Object value = pc.getValue(); PersistentProperty p = entity.getPropertyByName(property); if (p != null && !p.getType().isInstance(value)) { pc.setValue(conversionService.convert(value, p.getType())); } }
static void doTypeConversionIfNeccessary(PersistentEntity entity, PropertyCriterion pc) { if(!pc.getClass().getSimpleName().startsWith(SIZE_CONSTRAINT_PREFIX)) { String property = pc.getProperty(); Object value = pc.getValue(); PersistentProperty p = entity.getPropertyByName(property); if(p != null && !p.getType().isInstance(value)) { pc.setValue( conversionService.convert(value, p.getType())); } } }
@Override public PersistentProperty getPropertyByName(String name) { if(name != null && name.contains(".")) { String[] props = name.split("\\."); // Get the embedded property type PersistentProperty embeddedProp = super.getPropertyByName(props[0]); if( embeddedProp instanceof Embedded) { PersistentEntity embeddedEntity = ((Embedded) embeddedProp).getAssociatedEntity(); return embeddedEntity.getPropertyByName(props[1]); } return super.getPropertyByName(name); } else { return super.getPropertyByName(name); } }
protected void storeDateCreatedAndLastUpdatedInfo(PersistentEntity persistentEntity) { if(persistentEntity.isInitialized()) { ClassMapping<?> classMapping = persistentEntity.getMapping(); Entity mappedForm = classMapping.getMappedForm(); if(mappedForm == null || mappedForm.isAutoTimestamp()) { storeTimestampAvailability(entitiesWithDateCreated, persistentEntity, persistentEntity.getPropertyByName(DATE_CREATED_PROPERTY)); storeTimestampAvailability(entitiesWithLastUpdated, persistentEntity, persistentEntity.getPropertyByName(LAST_UPDATED_PROPERTY)); } } else { uninitializedEntities.add(persistentEntity.getName()); } }
/** * Obtain the fetch strategy for the given property * * @param property The fetch strategy * @return A specific strategy or lazy by default */ protected FetchType fetchStrategy(String property) { final FetchType fetchType = fetchStrategies.get(property); if(fetchType != null) { return fetchType; } else { final PersistentProperty prop = entity.getPropertyByName(property); if(prop != null) { return prop.getMapping().getMappedForm().getFetchStrategy(); } } return FetchType.LAZY; }
private static PersistentProperty validateProperty(PersistentEntity entity, String name, Class criterionType) { PersistentProperty identity = entity.getIdentity(); if (identity != null && identity.getName().equals(name)) { return identity; } PersistentProperty[] compositeIdentity = ((AbstractPersistentEntity) entity).getCompositeIdentity(); if(compositeIdentity != null) { for (PersistentProperty property : compositeIdentity) { if(property.getName().equals(name)) { return property; } } } PersistentProperty prop = entity.getPropertyByName(name); if (prop == null) { throw new InvalidDataAccessResourceUsageException("Cannot use [" + criterionType.getSimpleName() + "] criterion on non-existent property: " + name); } return prop; }
/** * Creates an association query * * @param associationName The assocation name * @return The Query instance */ public AssociationQuery createQuery(String associationName) { final PersistentProperty property = entity.getPropertyByName(associationName); if (property == null || !(property instanceof Association)) { throw new InvalidDataAccessResourceUsageException("Cannot query association [" + associationName + "] of class [" + entity + "]. The specified property is not an association."); } Association association = (Association) property; final PersistentEntity associatedEntity = association.getAssociatedEntity(); return new AssociationQuery(session, associatedEntity, association); }
@Override public boolean isInherited() { if (inherited == null) { if (owner.isRoot()) { inherited = false; } else { PersistentEntity parentEntity = owner.getParentEntity(); boolean foundInParent = false; while (parentEntity != null) { final PersistentProperty p = parentEntity.getPropertyByName(name); if (p != null) { foundInParent = true; break; } parentEntity = parentEntity.getParentEntity(); } inherited = foundInParent; } } return inherited; }
/** * Add order to criteria, creating necessary subCriteria if nested sort property (ie. sort:'nested.property'). */ private static void addOrderPossiblyNested(Criteria c, PersistentEntity entity, String sort, String order, boolean ignoreCase) { int firstDotPos = sort.indexOf("."); if (firstDotPos == -1) { addOrder(c, sort, order, ignoreCase); } else { // nested property String sortHead = sort.substring(0,firstDotPos); String sortTail = sort.substring(firstDotPos+1); PersistentProperty property = entity.getPropertyByName(sortHead); if (property instanceof Embedded) { // embedded objects cannot reference entities (at time of writing), so no more recursion needed addOrder(c, sort, order, ignoreCase); } else if(property instanceof Association) { Association a = (Association) property; Criteria subCriteria = c.createCriteria(sortHead); PersistentEntity associatedEntity = a.getAssociatedEntity(); Class<?> propertyTargetClass = associatedEntity.getJavaClass(); cacheCriteriaByMapping(propertyTargetClass, subCriteria); addOrderPossiblyNested( subCriteria, associatedEntity, sortTail, order, ignoreCase); // Recurse on nested sort } } }
/** * @return The inverse side or null if the association is not bidirectional */ public Association getInverseSide() { final PersistentProperty associatedProperty = associatedEntity.getPropertyByName(referencedPropertyName); if (associatedProperty == null) return null; if (associatedProperty instanceof Association) { return (Association) associatedProperty; } throw new IllegalMappingException("The inverse side [" + associatedEntity.getName() + "." + associatedProperty.getName() + "] of the association [" + getOwner().getName() + "." + getName() + "] is not valid. Associations can only map to other entities and collection types."); }
@Override public AssociationQuery createQuery(String associationName) { final PersistentProperty property = entity.getPropertyByName(calculatePropertyName(associationName)); if (property != null && (property instanceof Association)) { @SuppressWarnings("hiding") String alias = generateAlias(associationName); CriteriaAndAlias subCriteria = getOrCreateAlias(associationName, alias); Association association = (Association) property; return new HibernateAssociationQuery(subCriteria.criteria, (HibernateSession) getSession(), association.getAssociatedEntity(), association, alias); } throw new InvalidDataAccessApiUsageException("Cannot query association [" + calculatePropertyName(associationName) + "] of entity [" + entity + "]. Property is not an association!"); }
@Override public AssociationQuery createQuery(String associationName) { final PersistentProperty property = entity.getPropertyByName(calculatePropertyName(associationName)); if (property != null && (property instanceof Association)) { String alias = generateAlias(associationName); CriteriaAndAlias subCriteria = getOrCreateAlias(associationName, alias); Association association = (Association) property; if(subCriteria.criteria != null) { return new HibernateAssociationQuery(subCriteria.criteria, (AbstractHibernateSession) getSession(), association.getAssociatedEntity(), association, alias); } else if(subCriteria.detachedCriteria != null) { return new HibernateAssociationQuery(subCriteria.detachedCriteria, (AbstractHibernateSession) getSession(), association.getAssociatedEntity(), association, alias); } } throw new InvalidDataAccessApiUsageException("Cannot query association [" + calculatePropertyName(associationName) + "] of entity [" + entity + "]. Property is not an association!"); }