JPA entity classes are not discovered automatically with Gradle

In Java SE environment with Hibernate, class elements is needed in persistence-unit element.

In JPA2.1 Specification(JSR338 8.2.1.6.4), it is described that

A list of all named managed persistence classes must be specified in Java SE environments to insure
portability. Portable Java SE applications should not rely on the other mechanisms described here to
specify the managed persistence classes of a persistence unit.

If you do not want to list classes, there are several ways to generate persistence.xml.

  • generate persistence.xml with Annotation Processor.
  • generate persistence.xml with Gradle custom task(, but it’s not available on executing test in IDE).
  • let IDE to list classes