Kihats
Kihats

Reputation: 3520

Combining Querydsl-jpa and querydsl-sql and code generation

Here is the thing:

  1. I have been using querydsl-jpa in my projects and code generation has never been a problem. I use this plugin in maven:

       <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>maven-apt-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    
  2. Now, I need to also use querydsl-sql and apparently, I can't use the Q-generated classes created by com.querydsl.apt.jpa.JPAAnnotationProcessor. Here is the plugin in maven:

        <plugin>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-maven-plugin</artifactId>
            <version>4.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>export</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                                    <jdbcDriver>com.mysql.cj.jdbc.Driver</jdbcDriver>
                <jdbcUrl>jdbc:mysql://localhost:3306/mydatabase</jdbcUrl>
                <jdbcUser>root</jdbcUser>
                <jdbcPassword></jdbcPassword>                    
                <packageName>com.myproject.domain</packageName>
                <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.13</version>
                </dependency>
            </dependencies>
        </plugin>
    

THE CHALLENGE

  1. The second plugin above generates Q-classes for all schemas in my DBMS (MySql) whereas I have specified the schema to generate Q-classes from.

  2. How do I specify the username, password and jdbcUrl from a file since I don't want to store sensitive information in the git repository.

Upvotes: 1

Views: 3068

Answers (2)

Kihats
Kihats

Reputation: 3520

Here are my solutions:

  1. For challenge one, I haven't found a solution per se but some sort of workaround. I created a user in my DBMS (MySql) that has privileges on the single schema that I am interested in. That way, the user won't be able to generate Q-classes for other schemas. So problem one "solved".

Though I still believe that in the plugin one should be able to specify the schema to be generated. Interestingly enough <schemaPattern></schemaPattern> as suggested by @Rober Bain which is also in the querydsl-sql documentation does not work.

  1. For challenge two, first you need to create a properties files say dev.properties with the needed content

    jdbc-url=jdbc:mysql://localhost:3306/myschema?nullNamePatternMatchesAll=true

    jdbc-user=my_user

    jdbc-password=my_password

    Then, include the following properties-maven-plugin

        <plugin>
             <groupId>org.codehaus.mojo</groupId>
             <artifactId>properties-maven-plugin</artifactId>
             <version>1.0-alpha-2</version>
             <executions>
                 <execution>
                     <phase>initialize</phase>
                     <goals>
                         <goal>read-project-properties</goal>
                     </goals>
                     <configuration>
                         <files>
                             <file>dev.properties</file> // Reference to properties file
                         </files>
                     </configuration>
                 </execution>
             </executions>
         </plugin>   
    

... and in your query-dsl plugin ...

        <plugin>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-maven-plugin</artifactId>
            <version>4.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>export</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
                <jdbcUrl>${jdbc-url}</jdbcUrl>
                <jdbcUser>${jdbc-user}</jdbcUser>
                <jdbcPassword>${jdbc-password}</jdbcPassword>
                <packageName>com.myproject.domain</packageName>
                <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>6.0.6</version>
                </dependency>
            </dependencies>
        </plugin> 

Check out this link for more info Read pom.xml configurations from properties file

Since the above link is down, use Wayback Online to see the original web page.

or

Here is a snapshot of the content enter image description here

and a continuation

enter image description here

Upvotes: 2

Robert Bain
Robert Bain

Reputation: 9576

  1. Use schemaPattern within the configuration element: "a schema name pattern in LIKE pattern form; must match the schema name as it is stored in the database, multiple can be separated by comma (default: null)" from the querydsl docs.

  2. While the doesn't do exactly what you're asking for, I believe it's the standard way of solving this problem. Use encrypted data in a Maven pom.

Upvotes: 0

Related Questions