Search in sources :

Example 1 with DiscriminatorType

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

the class AnnotationBinder method processSingleTableDiscriminatorProperties.

/**
 * Process all discriminator-related metadata per rules for "single table" inheritance
 */
private static Ejb3DiscriminatorColumn processSingleTableDiscriminatorProperties(XClass clazzToProcess, MetadataBuildingContext context, InheritanceState inheritanceState, EntityBinder entityBinder) {
    final boolean isRoot = !inheritanceState.hasParents();
    Ejb3DiscriminatorColumn discriminatorColumn = null;
    javax.persistence.DiscriminatorColumn discAnn = clazzToProcess.getAnnotation(javax.persistence.DiscriminatorColumn.class);
    DiscriminatorType discriminatorType = discAnn != null ? discAnn.discriminatorType() : DiscriminatorType.STRING;
    org.hibernate.annotations.DiscriminatorFormula discFormulaAnn = clazzToProcess.getAnnotation(org.hibernate.annotations.DiscriminatorFormula.class);
    if (isRoot) {
        discriminatorColumn = Ejb3DiscriminatorColumn.buildDiscriminatorColumn(discriminatorType, discAnn, discFormulaAnn, context);
    }
    if (discAnn != null && !isRoot) {
        LOG.invalidDiscriminatorAnnotation(clazzToProcess.getName());
    }
    final String discriminatorValue = clazzToProcess.isAnnotationPresent(DiscriminatorValue.class) ? clazzToProcess.getAnnotation(DiscriminatorValue.class).value() : null;
    entityBinder.setDiscriminatorValue(discriminatorValue);
    DiscriminatorOptions discriminatorOptions = clazzToProcess.getAnnotation(DiscriminatorOptions.class);
    if (discriminatorOptions != null) {
        entityBinder.setForceDiscriminator(discriminatorOptions.force());
        entityBinder.setInsertableDiscriminator(discriminatorOptions.insert());
    }
    return discriminatorColumn;
}
Also used : DiscriminatorColumn(javax.persistence.DiscriminatorColumn) DiscriminatorType(javax.persistence.DiscriminatorType) DiscriminatorFormula(org.hibernate.annotations.DiscriminatorFormula) DiscriminatorValue(javax.persistence.DiscriminatorValue) DiscriminatorOptions(org.hibernate.annotations.DiscriminatorOptions)

Example 2 with DiscriminatorType

use of javax.persistence.DiscriminatorType in project BroadleafCommerce by BroadleafCommerce.

the class SingleTableInheritanceClassTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    // Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
    if (className == null) {
        return null;
    }
    if (infos.isEmpty()) {
        return null;
    }
    String convertedClassName = className.replace('/', '.');
    SingleTableInheritanceInfo key = new SingleTableInheritanceInfo();
    key.setClassName(convertedClassName);
    int pos = infos.indexOf(key);
    if (pos >= 0) {
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Converting " + convertedClassName + " to a SingleTable inheritance strategy.");
            }
            SingleTableInheritanceInfo myInfo = infos.get(pos);
            ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
            ConstPool constantPool = classFile.getConstPool();
            AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag);
            List<?> attributes = classFile.getAttributes();
            Iterator<?> itr = attributes.iterator();
            while (itr.hasNext()) {
                Object object = itr.next();
                if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
                    AnnotationsAttribute attr = (AnnotationsAttribute) object;
                    Annotation[] items = attr.getAnnotations();
                    for (Annotation annotation : items) {
                        String typeName = annotation.getTypeName();
                        if (!typeName.equals(Inheritance.class.getName())) {
                            annotationsAttribute.addAnnotation(annotation);
                        }
                    }
                    itr.remove();
                }
            }
            Annotation inheritance = new Annotation(Inheritance.class.getName(), constantPool);
            ClassPool pool = ClassPool.getDefault();
            pool.importPackage("javax.persistence");
            pool.importPackage("java.lang");
            EnumMemberValue strategy = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("InheritanceType"));
            strategy.setType(InheritanceType.class.getName());
            strategy.setValue(InheritanceType.SINGLE_TABLE.name());
            inheritance.addMemberValue("strategy", strategy);
            annotationsAttribute.addAnnotation(inheritance);
            if (myInfo.getDiscriminatorName() != null) {
                Annotation discriminator = new Annotation(DiscriminatorColumn.class.getName(), constantPool);
                StringMemberValue name = new StringMemberValue(constantPool);
                name.setValue(myInfo.getDiscriminatorName());
                discriminator.addMemberValue("name", name);
                EnumMemberValue discriminatorType = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("DiscriminatorType"));
                discriminatorType.setType(DiscriminatorType.class.getName());
                discriminatorType.setValue(myInfo.getDiscriminatorType().name());
                discriminator.addMemberValue("discriminatorType", discriminatorType);
                IntegerMemberValue length = new IntegerMemberValue(constantPool);
                length.setValue(myInfo.getDiscriminatorLength());
                discriminator.addMemberValue("length", length);
                annotationsAttribute.addAnnotation(discriminator);
            }
            classFile.addAttribute(annotationsAttribute);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream os = new DataOutputStream(bos);
            classFile.write(os);
            os.close();
            return bos.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IllegalClassFormatException("Unable to convert " + convertedClassName + " to a SingleTable inheritance strategy: " + ex.getMessage());
        }
    } else {
        return null;
    }
}
Also used : EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) ConstPool(javassist.bytecode.ConstPool) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) DataOutputStream(java.io.DataOutputStream) Inheritance(javax.persistence.Inheritance) ClassPool(javassist.ClassPool) InheritanceType(javax.persistence.InheritanceType) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Annotation(javassist.bytecode.annotation.Annotation) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) DiscriminatorType(javax.persistence.DiscriminatorType) ByteArrayInputStream(java.io.ByteArrayInputStream) DiscriminatorColumn(javax.persistence.DiscriminatorColumn)

Example 3 with DiscriminatorType

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

the class JPAOverriddenAnnotationReader method getDiscriminatorColumn.

private DiscriminatorColumn getDiscriminatorColumn(Element tree, XMLContext.Default defaults) {
    Element element = tree != null ? tree.element("discriminator-column") : null;
    if (element != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(DiscriminatorColumn.class);
        copyStringAttribute(ad, element, "name", false);
        copyStringAttribute(ad, element, "column-definition", false);
        String value = element.attributeValue("discriminator-type");
        DiscriminatorType type = DiscriminatorType.STRING;
        if (value != null) {
            if ("STRING".equals(value)) {
                type = DiscriminatorType.STRING;
            } else if ("CHAR".equals(value)) {
                type = DiscriminatorType.CHAR;
            } else if ("INTEGER".equals(value)) {
                type = DiscriminatorType.INTEGER;
            } else {
                throw new AnnotationException("Unknown DiscrimiatorType in XML: " + value + " (" + SCHEMA_VALIDATION + ")");
            }
        }
        ad.setValue("discriminatorType", type);
        copyIntegerAttribute(ad, element, "length");
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(DiscriminatorColumn.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) DiscriminatorType(javax.persistence.DiscriminatorType) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) AnnotationException(org.hibernate.AnnotationException)

Example 4 with DiscriminatorType

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

the class AnnotationBinder method processJoinedDiscriminatorProperties.

/**
 * Process all discriminator-related metadata per rules for "joined" inheritance
 */
private static Ejb3DiscriminatorColumn processJoinedDiscriminatorProperties(XClass clazzToProcess, MetadataBuildingContext context, InheritanceState inheritanceState, EntityBinder entityBinder) {
    if (clazzToProcess.isAnnotationPresent(DiscriminatorFormula.class)) {
        throw new MappingException("@DiscriminatorFormula on joined inheritance not supported at this time");
    }
    // DiscriminatorValue handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final DiscriminatorValue discriminatorValueAnnotation = clazzToProcess.getAnnotation(DiscriminatorValue.class);
    final String discriminatorValue = discriminatorValueAnnotation != null ? clazzToProcess.getAnnotation(DiscriminatorValue.class).value() : null;
    entityBinder.setDiscriminatorValue(discriminatorValue);
    // DiscriminatorColumn handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final DiscriminatorColumn discriminatorColumnAnnotation = clazzToProcess.getAnnotation(DiscriminatorColumn.class);
    if (!inheritanceState.hasParents()) {
        // we want to process the discriminator column if either:
        // 1) There is an explicit DiscriminatorColumn annotation && we are not told to ignore them
        // 2) There is not an explicit DiscriminatorColumn annotation && we are told to create them implicitly
        final boolean generateDiscriminatorColumn;
        if (discriminatorColumnAnnotation != null) {
            if (context.getBuildingOptions().ignoreExplicitDiscriminatorsForJoinedInheritance()) {
                LOG.debugf("Ignoring explicit DiscriminatorColumn annotation on ", clazzToProcess.getName());
                generateDiscriminatorColumn = false;
            } else {
                LOG.applyingExplicitDiscriminatorColumnForJoined(clazzToProcess.getName(), AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS);
                generateDiscriminatorColumn = true;
            }
        } else {
            if (context.getBuildingOptions().createImplicitDiscriminatorsForJoinedInheritance()) {
                LOG.debug("Applying implicit DiscriminatorColumn using DiscriminatorColumn defaults");
                generateDiscriminatorColumn = true;
            } else {
                LOG.debug("Ignoring implicit (absent) DiscriminatorColumn");
                generateDiscriminatorColumn = false;
            }
        }
        if (generateDiscriminatorColumn) {
            final DiscriminatorType discriminatorType = discriminatorColumnAnnotation != null ? discriminatorColumnAnnotation.discriminatorType() : DiscriminatorType.STRING;
            return Ejb3DiscriminatorColumn.buildDiscriminatorColumn(discriminatorType, discriminatorColumnAnnotation, null, context);
        }
    } else {
        if (discriminatorColumnAnnotation != null) {
            LOG.invalidDiscriminatorAnnotation(clazzToProcess.getName());
        }
    }
    return null;
}
Also used : DiscriminatorType(javax.persistence.DiscriminatorType) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) DiscriminatorValue(javax.persistence.DiscriminatorValue) MappingException(org.hibernate.MappingException)

Aggregations

DiscriminatorColumn (javax.persistence.DiscriminatorColumn)4 DiscriminatorType (javax.persistence.DiscriminatorType)4 DiscriminatorValue (javax.persistence.DiscriminatorValue)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ClassPool (javassist.ClassPool)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 ClassFile (javassist.bytecode.ClassFile)1 ConstPool (javassist.bytecode.ConstPool)1 Annotation (javassist.bytecode.annotation.Annotation)1 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)1 IntegerMemberValue (javassist.bytecode.annotation.IntegerMemberValue)1 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)1 Inheritance (javax.persistence.Inheritance)1 InheritanceType (javax.persistence.InheritanceType)1 Element (org.dom4j.Element)1