Tagged: metamodel

how to exclude generated jpa metamodels from being javadoc-ed


<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <configuration>
      <sourceFileExcludes>
        <exclude>**/**_.java</exclude>
      </sourceFileExcludes>
    </configuration>
  </plugin>
</plugins>
Advertisements

finding an entity by its unique attribute value.


    public static <T extends BaseEntity, Y> Optional<T> findByUniqueAttribute(
            final EntityManager entityManager,
            final Class<T> entityClass,
            final Supplier<SingularAttribute<? super T, Y>> attributeSupplier,
            final Supplier<? extends Y> valueSupplier) {
        final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<T> criteria = builder.createQuery(entityClass);
        final Root<T> root = criteria.from(entityClass);
        criteria.select(root);
        criteria.where(builder.equal(root.get(
                attributeSupplier.get()), valueSupplier.get()));
        try {
            return Optional.of(
                    entityManager.createQuery(criteria).getSingleResult());
        } catch (final NoResultException nre) {
            return Optional.empty();
        }
    }
    public static <T extends BaseEntity, Y> Optional<T> findByUniqueAttribute2(
            @NotNull final EntityManager entityManager,
            @NotNull final Class<T> entityClass,
            @NotNull final Function<ManagedType<T>, SingularAttribute<? super T, Y>> attributeSupplier,
            @NotNull final Supplier<? extends Y> valueSupplier) {
        final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        final CriteriaQuery<T> criteria = builder.createQuery(entityClass);
        final Root<T> root = criteria.from(entityClass);
        criteria.select(root);
        criteria.where(builder.equal(
                root.get(attributeSupplier.apply(
                        entityManager.getMetamodel().entity(entityClass))),
                valueSupplier.get()));
        try {
            return Optional.of(
                    entityManager.createQuery(criteria).getSingleResult());
        } catch (final NoResultException nre) {
            return Optional.empty();
        }
    }

jpa-metamodels-with-maven


Sourcecode

$ git clone git@github.com:jinahya/jpa-metamodels-with-maven-example.git
$ cd jpa-metamodels-with-maven
$ git checkout develop
$ mvn help:all-profiles

Hibernate

With direct dependencies

<dependencies>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <scope>compile</scope>
    <optional>true</optional>
  </dependency>
</dependencies>

Using maven-processor-plugin

<build>
  <plugins>
    <plugin>
      <groupId>org.bsc.maven</groupId>
      <artifactId>maven-processor-plugin</artifactId>
      <executions>
        <execution>
          <id>process</id>
          <goals>
            <goal>process</goal>
          </goals>
          <phase>generate-sources</phase>
          <configuration>
            <processors>
               <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
            </processors>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-jpamodelgen</artifactId>
          <version>${hibernate.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

Apache OpenJPA

With direct dependencies

It seems that Apache OpenJPA must be triggered with a specific compiler argument.

<dependencies>
  <dependency>
    <groupId>org.apache.openjpa</groupId>
    <artifactId>openjpa</artifactId>
    <scope>compile</scope>
    <optional>true</optional>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <compilerArgs>
          <arg>-Aopenjpa.metamodel=true</arg>
        </compilerArgs>
      </configuration>
    </plugin>
  </plugins>
</build>

Using maven-processor-plugin

Again, Apache OpenJPA requires some additional configuration.

<build>
  <plugins>
    <plugin>
      <groupId>org.bsc.maven</groupId>
      <artifactId>maven-processor-plugin</artifactId>
      <executions>
        <execution>
          <id>process</id>
          <goals>
            <goal>process</goal>
          </goals>
          <phase>generate-sources</phase>
          <configuration>
            <processors>
              <processor>org.apache.openjpa.persistence.meta.AnnotationProcessor6</processor>
            </processors>
            <optionMap>
              <openjpa.metamodel>true</openjpa.metamodel>
            </optionMap>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.apache.openjpa</groupId>
          <artifactId>openjpa</artifactId>
          <version>${openjpa.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

EclipseLink

For EclipseLink, the location of persistence.xml must be specified.

Using direct dependencies

<dependencies>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
    <scope>compile</scope>
    <optional>true</optional>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <compilerArgs>
          <arg>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</arg>
        </compilerArgs>
      </configuration>
    </plugin>
  </plugins>
</build>

Using maven-processor-plugin

<build>
  <plugins>
    <plugin>
      <groupId>org.bsc.maven</groupId>
      <artifactId>maven-processor-plugin</artifactId>
      <executions>
        <execution>
          <id>process</id>
          <goals>
            <goal>process</goal>
          </goals>
          <phase>generate-sources</phase>
          <configuration>
            <processors>
              <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
            </processors>
            <compilerArguments>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</compilerArguments>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.eclipse.persistence</groupId>
          <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
          <version>${eclipselink.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

DataNucleus

Using direct dependencies

When working with DataNucleus, an additional dependency needs to be specified.

<dependencies>
  <dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-jpa-query</artifactId>
    <scope>compile</scope>
    <optional>true</optional>
  </dependency>
  <dependency>
    <groupId>org.datanucleus</groupId>
    <artifactId>datanucleus-core</artifactId>
    <version>${datanucleus.version}</version>
    <scope>compile</scope>
    <optional>true</optional>
  </dependency>
</dependencies>

Using maven-processor-plugin

<build>
  <plugins>
    <plugin>
      <groupId>org.bsc.maven</groupId>
      <artifactId>maven-processor-plugin</artifactId>
      <executions>
        <execution>
          <id>process</id>
          <goals>
            <goal>process</goal>
          </goals>
          <phase>generate-sources</phase>
          <configuration>
            <processors>
              <processor>org.datanucleus.jpa.query.JPACriteriaProcessor</processor>
            </processors>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.datanucleus</groupId>
          <artifactId>datanucleus-jpa-query</artifactId>
          <version>${datanucleus.version}</version>
        </dependency>
        <dependency>
          <groupId>org.datanucleus</groupId>
          <artifactId>datanucleus-core</artifactId>
          <version>${datanucleus.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>