Home > front end >  Metamodel and the Persistence.xml file
Metamodel and the Persistence.xml file

Time:12-17

I'm new to Spring Framework and trying to create dynamic search queries for an sql database- similar to what is described in the below thread.

Filtering database rows with spring-data-jpa and spring-mvc

This thread provided a useful guide however I'm having issues generating the metamodel files described. I know this is a common issue and I have tried to implement the solutions already available online however they have not worked.

I am having particular issue understanding the concept of the Persistence.xml file and where to find it. From what I've read it's function seems very similar to my 'application.properties file. The Persistence.xml file is apparently necessary to generate the metamodel. I've found the following answers regarding it's location but honestly do not really understand either.

"META-INF folder that needs to reside in the root of the Java classpath" "persistence. xml should be located in the WAR file's WEB-INF/classes/META-INF directory"

I am working with a maven project in Eclipse and the only META-INF directories I can find are within the jar files in 'Maven Dependencies' Should the "persistence. xml" file be auto-generated or do I need to create it myself? If so, where? I have added a plugin that was meant to generate it but it hasn't worked.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>

I am also worried that I may not have applied the correct changes to project 'Properties'. I have enabled 'annotation processing' and set the 'generated test source dictionary' to the default of '.apt_generated'. Is this okay or should I name the generated test source dictionary something else?

I have added the recommended dependency to my pom file-

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.6.2.Final</version>
        </dependency>

As well as the following plugin:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/metamodel</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

When I try to rebuild/run the project, it runs as before with no errors displaying in the console and no new metamodel classes being generated (_files are still not recognized)

Please see my pom.xml file below

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bron</groupId>
    <artifactId>demoJPA</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demoJPA</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!-- Spring JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.5.6</version>

        </dependency>
        <dependency>
            <!-- Spring starter web -->

            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>





        <!-- E-mail sending capabilities -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>


        <!-- Connect to mySQL database -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>



        <!-- Connect to mySQL database -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.5.6</version>
            <scope>test</scope>
        </dependency>

        <!--Reduce boiler plate code -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>


        <!--Spring security -->
        <dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
            <version>2.5.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security.experimental</groupId>
            <artifactId>spring-security-oauth2-authorization-server</artifactId>
            <version>0.1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.5.6</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.6.2.Final</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>


            <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>
                            <!-- source output directory -->
                            <outputDirectory>target/metamodel</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/metamodel</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
        </plugins>



    </build>





</project>

Because I'm new to Spring and complex Frameworks in general, I am finding the solutions I have read very difficult to understand and apply to my project. Any help would be very much appreciated!

CodePudding user response:

  1. The persistence.xml should be in src/main/resources/META-INF/ and if any of the folder of that path do not exist, you have to create them yourself, also you have to create / copy the persistence.xml file into that directory with the configuration you want.

  2. The metamodel generation should work, but I am unfamiliar with the plugin you are using to add the sources, I can give you an except of a pom where I know it is working:

  3. The metamodel classes should be generated upon using mvn install (mvn clean install), make sure you use maven to install your project to generate stuff like that, using an IDEs run / build command will most likely not work, as it doesn't do anything with the pom.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.6.0</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <arguments>
      <argument>--output</argument>
      <argument>target/generated-sources/</argument>
    </arguments>
  </configuration>
</plugin>
  1. You are using the maven-jar-plugin which is a bit confusing to me, as I thought you want to create a web application, so a war file should be generated. You can do that by simply adding <packaging>war</packaging> anywhere in your pom under <project> (for example under your <artifactId>)
  • Related