Lab 06

JPA relations

JPA Hibernate Model Gen

In project  lab 1 , lab 2, lab 3

Use maven to add the hibernate-jpamodelgen library to automatically generate descriptive classes for JPA entities.

Dependency to pom.xml

        <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
<version>6.6.11.Final</version>
</dependency>

Must add to maven-compiler-plugin as annotation processor right after lombok.

                        <path>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>6.6.11.Final</version>
</path>

Furthermore, also in the maven-compiler-plugin right after the end </configuration> add tags to execute plugin in phase generate-sources

                <executions>
<execution>
<id>process</id>
<phase>generate-sources</phase>
</execution>
</executions>

We need to add the generated sources in the target/generated-sources/annotations folder to the project files using the build-helper-maven-plugin:

            <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/annotations</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

It is necessary to add a module requrement to module-info.java for annotations in generated classes:

requires static jakarta.annotation;
    

Entities and relations

Make the Score, Player, Game, PlatformGame, FirstPersonShooter classes valid entities.

  • Don't forget the annotations for the constructor without parameters
  • Annotations for the OneToMany and ManyToOne bindings and the mappedBy parameter
  • Ensure that collections are not output as part of the toString method
  • Ensure that only the id is used for hashcode and equals.

JpaConnector

Implement the functionality of methods from the JpaConnector class

  • For the findBy(String partialName, Score.Difficult difficult) method, use CriteriaQuery and make sure that the partialName and difficult values are only used if they are not null and if at least one or both are set, use them as a search condition. In the case of partialName with the LIKE operator and in the case of difficult as ==