In my library project I have some JPA @Entity
classes and a couple of JPA utils.
The project has essentially JAP/hibernate-related dependencies only, and no Spring-mumbo-jumbo.
Now I want to unit-test my domain classes as well as the utilities.
My test classes are like so:
class DSLCriteriaTest {
@BeforeAll
static void init() {
new DSLCriteria( Persistence.createEntityManagerFactory("tst").createEntityManager() );
}
@Test
void testDotNotationVSNested() {
var list = DSLCriteria.list( A.class, q -> q
.eq( "name", "a" )
.on( "b", () -> q
.on( "c.d", () -> q
.in( "lastName", "John" )
.orderBy( "id", "desc" )
)
)
);
assertFalse( list.isEmpty() );
}
}
and src/test/resources/META-INF/persistence.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd ">
<persistence-unit name="tst" transaction-type="RESOURCE_LOCAL">
<!--
<class>my.domain.A</class>
<class>my.domain.B</class>
... 30 other classes ...
-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
...
</properties>
</persistence-unit>
</persistence>
This configuration works only if I un-comment the <class>
-elements. The “auto-detection” fails miserably with an Exception:
java.lang.IllegalArgumentException: Not an entity: my.domain.A
at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.entity(JpaMetamodelImpl.java:205)
at org.hibernate.query.sqm.tree.select.AbstractSqmSelectQuery.from(AbstractSqmSelectQuery.java:243)
at org.hibernate.query.sqm.tree.select.AbstractSqmSelectQuery.from(AbstractSqmSelectQuery.java:44)
How to auto-scan all JPA entities of the same project in 2024 without using irrelevant tools?