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;
}
}
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;
}
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;
}
}
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);
}
}
}
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) {
}
}
}
Aggregations