Search in sources :

Example 1 with JaxbSecondaryTable

use of org.hibernate.boot.jaxb.mapping.spi.JaxbSecondaryTable 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)

Aggregations

SecondaryTable (jakarta.persistence.SecondaryTable)1 SecondaryTables (jakarta.persistence.SecondaryTables)1 ArrayList (java.util.ArrayList)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 JaxbEntity (org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)1 JaxbSecondaryTable (org.hibernate.boot.jaxb.mapping.spi.JaxbSecondaryTable)1