Skip to content

Commit

Permalink
Merge pull request #1 from kaczynskid/master
Browse files Browse the repository at this point in the history
Proper solution without the need for patching
  • Loading branch information
renataogarcia committed Nov 19, 2015
2 parents eb727fc + 42f0119 commit 3ce759a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.class
*.iml

# Package Files #
*.jar
Expand Down
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
querydsl-groovy-maven
=====================

Sample using Querydsl APT Groovy and Maven
Sample using QueryDSL APT processing in a Groovy and Maven project.

Instructions:
`Foo` is an `@Entity`. `QFoo` is a QueryDSL query object for `Foo` generated with APT (JSR-269).
The annotation processor used is declared in `com.querydsl:querydsl-apt` in `META-INF/services/javax.annotation.processing.Processor`.
`QFoo` is also referenced by `FooRepository` to prove that it's generated and compiled on the fly
and can be safely used in other classes, even within the same project and source directory.
Also as an example `generatedSourcesDirectory` is changed in `groovy-eclipse-compiler` configuration
to prove that plugin configuration is applied to the annotation processor as well.

In order for this to work you'll need to patch the querydsl-apt-2.6.0.jar file and include [this](https://github.com/downloads/renataogarcia/querydsl-groovy-maven/javax.annotation.processing.Processor) to the META-INF/services directory.
Tested with:

```
$ mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00)
Java version: 1.8.0_60, vendor: Oracle Corporation
```
125 changes: 70 additions & 55 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,61 +1,76 @@
<project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample</groupId>
<artifactId>querydsl-groovy-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<querydsl.version>2.6.0</querydsl.version>
</properties>
<groupId>sample</groupId>
<artifactId>querydsl-groovy-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<fork>true</fork>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.6.0-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>

<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<groovy.version>2.4.4</groovy.version>
<querydsl.version>4.0.6</querydsl.version>
</properties>

<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>

</dependencies>
</project>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<generatedSourcesDirectory>${project.build.directory}/generated-sources/java</generatedSourcesDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.9.1-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.3.7-01</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jpa</classifier>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
11 changes: 7 additions & 4 deletions src/main/java/sample/Foo.groovy
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package sample;
package sample

import javax.persistence.Entity;
import javax.persistence.Entity
import javax.persistence.Id

@Entity
class Foo {

String bar

@Id
Long id

String bar
}
23 changes: 23 additions & 0 deletions src/main/java/sample/FooRepository.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sample

import com.querydsl.jpa.impl.JPAQueryFactory

import javax.persistence.EntityManager

import static sample.QFoo.foo

class FooRepository {

@Delegate
final JPAQueryFactory queryFactory

FooRepository(EntityManager entityManager) {
this.queryFactory = new JPAQueryFactory(entityManager)
}

Foo findOne(Long id) {
return selectFrom(foo)
.where(foo.id.eq(id))
.fetchFirst()
}
}

0 comments on commit 3ce759a

Please sign in to comment.