Search in sources :

Example 1 with IdClass

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

the class JPAOverriddenAnnotationReader method getIdClass.

private IdClass getIdClass(Element tree, XMLContext.Default defaults) {
    Element element = tree == null ? null : tree.element("id-class");
    if (element != null) {
        Attribute attr = element.attribute("class");
        if (attr != null) {
            AnnotationDescriptor ad = new AnnotationDescriptor(IdClass.class);
            Class clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(attr.getValue(), defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find id-class: " + attr.getValue(), e);
            }
            ad.setValue("value", clazz);
            return AnnotationFactory.create(ad);
        } else {
            throw new AnnotationException("id-class without class. " + SCHEMA_VALIDATION);
        }
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(IdClass.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) IdClass(javax.persistence.IdClass) Attribute(org.dom4j.Attribute) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) AnnotationException(org.hibernate.AnnotationException) MapKeyClass(javax.persistence.MapKeyClass) IdClass(javax.persistence.IdClass)

Example 2 with IdClass

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

the class AnnotationBinder method mapAsIdClass.

private static boolean mapAsIdClass(Map<XClass, InheritanceState> inheritanceStatePerClass, InheritanceState inheritanceState, PersistentClass persistentClass, EntityBinder entityBinder, PropertyHolder propertyHolder, InheritanceState.ElementsToProcess elementsToProcess, Set<String> idPropertiesIfIdClass, MetadataBuildingContext context) {
    /*
		 * We are looking for @IdClass
		 * In general we map the id class as identifier using the mapping metadata of the main entity's properties
		 * and we create an identifier mapper containing the id properties of the main entity
		 *
		 * In JPA 2, there is a shortcut if the id class is the Pk of the associated class pointed to by the id
		 * it ought to be treated as an embedded and not a real IdClass (at least in the Hibernate's internal way
		 */
    XClass classWithIdClass = inheritanceState.getClassWithIdClass(false);
    if (classWithIdClass != null) {
        IdClass idClass = classWithIdClass.getAnnotation(IdClass.class);
        XClass compositeClass = context.getBootstrapContext().getReflectionManager().toXClass(idClass.value());
        PropertyData inferredData = new PropertyPreloadedData(entityBinder.getPropertyAccessType(), "id", compositeClass);
        PropertyData baseInferredData = new PropertyPreloadedData(entityBinder.getPropertyAccessType(), "id", classWithIdClass);
        AccessType propertyAccessor = entityBinder.getPropertyAccessor(compositeClass);
        // In JPA 2, there is a shortcut if the IdClass is the Pk of the associated class pointed to by the id
        // it ought to be treated as an embedded and not a real IdClass (at least in the Hibernate's internal way
        final boolean isFakeIdClass = isIdClassPkOfTheAssociatedEntity(elementsToProcess, compositeClass, inferredData, baseInferredData, propertyAccessor, inheritanceStatePerClass, context);
        if (isFakeIdClass) {
            return false;
        }
        boolean isComponent = true;
        String generatorType = "assigned";
        String generator = BinderHelper.ANNOTATION_STRING_DEFAULT;
        boolean ignoreIdAnnotations = entityBinder.isIgnoreIdAnnotations();
        entityBinder.setIgnoreIdAnnotations(true);
        propertyHolder.setInIdClass(true);
        bindIdClass(generatorType, generator, inferredData, baseInferredData, null, propertyHolder, isComponent, propertyAccessor, entityBinder, true, false, context, inheritanceStatePerClass);
        propertyHolder.setInIdClass(null);
        inferredData = new PropertyPreloadedData(propertyAccessor, PropertyPath.IDENTIFIER_MAPPER_PROPERTY, compositeClass);
        Component mapper = fillComponent(propertyHolder, inferredData, baseInferredData, propertyAccessor, false, entityBinder, true, true, false, context, inheritanceStatePerClass);
        entityBinder.setIgnoreIdAnnotations(ignoreIdAnnotations);
        persistentClass.setIdentifierMapper(mapper);
        // If id definition is on a mapped superclass, update the mapping
        final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(classWithIdClass, inheritanceStatePerClass, context);
        if (superclass != null) {
            superclass.setDeclaredIdentifierMapper(mapper);
        } else {
            // we are for sure on the entity
            persistentClass.setDeclaredIdentifierMapper(mapper);
        }
        Property property = new Property();
        property.setName(PropertyPath.IDENTIFIER_MAPPER_PROPERTY);
        property.setUpdateable(false);
        property.setInsertable(false);
        property.setValue(mapper);
        property.setPropertyAccessorName("embedded");
        persistentClass.addProperty(property);
        entityBinder.setIgnoreIdAnnotations(true);
        Iterator properties = mapper.getPropertyIterator();
        while (properties.hasNext()) {
            idPropertiesIfIdClass.add(((Property) properties.next()).getName());
        }
        return true;
    } else {
        return false;
    }
}
Also used : XClass(org.hibernate.annotations.common.reflection.XClass) IdClass(javax.persistence.IdClass) Iterator(java.util.Iterator) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty)

Aggregations

IdClass (javax.persistence.IdClass)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Iterator (java.util.Iterator)1 MapKeyClass (javax.persistence.MapKeyClass)1 Attribute (org.dom4j.Attribute)1 Element (org.dom4j.Element)1 AnnotationException (org.hibernate.AnnotationException)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 Component (org.hibernate.mapping.Component)1 Property (org.hibernate.mapping.Property)1