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:
- The model module which contains the entity class of our example.
- The service module which contains the Spring Data JPA repository and a simple service implementation.
We can create this project by following these steps:
- Create the parent project.
- Create the model module.
- 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:
- Spring Data JPA Tutorial Part Five: Querydsl
- Guide to Working with Multiple Modules
- Multi-modules projects
Let's get started.
Creating the Parent Project
We can create the parent project by following these steps:
- Create a pom.xml file to the root directory of our project.
- Add the modules of our example to the created pom.xml file.
- 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:
- Add a module called model to the POM file.
- 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:
- Add the Spring Data JPA dependency to the POM file.
- 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:
- Create a pom.xml file to the model directory.
- Configure the APT Maven plugin.
- 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:
- Add the plugin declaration to the plugins section of the model/pom.xml file.
- Create an execution which runs the process goal when the generate-sources lifecycle phase is executed.
- Specify the target directory in which the Querydsl query types are generated.
- 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:
- Create a pom.xml file to the service directory.
- 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:
- We have to add the APT Maven plugin configuration to the module which contains our entities.
- 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.
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
Thanks for the Feedback! I will make these changes later today after I have finished working for the man. :)
Done.
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.
I have two questions concerning your application context configuration:
@PropertySource
annotation?PropertySourcesPlaceholderConfigurer
bean?Also, it would be useful to know how do you try to read values from the properties file.
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.