Are you looking for an advent calendar? You found it!

Spring Data JPA Tutorial: Using Querydsl in a Multi-module Maven Project

Lego bricks

I received a question from a reader of my Spring Data book last week and decided to publish my answer as a blog post. The question was:

How can I use Querydsl with Spring Data JPA in a multi-module Maven project?

This blog post answers to that question and describes how we can create a Maven project which has two modules:

  1. The model module which contains the entity class of our example.
  2. The service module which contains the Spring Data JPA repository and a simple service implementation.

We can create this project by following these steps:

  1. Create the parent project.
  2. Create the model module.
  3. Create the service module.

These steps are described with more details in the following sections.

Note: This blog post is not a Querydsl or Maven multi-module project tutorial. The following web pages gives us more information about these subjects:

Let’s get started.

Creating the Parent Project

We can create the parent project by following these steps:

  1. Create a pom.xml file to the root directory of our project.
  2. Add the modules of our example to the created pom.xml file.
  3. Configure the dependencies of our project in the dependency management section of the created pom.xml file.

The skeleton of our pom.xml file looks as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.petrikainulainen.spring.data.jpa</groupId>
    <artifactId>querydsl-multi-module</artifactId>
    <packaging>pom</packaging>
    <version>0.1-SNAPSHOT</version>
    
	<!-- Add module configuration here -->

    <!-- Build configuration is omitted -->
    <dependencyManagement>
		<!-- Add dependencies here -->
    </dependencyManagement>
</project>

Let’s move on and find out how we can add the modules of our example application to the created pom.xml file and configure the required dependencies.

Adding Module Configuration

We can add the module configuration to the POM file of the parent project by following these steps:

  1. Add a module called model to the POM file.
  2. Add a module called service to the pom.xml file.

The module configuration of our parent project looks as follows:

<modules>
     <module>model</module>
     <module>service</module>
</modules>

We are now ready to declare the dependencies of our project. Let’s find out how this is done.

Configuring the Required Dependencies

We can configure the required dependencies in the dependency management section of our POM file by following these steps:

  1. Add the Spring Data JPA dependency to the POM file.
  2. Add the Querydsl dependencies to the pom.xml file.

The relevant dependency declarations looks as follows:

<!-- Spring Data JPA -->
<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-jpa</artifactId>
	<version>1.3.2.RELEASE</version>
</dependency>
<!-- Querydsl -->
<dependency>
	<groupId>com.mysema.querydsl</groupId>
	<artifactId>querydsl-core</artifactId>
	<version>2.8.0</version>
</dependency>
<dependency>
	<groupId>com.mysema.querydsl</groupId>
	<artifactId>querydsl-apt</artifactId>
	<version>2.8.0</version>
</dependency>
<dependency>
	<groupId>com.mysema.querydsl</groupId>
	<artifactId>querydsl-jpa</artifactId>
	<version>2.8.0</version>
</dependency>

Note: The other dependencies of our example application are omitted for the sake of clarity.

Let’s move on and create the Maven configuration of the model module.

Creating the Model Module

We can create the model module by following these steps:

  1. Create a pom.xml file to the model directory.
  2. Configure the APT Maven plugin.
  3. Configure the required dependencies.

The skeleton of the model/pom.xml file looks as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>querydsl-multi-module</artifactId>
        <groupId>net.petrikainulainen.spring.data.jpa</groupId>
        <version>0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>querydsl-multi-module-model</artifactId>
    <version>${project.parent.version}</version>
    <build>
        <plugins>
            <!-- Configure the APT Maven Plugin here -->
        </plugins>
    </build>

    <dependencies>
		<!-- Configure the dependencies here -->
    </dependencies>
</project>

Let’s start by adding the configuration of the APT Maven plugin to the model/pom.xml file.

Configuring the APT Maven Plugin

We can configure the APT Maven plugin by following these steps:

  1. Add the plugin declaration to the plugins section of the model/pom.xml file.
  2. Create an execution which runs the process goal when the generate-sources lifecycle phase is executed.
  3. Specify the target directory in which the Querydsl query types are generated.
  4. Configure the APT code generator to look for JPA annotations.

The configuration of the APT Maven plugin looks as follows:

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.1</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <!-- Specifies the directory in which the query types are generated -->
                <outputDirectory>target/generated-sources</outputDirectory>
                <!-- States that the APT code generator should look for JPA annotations -->
                <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>

Let’s move on and find out how we can configure the dependencies of the model module.

Configuring the Required Dependencies

We can configure the dependencies of the model module by adding the Querydsl dependencies to the dependencies section of the POM file. The dependency declarations of the model/pom.xml file looks as follows:

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-core</artifactId>
</dependency>
<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
</dependency>
<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
</dependency>

We have now successfully created the model module. Let’s find out how we can create the service module.

Creating the Service Module

We can create the service module by following these steps:

  1. Create a pom.xml file to the service directory.
  2. Add the dependency of our model module to the dependencies section of the created pom.xml file. This ensures that we can use the created Querydsl query types in the service module.

The service/pom.xml file looks as follows:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>querydsl-multi-module</artifactId>
        <groupId>net.petrikainulainen.spring.data.jpa</groupId>
        <version>0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>querydsl-multi-module-service</artifactId>
    <version>${project.parent.version}</version>

    <dependencies>
        <!--
            Include the module which contains the model object
            and the Querydsl query type.
        -->
        <dependency>
            <groupId>net.petrikainulainen.spring.data.jpa</groupId>
            <artifactId>querydsl-multi-module-model</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
		<!-- Other dependencies are omitted. -->
    </dependencies>
</Project>

We are done. Let’s spend a moment to summarize what we have learned.

Summary

We have now successfully created a multi-module Maven project which uses Querydsl and Spring Data JPA. This blog post has taught us two things:

  1. We have to add the APT Maven plugin configuration to the module which contains our entities.
  2. We can use Querydsl query types in another module by adding the module which contains our entities as its dependency.

The source code of the example application is available at Github.

If you want to learn more about Spring Data JPA, you should read all parts of my Spring Data JPA tutorial.
If you enjoyed reading this blog post, you should follow me on Twitter:

About the Author

Petri Kainulainen is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.

About Petri Kainulainen →

6 comments… add one

  • Nice blog post. I have small correction to suggest. The link to the maven plugin should be
    https://github.com/mysema/apt-maven-plugin
    not http://mojo.codehaus.org/apt-maven-plugin/

    Also the artifactId changed from maven-apt-plugin to apt-maven-plugin

    The apt-maven-plugin of codehaus is for the APT processor of Java 5, which is different from Java 6’s APT

    Reply
    • Thanks for the Feedback! I will make these changes later today after I have finished working for the man. :)

      Reply
      • Done.

        Reply
  • Thanks for this wonderful tutorial. I have implemented according to your source code.
    i am not able to read values from properties file outside the service module.

    Please help me in this.

    Reply
    • I have two questions concerning your application context configuration:

      • Which configuration classes are annotated with the @PropertySource annotation?
      • Where do you configure the PropertySourcesPlaceholderConfigurer bean?

      Also, it would be useful to know how do you try to read values from the properties file.

      Reply
  • Petri estimated,

    May you make an example of Spring Data in a Spring multimodule project? Whereas the web layer, business layer and persistence?

    Greetings and thanks.

    Reply

Leave a Comment