Search in sources :

Example 11 with JaxbEntity

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntity in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method getDiscriminatorColumn.

private DiscriminatorColumn getDiscriminatorColumn(ManagedType root, XMLContext.Default defaults) {
    JaxbDiscriminatorColumn element = root instanceof JaxbEntity ? ((JaxbEntity) root).getDiscriminatorColumn() : null;
    if (element != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(DiscriminatorColumn.class);
        copyAttribute(ad, "name", element.getName(), false);
        copyAttribute(ad, "column-definition", element.getColumnDefinition(), false);
        DiscriminatorType type = element.getDiscriminatorType();
        if (type != null) {
            ad.setValue("discriminatorType", type);
        }
        copyAttribute(ad, "length", element.getLength(), false);
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(DiscriminatorColumn.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbDiscriminatorColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbDiscriminatorColumn) DiscriminatorType(jakarta.persistence.DiscriminatorType) JaxbDiscriminatorColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbDiscriminatorColumn) DiscriminatorColumn(jakarta.persistence.DiscriminatorColumn) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Example 12 with JaxbEntity

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntity in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method getConverts.

private Converts getConverts(ManagedType root, XMLContext.Default defaults) {
    // NOTE : we use a map here to make sure that an xml and annotation referring to the same attribute
    // properly overrides.  Bit sparse, but easy...
    final Map<String, Convert> convertAnnotationsMap = new HashMap<>();
    if (root instanceof JaxbEntity) {
        applyXmlDefinedConverts(((JaxbEntity) root).getConvert(), defaults, null, convertAnnotationsMap);
    }
    if (defaults.canUseJavaAnnotations()) {
        applyPhysicalConvertAnnotations(null, convertAnnotationsMap);
    }
    if (!convertAnnotationsMap.isEmpty()) {
        final AnnotationDescriptor groupingDescriptor = new AnnotationDescriptor(Converts.class);
        groupingDescriptor.setValue("value", convertAnnotationsMap.values().toArray(new Convert[convertAnnotationsMap.size()]));
        return AnnotationFactory.create(groupingDescriptor);
    }
    return null;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) Convert(jakarta.persistence.Convert) JaxbConvert(org.hibernate.boot.jaxb.mapping.spi.JaxbConvert) HashMap(java.util.HashMap) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Example 13 with JaxbEntity

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntity in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method getSecondaryTables.

private SecondaryTables getSecondaryTables(ManagedType root, XMLContext.Default defaults) {
    List<JaxbSecondaryTable> elements = root instanceof JaxbEntity ? ((JaxbEntity) root).getSecondaryTable() : Collections.emptyList();
    List<SecondaryTable> secondaryTables = new ArrayList<>(3);
    for (JaxbSecondaryTable element : elements) {
        AnnotationDescriptor annotation = new AnnotationDescriptor(SecondaryTable.class);
        copyAttribute(annotation, "name", element.getName(), false);
        copyAttribute(annotation, "catalog", element.getCatalog(), false);
        if (StringHelper.isNotEmpty(defaults.getCatalog()) && StringHelper.isEmpty((String) annotation.valueOf("catalog"))) {
            annotation.setValue("catalog", defaults.getCatalog());
        }
        copyAttribute(annotation, "schema", element.getSchema(), false);
        if (StringHelper.isNotEmpty(defaults.getSchema()) && StringHelper.isEmpty((String) annotation.valueOf("schema"))) {
            annotation.setValue("schema", defaults.getSchema());
        }
        buildUniqueConstraints(annotation, element.getUniqueConstraint());
        buildIndex(annotation, element.getIndex());
        annotation.setValue("pkJoinColumns", buildPrimaryKeyJoinColumns(element.getPrimaryKeyJoinColumn()));
        secondaryTables.add(AnnotationFactory.create(annotation));
    }
    /*
		 * You can't have both secondary tables in XML and Java,
		 * since there would be no way to "remove" a secondary table
		 */
    if (secondaryTables.size() == 0 && defaults.canUseJavaAnnotations()) {
        SecondaryTable secTableAnn = getPhysicalAnnotation(SecondaryTable.class);
        overridesDefaultInSecondaryTable(secTableAnn, defaults, secondaryTables);
        SecondaryTables secTablesAnn = getPhysicalAnnotation(SecondaryTables.class);
        if (secTablesAnn != null) {
            for (SecondaryTable table : secTablesAnn.value()) {
                overridesDefaultInSecondaryTable(table, defaults, secondaryTables);
            }
        }
    }
    if (secondaryTables.size() > 0) {
        AnnotationDescriptor descriptor = new AnnotationDescriptor(SecondaryTables.class);
        descriptor.setValue("value", secondaryTables.toArray(new SecondaryTable[secondaryTables.size()]));
        return AnnotationFactory.create(descriptor);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) SecondaryTables(jakarta.persistence.SecondaryTables) ArrayList(java.util.ArrayList) JaxbSecondaryTable(org.hibernate.boot.jaxb.mapping.spi.JaxbSecondaryTable) SecondaryTable(jakarta.persistence.SecondaryTable) JaxbSecondaryTable(org.hibernate.boot.jaxb.mapping.spi.JaxbSecondaryTable) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Example 14 with JaxbEntity

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntity in project hibernate-orm by hibernate.

the class XMLContext method addClass.

private void addClass(List<? extends ManagedType> managedTypes, String packageName, Default defaults, List<String> addedClasses) {
    for (ManagedType element : managedTypes) {
        String className = buildSafeClassName(element.getClazz(), packageName);
        if (managedTypeOverride.containsKey(className)) {
            // maybe switch it to warn?
            throw new IllegalStateException("Duplicate XML entry for " + className);
        }
        addedClasses.add(className);
        managedTypeOverride.put(className, element);
        Default mergedDefaults = new Default();
        // Apply entity mapping defaults
        mergedDefaults.overrideWithCatalogAndSchema(defaults);
        // ... then apply entity settings
        Default fileDefaults = new Default();
        fileDefaults.setMetadataComplete(element.isMetadataComplete());
        fileDefaults.setAccess(element.getAccess());
        mergedDefaults.overrideWithCatalogAndSchema(fileDefaults);
        // ... and we get the merged defaults for that entity
        defaultsOverride.put(className, mergedDefaults);
        LOG.debugf("Adding XML overriding information for %s", className);
        if (element instanceof JaxbEntity) {
            addEntityListenerClasses(((JaxbEntity) element).getEntityListeners(), packageName, addedClasses);
        } else if (element instanceof JaxbMappedSuperclass) {
            addEntityListenerClasses(((JaxbMappedSuperclass) element).getEntityListeners(), packageName, addedClasses);
        }
    }
}
Also used : ManagedType(org.hibernate.boot.jaxb.mapping.spi.ManagedType) JaxbMappedSuperclass(org.hibernate.boot.jaxb.mapping.spi.JaxbMappedSuperclass) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Example 15 with JaxbEntity

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntity in project hibernate-orm by hibernate.

the class JakartaXmlSmokeTests method testLoadingOrmXml.

@Test
public void testLoadingOrmXml(ServiceRegistryScope scope) {
    final ClassLoaderService cls = scope.getRegistry().getService(ClassLoaderService.class);
    final MappingBinder mappingBinder = new MappingBinder(cls, true);
    final InputStream inputStream = cls.locateResourceStream("xml/jakarta/simple/orm.xml");
    try {
        final Binding<JaxbEntityMappings> binding = mappingBinder.bind(new StreamSource(inputStream), new Origin(SourceType.RESOURCE, "xml/jakarta/simple/orm.xml"));
        assertThat(binding.getRoot().getEntity().stream().map(JaxbEntity::getClazz)).containsOnly("Lighter", "ApplicationServer");
        final JaxbPersistenceUnitMetadata puMetadata = binding.getRoot().getPersistenceUnitMetadata();
        final JaxbPersistenceUnitDefaults puDefaults = puMetadata.getPersistenceUnitDefaults();
        final Stream<String> listenerNames = puDefaults.getEntityListeners().getEntityListener().stream().map(JaxbEntityListener::getClazz);
        assertThat(listenerNames).containsOnly("org.hibernate.jpa.test.pack.defaultpar.IncrementListener");
    } finally {
        try {
            inputStream.close();
        } catch (IOException ignore) {
        }
    }
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) InputStream(java.io.InputStream) JaxbEntityListener(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) MappingBinder(org.hibernate.boot.jaxb.internal.MappingBinder) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings) JaxbPersistenceUnitDefaults(org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitDefaults) JaxbPersistenceUnitMetadata(org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitMetadata) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity) Test(org.junit.jupiter.api.Test)

Aggregations

JaxbEntity (org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)16 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)13 SecondaryTable (jakarta.persistence.SecondaryTable)2 JaxbMappedSuperclass (org.hibernate.boot.jaxb.mapping.spi.JaxbMappedSuperclass)2 JaxbPersistenceUnitDefaults (org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitDefaults)2 JaxbPersistenceUnitMetadata (org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitMetadata)2 JaxbSecondaryTable (org.hibernate.boot.jaxb.mapping.spi.JaxbSecondaryTable)2 CollectionTable (jakarta.persistence.CollectionTable)1 Convert (jakarta.persistence.Convert)1 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)1 DiscriminatorType (jakarta.persistence.DiscriminatorType)1 DiscriminatorValue (jakarta.persistence.DiscriminatorValue)1 Entity (jakarta.persistence.Entity)1 Inheritance (jakarta.persistence.Inheritance)1 InheritanceType (jakarta.persistence.InheritanceType)1 JoinTable (jakarta.persistence.JoinTable)1 NamedEntityGraph (jakarta.persistence.NamedEntityGraph)1 NamedEntityGraphs (jakarta.persistence.NamedEntityGraphs)1 NamedNativeQueries (jakarta.persistence.NamedNativeQueries)1 NamedNativeQuery (jakarta.persistence.NamedNativeQuery)1