Search in sources :

Example 11 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)

Example 12 with XProperty

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

the class PropertyContainer method collectPersistentAttributesUsingClassLevelAccessType.

private void collectPersistentAttributesUsingClassLevelAccessType(TreeMap<String, XProperty> persistentAttributeMap, Map<String, XProperty> persistentAttributesFromGetters, List<XProperty> fields, List<XProperty> getters) {
    if (classLevelAccessType == AccessType.FIELD) {
        for (XProperty field : fields) {
            if (persistentAttributeMap.containsKey(field.getName())) {
                continue;
            }
            persistentAttributeMap.put(field.getName(), field);
        }
    } else {
        for (XProperty getter : getters) {
            final String name = getter.getName();
            // HHH-10242 detect registration of the same property getter twice - eg boolean isId() + UUID getId()
            final XProperty previous = persistentAttributesFromGetters.get(name);
            if (previous != null) {
                throw new org.hibernate.boot.MappingException(LOG.ambiguousPropertyMethods(xClass.getName(), HCANNHelper.annotatedElementSignature(previous), HCANNHelper.annotatedElementSignature(getter)), new Origin(SourceType.ANNOTATION, xClass.getName()));
            }
            if (persistentAttributeMap.containsKey(name)) {
                continue;
            }
            persistentAttributeMap.put(getter.getName(), getter);
            persistentAttributesFromGetters.put(name, getter);
        }
    }
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) XProperty(org.hibernate.annotations.common.reflection.XProperty) MappingException(org.hibernate.MappingException)

Example 13 with XProperty

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

the class PropertyContainer method preFilter.

private void preFilter(List<XProperty> fields, List<XProperty> getters) {
    Iterator<XProperty> propertyIterator = fields.iterator();
    while (propertyIterator.hasNext()) {
        final XProperty property = propertyIterator.next();
        if (mustBeSkipped(property)) {
            propertyIterator.remove();
        }
    }
    propertyIterator = getters.iterator();
    while (propertyIterator.hasNext()) {
        final XProperty property = propertyIterator.next();
        if (mustBeSkipped(property)) {
            propertyIterator.remove();
        }
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty)

Example 14 with XProperty

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

the class AnnotationBinder method bindComponent.

private static PropertyBinder bindComponent(PropertyData inferredData, PropertyHolder propertyHolder, AccessType propertyAccessor, EntityBinder entityBinder, boolean isIdentifierMapper, MetadataBuildingContext buildingContext, boolean isComponentEmbedded, //is a identifier
boolean isId, Map<XClass, InheritanceState> inheritanceStatePerClass, //is a component who is overridden by a @MapsId
String referencedEntityName, Ejb3JoinColumn[] columns) {
    Component comp;
    if (referencedEntityName != null) {
        comp = createComponent(propertyHolder, inferredData, isComponentEmbedded, isIdentifierMapper, buildingContext);
        SecondPass sp = new CopyIdentifierComponentSecondPass(comp, referencedEntityName, columns, buildingContext);
        buildingContext.getMetadataCollector().addSecondPass(sp);
    } else {
        comp = fillComponent(propertyHolder, inferredData, propertyAccessor, !isId, entityBinder, isComponentEmbedded, isIdentifierMapper, false, buildingContext, inheritanceStatePerClass);
    }
    if (isId) {
        comp.setKey(true);
        if (propertyHolder.getPersistentClass().getIdentifier() != null) {
            throw new AnnotationException(comp.getComponentClassName() + " must not have @Id properties when used as an @EmbeddedId: " + BinderHelper.getPath(propertyHolder, inferredData));
        }
        if (referencedEntityName == null && comp.getPropertySpan() == 0) {
            throw new AnnotationException(comp.getComponentClassName() + " has no persistent id property: " + BinderHelper.getPath(propertyHolder, inferredData));
        }
    }
    XProperty property = inferredData.getProperty();
    setupComponentTuplizer(property, comp);
    PropertyBinder binder = new PropertyBinder();
    binder.setDeclaringClass(inferredData.getDeclaringClass());
    binder.setName(inferredData.getPropertyName());
    binder.setValue(comp);
    binder.setProperty(inferredData.getProperty());
    binder.setAccessType(inferredData.getDefaultAccess());
    binder.setEmbedded(isComponentEmbedded);
    binder.setHolder(propertyHolder);
    binder.setId(isId);
    binder.setEntityBinder(entityBinder);
    binder.setInheritanceStatePerClass(inheritanceStatePerClass);
    binder.setBuildingContext(buildingContext);
    binder.makePropertyAndBind();
    return binder;
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) AnnotationException(org.hibernate.AnnotationException) PropertyBinder(org.hibernate.cfg.annotations.PropertyBinder) Component(org.hibernate.mapping.Component)

Example 15 with XProperty

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

the class AnnotationBinder method bindManyToOne.

private static void bindManyToOne(String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional, boolean ignoreNotFound, boolean cascadeOnDelete, XClass targetEntity, PropertyHolder propertyHolder, PropertyData inferredData, boolean unique, boolean isIdentifierMapper, boolean inSecondPass, PropertyBinder propertyBinder, MetadataBuildingContext context) {
    //All FK columns should be in the same table
    org.hibernate.mapping.ManyToOne value = new org.hibernate.mapping.ManyToOne(context.getMetadataCollector(), columns[0].getTable());
    // This is a @OneToOne mapped to a physical o.h.mapping.ManyToOne
    if (unique) {
        value.markAsLogicalOneToOne();
    }
    value.setReferencedEntityName(ToOneBinder.getReferenceEntityName(inferredData, targetEntity, context));
    final XProperty property = inferredData.getProperty();
    defineFetchingStrategy(value, property);
    //value.setFetchMode( fetchMode );
    value.setIgnoreNotFound(ignoreNotFound);
    value.setCascadeDeleteEnabled(cascadeOnDelete);
    //value.setLazy( fetchMode != FetchMode.JOIN );
    if (!optional) {
        for (Ejb3JoinColumn column : columns) {
            column.setNullable(false);
        }
    }
    if (property.isAnnotationPresent(MapsId.class)) {
        //read only
        for (Ejb3JoinColumn column : columns) {
            column.setInsertable(false);
            column.setUpdatable(false);
        }
    }
    final JoinColumn joinColumn = property.getAnnotation(JoinColumn.class);
    final JoinColumns joinColumns = property.getAnnotation(JoinColumns.class);
    //Make sure that JPA1 key-many-to-one columns are read only tooj
    boolean hasSpecjManyToOne = false;
    if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
        String columnName = "";
        for (XProperty prop : inferredData.getDeclaringClass().getDeclaredProperties(AccessType.FIELD.getType())) {
            if (prop.isAnnotationPresent(Id.class) && prop.isAnnotationPresent(Column.class)) {
                columnName = prop.getAnnotation(Column.class).name();
            }
            if (property.isAnnotationPresent(ManyToOne.class) && joinColumn != null && !BinderHelper.isEmptyAnnotationValue(joinColumn.name()) && joinColumn.name().equals(columnName) && !property.isAnnotationPresent(MapsId.class)) {
                hasSpecjManyToOne = true;
                for (Ejb3JoinColumn column : columns) {
                    column.setInsertable(false);
                    column.setUpdatable(false);
                }
            }
        }
    }
    value.setTypeName(inferredData.getClassOrElementName());
    final String propertyName = inferredData.getPropertyName();
    value.setTypeUsingReflection(propertyHolder.getClassName(), propertyName);
    if ((joinColumn != null && joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) || (joinColumns != null && joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT)) {
        // not ideal...
        value.setForeignKeyName("none");
    } else {
        final ForeignKey fk = property.getAnnotation(ForeignKey.class);
        if (fk != null && StringHelper.isNotEmpty(fk.name())) {
            value.setForeignKeyName(fk.name());
        } else {
            final javax.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey(StringHelper.qualify(propertyHolder.getPath(), propertyName));
            if (fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT) {
                value.setForeignKeyName("none");
            } else if (fkOverride != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(fkOverride.name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(fkOverride.foreignKeyDefinition()));
            } else if (joinColumns != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumns.foreignKey().name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumns.foreignKey().foreignKeyDefinition()));
            } else if (joinColumn != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumn.foreignKey().name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumn.foreignKey().foreignKeyDefinition()));
            }
        }
    }
    String path = propertyHolder.getPath() + "." + propertyName;
    FkSecondPass secondPass = new ToOneFkSecondPass(value, columns, //cannot have nullable and unique on certain DBs like Derby
    !optional && unique, propertyHolder.getEntityOwnerClassName(), path, context);
    if (inSecondPass) {
        secondPass.doSecondPass(context.getMetadataCollector().getEntityBindingMap());
    } else {
        context.getMetadataCollector().addSecondPass(secondPass);
    }
    Ejb3Column.checkPropertyConsistency(columns, propertyHolder.getEntityName() + "." + propertyName);
    //PropertyBinder binder = new PropertyBinder();
    propertyBinder.setName(propertyName);
    propertyBinder.setValue(value);
    //binder.setCascade(cascadeStrategy);
    if (isIdentifierMapper) {
        propertyBinder.setInsertable(false);
        propertyBinder.setUpdatable(false);
    } else if (hasSpecjManyToOne) {
        propertyBinder.setInsertable(false);
        propertyBinder.setUpdatable(false);
    } else {
        propertyBinder.setInsertable(columns[0].isInsertable());
        propertyBinder.setUpdatable(columns[0].isUpdatable());
    }
    propertyBinder.setColumns(columns);
    propertyBinder.setAccessType(inferredData.getDefaultAccess());
    propertyBinder.setCascade(cascadeStrategy);
    propertyBinder.setProperty(property);
    propertyBinder.setXToMany(true);
    propertyBinder.makePropertyAndBind();
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) ForeignKey(org.hibernate.annotations.ForeignKey) ManyToOne(javax.persistence.ManyToOne) 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)

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