Giter VIP home page Giter VIP logo

querydsl-entityql-maven-plugin's Introduction

Static Code Generation for QueryDSL EntityQL

Build Status

This repository is a part of EntityQL project. It is a Maven Plugin that generates QueryDSL-SQL compatible Static Models from JPA Entities.

Generating JPA Models:

  • add the following Maven Repositories to your pom.xml:
    <!-- JitPack repository for EntityQL Maven plugin -->
    <pluginRepositories>
        <pluginRepository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </pluginRepository>
    </pluginRepositories>
  • add the following Maven Plugin to your pom.xml:
    <plugin>
        <groupId>com.github.eXsio</groupId>
        <artifactId>querydsl-entityql-maven-plugin</artifactId>
        <version>1.2.8</version>
        <configuration>
            <generators>
                <!-- use as many generators as you need -->
                <generator>
                    <sourcePackage>pl.exsio.querydsl.entityql.examples.entity</sourcePackage>
                    <destinationPackage>pl.exsio.querydsl.entityql.examples.entity.generated</destinationPackage>
                    <!-- below settings are not required. If not provided, will have the below, default values -->
                    <!-- <destinationPath>${project.basedir}\src\main\java</destinationPath>-->
                    <!-- <filenamePattern>Q%s.java</filenamePattern>-->
                </generator>
            </generators>
        </configuration>
        <dependencies>
            <!-- use EntityQL in the same version as in project -->
            <dependency>
                <groupId>com.github.eXsio</groupId>
                <artifactId>querydsl-entityql</artifactId>
                <version>3.2.0</version>
            </dependency>
        </dependencies>
    </plugin>
  • generate Static Models by running the querydsl-entityql:generate-models plugin from Command Line or your IDE

Generating Spring Data JDBC Models:

  • add the following Maven Repositories to your pom.xml:
    <!-- JitPack repository for EntityQL Maven plugin -->
    <pluginRepositories>
        <pluginRepository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </pluginRepository>
    </pluginRepositories>
  • add the following Maven Plugin to your pom.xml:
    <plugin>
        <groupId>com.github.eXsio</groupId>
        <artifactId>querydsl-entityql-maven-plugin</artifactId>
        <version>1.2.8</version>
        <configuration>
            <generators>
                <!-- use as many generators as you need -->
                <generator>
                    <type>SPRING_DATA_JDBC</type>
                    <sourcePackage>pl.exsio.querydsl.entityql.examples.entity</sourcePackage>
                    <destinationPackage>pl.exsio.querydsl.entityql.examples.entity.generated</destinationPackage>
                    <!-- out of the box you can use one of the 2 provided naming strategies
                        - pl.exsio.querydsl.entityql.jdbc.UpperCaseWithUnderscoresNamingStrategy
                        - pl.exsio.querydsl.entityql.jdbc.DefaultNamingStrategy
                        If you want to use your own custom one, you will have to add a new <dependency/> that contains it
                    -->
                    <params>
                        <namingStrategy>pl.exsio.querydsl.entityql.jdbc.UpperCaseWithUnderscoresNamingStrategy</namingStrategy>
                    </params>
                    <!-- below settings are not required. If not provided, will have the below, default values -->
                    <!-- <destinationPath>${project.basedir}\src\main\java</destinationPath>-->
                    <!-- <filenamePattern>Q%s.java</filenamePattern>-->
                </generator>
            </generators>
        </configuration>
        <dependencies>
            <!-- use EntityQL in the same version as in project -->
            <dependency>
                <groupId>com.github.eXsio</groupId>
                <artifactId>querydsl-entityql</artifactId>
                <version>3.2.0</version>
            </dependency>
        </dependencies>
    </plugin>
  • generate Static Models by running the querydsl-entityql:generate-models plugin from Command Line or your IDE

Using Models:

  • add the following Maven Repositories to your pom.xml:
<!-- JitPack repository for EntityQL library -->
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
  • add the following Maven Dependencies to your pom.xml:
   <!-- EntityQL in the newest available version -->
   <dependency>
       <groupId>com.github.eXsio</groupId>
       <artifactId>querydsl-entityql</artifactId>
       <version>3.2.0</version>
   </dependency>

   <!-- basic dependencies required by EntityQL -->
   <dependency>
       <groupId>com.querydsl</groupId>
       <artifactId>querydsl-sql</artifactId>
       <version>5.0.0</version>
   </dependency>
   <dependency>
       <groupId>org.hibernate.javax.persistence</groupId>
       <artifactId>hibernate-jpa-2.1-api</artifactId>
       <version>1.0.2.Final</version>
   </dependency>

   <!-- dependencies required by EntityQL when used with Spring -->
   <dependency>
       <groupId>com.querydsl</groupId>
       <artifactId>querydsl-sql-spring</artifactId>
       <version>5.0.0</version>
   </dependency>
   <dependency>
       <groupId>org.reflections</groupId>
       <artifactId>reflections</artifactId>
       <version>0.9.12</version>
   </dependency>
  • configure QueryDSL:
@Bean
public SQLTemplates sqlTemplates() {
    //choose the implementation that matches your database engine
    return new H2Templates(); 
}

@Bean
public SQLQueryFactory queryFactory(DataSource dataSource, SQLTemplates sqlTemplates) {
    //last param is an optional varargs String with all the java.lang.Enum packages that you use in your Entities
    return new EntityQlQueryFactory(new Configuration(sqlTemplates), dataSource, "your.enums.package");
}
  • Create SQL Queries with generated models:
//obtain instances of generated models
 QBook book = QBook.INSTANCE; 
 QOrder order = QOrder.INSTANCE;
 QOrderItem orderItem = QOrderItem.INSTANCE;

//use them by creating and executing a Native Query using QueryDSL API
Long count = queryFactory.select(count())
                .from(
                        select(
                                 book.name, 
                                 order.id
                        )
                        .from(orderItem)
                        .innerJoin(orderItem.book, book)
                        .innerJoin(orderItem.order, order)
                        .where(book.price.gt(new BigDecimal("80")))
                        .groupBy(book.category) 
                ).fetchOne();

Don't like calling Maven plugin manually?

Use this Maven Run Configuration to automate things:

mvn clean install querydsl-entityql:generate-models install --also-make -f pom.xml

You can create and save this Run configuration in your IDE to make things easy

Alternatively you can configure custom Maven profile that will pre-compile and generate the Query Models:

    <profile>
        <id>generate-query-models</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <executions>
                        <execution>
                            <id>generate</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        ...
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.github.eXsio</groupId>
                    <artifactId>querydsl-entityql-maven-plugin</artifactId>
                    <version>1.2.8</version>
                    <executions>
                        <execution>
                            <id>generate</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate-models</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        ...
                    </configuration>
                    <dependencies>
                        ...
                    </dependencies>
                </plugin>

            </plugins>
        </build>
    </profile>

This way Query Model generation will be a part of standard clean-install Maven build, as long as the Profile is enabled. It will also ensure that Models are based on the current source code.

Examples

Feel free to browse the Examples Project to find out how to use EntityQL in your code.

querydsl-entityql-maven-plugin's People

Contributors

exsio avatar lpandzic avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

querydsl-entityql-maven-plugin's Issues

test.properties must be preset on test classpath

Hi.
Thanks for making a nice plugin.
I would like to use the EntityQL project, but I am currently working on a project with Gradle.
So I am going to make a Gradle plugin by referring to this plugin.

So I run a test to analyze this project, but the test fails to run with the following error.

java.lang.AssertionError: test.properties must be preset on test classpath, see https://github.com/takari/takari-plugin-testing-project/blob/master/testproperties.md for me details

What values can I put in the test.properties item?

Please answer.
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.