Files
hibernate-demo/src/test/java/com/ankurm/hibernatedemo/mappingstyle/XmlMappingMetadataCompleteTest.java
T

47 lines
2.1 KiB
Java
Executable File

package com.ankurm.hibernatedemo.mappingstyle;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.ankurm.hibernatedemo.HibernateDemoApplication;
import org.hibernate.AnnotationException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
/**
* <xml-mapping-metadata-complete/> makes Hibernate IGNORE ALL annotations for this
* persistence unit -- not just the ones a matching orm.xml entry overrides. OverrideEntity
* carries @Id/@GeneratedValue on "id" in Java, but the metadata-complete orm.xml entry only
* declares <basic name="value"> and omits <id> entirely. Result: bootstrap fails because
* the (ignored) @Id annotation no longer counts -- proving metadata-complete is not just an
* override switch, it is a full annotation kill-switch for the persistence unit.
*
* <p>Docs: docs/04-annotations-vs-xml.md, Topic 1.
*/
class XmlMappingMetadataCompleteTest {
@Test
void metadataComplete_ignoresAtIdAnnotation_bootFailsWithNoIdentifier() {
SpringApplicationBuilder builder = new SpringApplicationBuilder(HibernateDemoApplication.class)
.properties("spring.jpa.mapping-resources=orm-xml-metadata-complete.xml");
BeanCreationException ex = assertThrows(BeanCreationException.class, () -> {
try (ConfigurableApplicationContext ctx = builder.run()) {
// never reached
}
});
Throwable root = ex;
while (root.getCause() != null) {
root = root.getCause();
}
System.out.println("RESULT[metadata-complete]: boot FAILED as predicted: " + root.getClass().getName()
+ ": " + root.getMessage());
assertThat(root).isInstanceOf(AnnotationException.class);
assertThat(root.getMessage()).contains("has no identifier");
}
}