Search in sources :

Example 1 with Access

use of javax.persistence.Access in project hibernate-orm by hibernate.

the class PropertyContainer method collectPersistentAttributesUsingLocalAccessType.

private void collectPersistentAttributesUsingLocalAccessType(TreeMap<String, XProperty> persistentAttributeMap, Map<String, XProperty> persistentAttributesFromGetters, List<XProperty> fields, List<XProperty> getters) {
    // Check fields...
    Iterator<XProperty> propertyIterator = fields.iterator();
    while (propertyIterator.hasNext()) {
        final XProperty xProperty = propertyIterator.next();
        final Access localAccessAnnotation = xProperty.getAnnotation(Access.class);
        if (localAccessAnnotation == null || localAccessAnnotation.value() != javax.persistence.AccessType.FIELD) {
            continue;
        }
        propertyIterator.remove();
        persistentAttributeMap.put(xProperty.getName(), xProperty);
    }
    // Check getters...
    propertyIterator = getters.iterator();
    while (propertyIterator.hasNext()) {
        final XProperty xProperty = propertyIterator.next();
        final Access localAccessAnnotation = xProperty.getAnnotation(Access.class);
        if (localAccessAnnotation == null || localAccessAnnotation.value() != javax.persistence.AccessType.PROPERTY) {
            continue;
        }
        propertyIterator.remove();
        final String name = xProperty.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(xProperty)), new Origin(SourceType.ANNOTATION, xClass.getName()));
        }
        persistentAttributeMap.put(name, xProperty);
        persistentAttributesFromGetters.put(name, xProperty);
    }
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) XProperty(org.hibernate.annotations.common.reflection.XProperty) Access(javax.persistence.Access) MappingException(org.hibernate.MappingException)

Example 2 with Access

use of javax.persistence.Access in project hibernate-orm by hibernate.

the class PropertyInferredData method getDefaultAccess.

public AccessType getDefaultAccess() throws MappingException {
    AccessType accessType = defaultAccess;
    AccessType hibernateAccessType = AccessType.DEFAULT;
    AccessType jpaAccessType = AccessType.DEFAULT;
    org.hibernate.annotations.AccessType accessTypeAnnotation = property.getAnnotation(org.hibernate.annotations.AccessType.class);
    if (accessTypeAnnotation != null) {
        hibernateAccessType = AccessType.getAccessStrategy(accessTypeAnnotation.value());
    }
    Access access = property.getAnnotation(Access.class);
    if (access != null) {
        jpaAccessType = AccessType.getAccessStrategy(access.value());
    }
    if (hibernateAccessType != AccessType.DEFAULT && jpaAccessType != AccessType.DEFAULT && hibernateAccessType != jpaAccessType) {
        StringBuilder builder = new StringBuilder();
        builder.append(property.toString());
        builder.append(" defines @AccessType and @Access with contradicting values. Use of @Access only is recommended.");
        throw new MappingException(builder.toString());
    }
    if (hibernateAccessType != AccessType.DEFAULT) {
        accessType = hibernateAccessType;
    } else if (jpaAccessType != AccessType.DEFAULT) {
        accessType = jpaAccessType;
    }
    return accessType;
}
Also used : Access(javax.persistence.Access) MappingException(org.hibernate.MappingException)

Example 3 with Access

use of javax.persistence.Access in project hibernate-orm by hibernate.

the class PropertyContainer method determineLocalClassDefinedAccessStrategy.

// 
// private void considerExplicitFieldAndPropertyAccess() {
// for ( XProperty property : fieldAccessMap.values() ) {
// Access access = property.getAnnotation( Access.class );
// if ( access == null ) {
// continue;
// }
// 
// // see "2.3.2 Explicit Access Type" of JPA 2 spec
// // the access type for this property is explicitly set to AccessType.FIELD, hence we have to
// // use field access for this property even if the default access type for the class is AccessType.PROPERTY
// AccessType accessType = AccessType.getAccessStrategy( access.value() );
// if (accessType == AccessType.FIELD) {
// propertyAccessMap.put(property.getName(), property);
// }
// else {
// LOG.debug( "Placing @Access(AccessType.FIELD) on a field does not have any effect." );
// }
// }
// 
// for ( XProperty property : propertyAccessMap.values() ) {
// Access access = property.getAnnotation( Access.class );
// if ( access == null ) {
// continue;
// }
// 
// AccessType accessType = AccessType.getAccessStrategy( access.value() );
// 
// // see "2.3.2 Explicit Access Type" of JPA 2 spec
// // the access type for this property is explicitly set to AccessType.PROPERTY, hence we have to
// // return use method access even if the default class access type is AccessType.FIELD
// if (accessType == AccessType.PROPERTY) {
// fieldAccessMap.put(property.getName(), property);
// }
// else {
// LOG.debug( "Placing @Access(AccessType.PROPERTY) on a field does not have any effect." );
// }
// }
// }
// /**
// * Retrieves all properties from the {@code xClass} with the specified access type. This method does not take
// * any jpa access rules/annotations into account yet.
// *
// * @param access The access type - {@code AccessType.FIELD}  or {@code AccessType.Property}
// *
// * @return A maps of the properties with the given access type keyed against their property name
// */
// private TreeMap<String, XProperty> initProperties(AccessType access) {
// if ( !( AccessType.PROPERTY.equals( access ) || AccessType.FIELD.equals( access ) ) ) {
// throw new IllegalArgumentException( "Access type has to be AccessType.FIELD or AccessType.Property" );
// }
// 
// //order so that property are used in the same order when binding native query
// TreeMap<String, XProperty> propertiesMap = new TreeMap<String, XProperty>();
// List<XProperty> properties = xClass.getDeclaredProperties( access.getType() );
// for ( XProperty property : properties ) {
// if ( mustBeSkipped( property ) ) {
// continue;
// }
// // HHH-10242 detect registration of the same property twice eg boolean isId() + UUID getId()
// XProperty oldProperty = propertiesMap.get( property.getName() );
// if ( oldProperty != null ) {
// throw new org.hibernate.boot.MappingException(
// LOG.ambiguousPropertyMethods(
// xClass.getName(),
// HCANNHelper.annotatedElementSignature( oldProperty ),
// HCANNHelper.annotatedElementSignature( property )
// ),
// new Origin( SourceType.ANNOTATION, xClass.getName() )
// );
// }
// 
// propertiesMap.put( property.getName(), property );
// }
// return propertiesMap;
// }
private AccessType determineLocalClassDefinedAccessStrategy() {
    AccessType classDefinedAccessType;
    AccessType hibernateDefinedAccessType = AccessType.DEFAULT;
    AccessType jpaDefinedAccessType = AccessType.DEFAULT;
    org.hibernate.annotations.AccessType accessType = xClass.getAnnotation(org.hibernate.annotations.AccessType.class);
    if (accessType != null) {
        hibernateDefinedAccessType = AccessType.getAccessStrategy(accessType.value());
    }
    Access access = xClass.getAnnotation(Access.class);
    if (access != null) {
        jpaDefinedAccessType = AccessType.getAccessStrategy(access.value());
    }
    if (hibernateDefinedAccessType != AccessType.DEFAULT && jpaDefinedAccessType != AccessType.DEFAULT && hibernateDefinedAccessType != jpaDefinedAccessType) {
        throw new MappingException("@AccessType and @Access specified with contradicting values. Use of @Access only is recommended. ");
    }
    if (hibernateDefinedAccessType != AccessType.DEFAULT) {
        classDefinedAccessType = hibernateDefinedAccessType;
    } else {
        classDefinedAccessType = jpaDefinedAccessType;
    }
    return classDefinedAccessType;
}
Also used : Access(javax.persistence.Access) MappingException(org.hibernate.MappingException)

Example 4 with Access

use of javax.persistence.Access in project hibernate-orm by hibernate.

the class EntityBinder method getExplicitAccessType.

public AccessType getExplicitAccessType(XAnnotatedElement element) {
    AccessType accessType = null;
    AccessType hibernateAccessType = null;
    AccessType jpaAccessType = null;
    org.hibernate.annotations.AccessType accessTypeAnnotation = element.getAnnotation(org.hibernate.annotations.AccessType.class);
    if (accessTypeAnnotation != null) {
        hibernateAccessType = AccessType.getAccessStrategy(accessTypeAnnotation.value());
    }
    Access access = element.getAnnotation(Access.class);
    if (access != null) {
        jpaAccessType = AccessType.getAccessStrategy(access.value());
    }
    if (hibernateAccessType != null && jpaAccessType != null && hibernateAccessType != jpaAccessType) {
        throw new MappingException("Found @Access and @AccessType with conflicting values on a property in class " + annotatedClass.toString());
    }
    if (hibernateAccessType != null) {
        accessType = hibernateAccessType;
    } else if (jpaAccessType != null) {
        accessType = jpaAccessType;
    }
    return accessType;
}
Also used : Access(javax.persistence.Access) AccessType(org.hibernate.cfg.AccessType) MappingException(org.hibernate.MappingException)

Example 5 with Access

use of javax.persistence.Access in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getAccessType.

private Access getAccessType(Element tree, XMLContext.Default defaults) {
    String access = tree == null ? null : tree.attributeValue("access");
    if (access != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(Access.class);
        AccessType type;
        try {
            type = AccessType.valueOf(access);
        } catch (IllegalArgumentException e) {
            throw new AnnotationException(access + " is not a valid access type. Check you xml confguration.");
        }
        ad.setValue("value", type);
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations() && isPhysicalAnnotationPresent(Access.class)) {
        return getPhysicalAnnotation(Access.class);
    } else if (defaults.getAccess() != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(Access.class);
        ad.setValue("value", defaults.getAccess());
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoaderAccess(org.hibernate.boot.spi.ClassLoaderAccess) Access(javax.persistence.Access) AnnotationException(org.hibernate.AnnotationException) AccessType(javax.persistence.AccessType)

Aggregations

Access (javax.persistence.Access)6 MappingException (org.hibernate.MappingException)4 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 AccessType (javax.persistence.AccessType)1 AnnotationException (org.hibernate.AnnotationException)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1 Origin (org.hibernate.boot.jaxb.Origin)1 ClassLoaderAccess (org.hibernate.boot.spi.ClassLoaderAccess)1 AccessType (org.hibernate.cfg.AccessType)1