Search in sources :

Example 16 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class AnnotationBinder method isIdClassPkOfTheAssociatedEntity.

private static boolean isIdClassPkOfTheAssociatedEntity(InheritanceState.ElementsToProcess elementsToProcess, XClass compositeClass, PropertyData inferredData, PropertyData baseInferredData, AccessType propertyAccessor, Map<XClass, InheritanceState> inheritanceStatePerClass, MetadataBuildingContext context) {
    if (elementsToProcess.getIdPropertyCount() == 1) {
        final PropertyData idPropertyOnBaseClass = getUniqueIdPropertyFromBaseClass(inferredData, baseInferredData, propertyAccessor, context);
        final InheritanceState state = inheritanceStatePerClass.get(idPropertyOnBaseClass.getClassOrElement());
        if (state == null) {
            //while it is likely a user error, let's consider it is something that might happen
            return false;
        }
        final XClass associatedClassWithIdClass = state.getClassWithIdClass(true);
        if (associatedClassWithIdClass == null) {
            //we cannot know for sure here unless we try and find the @EmbeddedId
            //Let's not do this thorough checking but do some extra validation
            final XProperty property = idPropertyOnBaseClass.getProperty();
            return property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(OneToOne.class);
        } else {
            final XClass idClass = context.getBuildingOptions().getReflectionManager().toXClass(associatedClassWithIdClass.getAnnotation(IdClass.class).value());
            return idClass.equals(compositeClass);
        }
    } else {
        return false;
    }
}
Also used : OneToOne(javax.persistence.OneToOne) XProperty(org.hibernate.annotations.common.reflection.XProperty) XClass(org.hibernate.annotations.common.reflection.XClass) ManyToOne(javax.persistence.ManyToOne)

Example 17 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class AnnotationBinder method addElementsOfClass.

/**
	 * @param elements List of {@code ProperyData} instances
	 * @param propertyContainer Metadata about a class and its properties
	 *
	 * @return the number of id properties found while iterating the elements of {@code annotatedClass} using
	 *         the determined access strategy, {@code false} otherwise.
	 */
static int addElementsOfClass(List<PropertyData> elements, PropertyContainer propertyContainer, MetadataBuildingContext context) {
    int idPropertyCounter = 0;
    Collection<XProperty> properties = propertyContainer.getProperties();
    for (XProperty p : properties) {
        final int currentIdPropertyCounter = addProperty(propertyContainer, p, elements, context);
        idPropertyCounter += currentIdPropertyCounter;
    }
    return idPropertyCounter;
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) UniqueConstraint(javax.persistence.UniqueConstraint) Constraint(org.hibernate.mapping.Constraint)

Example 18 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class AnnotationBinder method addProperty.

private static int addProperty(PropertyContainer propertyContainer, XProperty property, List<PropertyData> inFlightPropertyDataList, MetadataBuildingContext context) {
    // and if so, skip it..
    for (PropertyData propertyData : inFlightPropertyDataList) {
        if (propertyData.getPropertyName().equals(property.getName())) {
            // EARLY EXIT!!!
            return 0;
        }
    }
    final XClass declaringClass = propertyContainer.getDeclaringClass();
    final XClass entity = propertyContainer.getEntityAtStake();
    int idPropertyCounter = 0;
    PropertyData propertyAnnotatedElement = new PropertyInferredData(declaringClass, property, propertyContainer.getClassLevelAccessType().getType(), context.getBuildingOptions().getReflectionManager());
    /*
		 * put element annotated by @Id in front
		 * since it has to be parsed beforeQuery any association by Hibernate
		 */
    final XAnnotatedElement element = propertyAnnotatedElement.getProperty();
    if (element.isAnnotationPresent(Id.class) || element.isAnnotationPresent(EmbeddedId.class)) {
        inFlightPropertyDataList.add(0, propertyAnnotatedElement);
        /**
			 * The property must be put in hibernate.properties as it's a system wide property. Fixable?
			 * TODO support true/false/default on the property instead of present / not present
			 * TODO is @Column mandatory?
			 * TODO add method support
			 */
        if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
            if (element.isAnnotationPresent(Id.class) && element.isAnnotationPresent(Column.class)) {
                String columnName = element.getAnnotation(Column.class).name();
                for (XProperty prop : declaringClass.getDeclaredProperties(AccessType.FIELD.getType())) {
                    if (!prop.isAnnotationPresent(MapsId.class)) {
                        /**
							 * The detection of a configured individual JoinColumn differs between Annotation
							 * and XML configuration processing.
							 */
                        boolean isRequiredAnnotationPresent = false;
                        JoinColumns groupAnnotation = prop.getAnnotation(JoinColumns.class);
                        if ((prop.isAnnotationPresent(JoinColumn.class) && prop.getAnnotation(JoinColumn.class).name().equals(columnName))) {
                            isRequiredAnnotationPresent = true;
                        } else if (prop.isAnnotationPresent(JoinColumns.class)) {
                            for (JoinColumn columnAnnotation : groupAnnotation.value()) {
                                if (columnName.equals(columnAnnotation.name())) {
                                    isRequiredAnnotationPresent = true;
                                    break;
                                }
                            }
                        }
                        if (isRequiredAnnotationPresent) {
                            //create a PropertyData fpr the specJ property holding the mapping
                            PropertyData specJPropertyData = new PropertyInferredData(declaringClass, //same dec
                            prop, // the actual @XToOne property
                            propertyContainer.getClassLevelAccessType().getType(), //TODO we should get the right accessor but the same as id would do
                            context.getBuildingOptions().getReflectionManager());
                            context.getMetadataCollector().addPropertyAnnotatedWithMapsIdSpecj(entity, specJPropertyData, element.toString());
                        }
                    }
                }
            }
        }
        if (element.isAnnotationPresent(ManyToOne.class) || element.isAnnotationPresent(OneToOne.class)) {
            context.getMetadataCollector().addToOneAndIdProperty(entity, propertyAnnotatedElement);
        }
        idPropertyCounter++;
    } else {
        inFlightPropertyDataList.add(propertyAnnotatedElement);
    }
    if (element.isAnnotationPresent(MapsId.class)) {
        context.getMetadataCollector().addPropertyAnnotatedWithMapsId(entity, propertyAnnotatedElement);
    }
    return idPropertyCounter;
}
Also used : MapsId(javax.persistence.MapsId) XProperty(org.hibernate.annotations.common.reflection.XProperty) EmbeddedId(javax.persistence.EmbeddedId) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) XAnnotatedElement(org.hibernate.annotations.common.reflection.XAnnotatedElement) XClass(org.hibernate.annotations.common.reflection.XClass) UniqueConstraint(javax.persistence.UniqueConstraint) Constraint(org.hibernate.mapping.Constraint) ManyToOne(javax.persistence.ManyToOne) OneToOne(javax.persistence.OneToOne) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) MapKeyColumn(javax.persistence.MapKeyColumn) OrderColumn(javax.persistence.OrderColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) JoinColumn(javax.persistence.JoinColumn) MapsId(javax.persistence.MapsId) NaturalId(org.hibernate.annotations.NaturalId) EmbeddedId(javax.persistence.EmbeddedId) Id(javax.persistence.Id) CollectionId(org.hibernate.annotations.CollectionId)

Example 19 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class AnnotationBinder method hasAnnotationsOnIdClass.

private static boolean hasAnnotationsOnIdClass(XClass idClass) {
    //		if(idClass.getAnnotation(Embeddable.class) != null)
    //			return true;
    List<XProperty> properties = idClass.getDeclaredProperties(XClass.ACCESS_FIELD);
    for (XProperty property : properties) {
        if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(Id.class) || property.isAnnotationPresent(GeneratedValue.class) || property.isAnnotationPresent(OneToOne.class) || property.isAnnotationPresent(ManyToMany.class)) {
            return true;
        }
    }
    List<XMethod> methods = idClass.getDeclaredMethods();
    for (XMethod method : methods) {
        if (method.isAnnotationPresent(Column.class) || method.isAnnotationPresent(OneToMany.class) || method.isAnnotationPresent(ManyToOne.class) || method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(GeneratedValue.class) || method.isAnnotationPresent(OneToOne.class) || method.isAnnotationPresent(ManyToMany.class)) {
            return true;
        }
    }
    return false;
}
Also used : GeneratedValue(javax.persistence.GeneratedValue) XProperty(org.hibernate.annotations.common.reflection.XProperty) XMethod(org.hibernate.annotations.common.reflection.XMethod) ManyToMany(javax.persistence.ManyToMany) ManyToOne(javax.persistence.ManyToOne)

Example 20 with XProperty

use of org.hibernate.annotations.common.reflection.XProperty in project hibernate-orm by hibernate.

the class SimpleValue method createParameterImpl.

private void createParameterImpl() {
    try {
        String[] columnsNames = new String[columns.size()];
        for (int i = 0; i < columns.size(); i++) {
            Selectable column = columns.get(i);
            if (column instanceof Column) {
                columnsNames[i] = ((Column) column).getName();
            }
        }
        final XProperty xProperty = (XProperty) typeParameters.get(DynamicParameterizedType.XPROPERTY);
        // todo : not sure this works for handling @MapKeyEnumerated
        final Annotation[] annotations = xProperty == null ? null : xProperty.getAnnotations();
        final ClassLoaderService classLoaderService = getMetadata().getMetadataBuildingOptions().getServiceRegistry().getService(ClassLoaderService.class);
        typeParameters.put(DynamicParameterizedType.PARAMETER_TYPE, new ParameterTypeImpl(classLoaderService.classForName(typeParameters.getProperty(DynamicParameterizedType.RETURNED_CLASS)), annotations, table.getCatalog(), table.getSchema(), table.getName(), Boolean.valueOf(typeParameters.getProperty(DynamicParameterizedType.IS_PRIMARY_KEY)), columnsNames));
    } catch (ClassLoadingException e) {
        throw new MappingException("Could not create DynamicParameterizedType for type: " + typeName, e);
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Annotation(java.lang.annotation.Annotation) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) MappingException(org.hibernate.MappingException)

Aggregations

XProperty (org.hibernate.annotations.common.reflection.XProperty)20 XClass (org.hibernate.annotations.common.reflection.XClass)9 AnnotationException (org.hibernate.AnnotationException)7 HashMap (java.util.HashMap)6 ManyToOne (javax.persistence.ManyToOne)6 EmbeddedId (javax.persistence.EmbeddedId)5 MappingException (org.hibernate.MappingException)5 Component (org.hibernate.mapping.Component)5 Column (javax.persistence.Column)4 Id (javax.persistence.Id)4 JoinColumn (javax.persistence.JoinColumn)4 Property (org.hibernate.mapping.Property)4 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)3 GeneratedValue (javax.persistence.GeneratedValue)3 JoinColumns (javax.persistence.JoinColumns)3 ManyToMany (javax.persistence.ManyToMany)3 MapsId (javax.persistence.MapsId)3 OneToOne (javax.persistence.OneToOne)3 UniqueConstraint (javax.persistence.UniqueConstraint)3 CollectionId (org.hibernate.annotations.CollectionId)3