2

This is a follow up question to this SO Question. I am using jooq codegen for auto generation of jooq classes from jpa entities. I am planning to use jooq as a SQL query builder and execute the actual queries with JPA EntityManager. But jooq is generating the tables from entities with schema defaulted to PUBLIC.

For example if my query has to be

select SCHEMA_A.colA, SCHEMA_A.colB from SCHEMA_A.tableA;

jooq is generating

select PUBLIC.colA, PUBLIC.colB from PUBLIC.tableA;

This is making the query fail when I fire the following query because the schema is invalid.

entityManager.createNativeQuery(sqlString).getResultList();

What configuration do I need to add to make the autogenerated code contain the actual schema name?

Codegen:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen-maven</artifactId>
            <version>3.9.1</version>

            <!-- The plugin should hook into the generate goal -->
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>

            <dependencies>
                <dependency>
                    <groupId>org.jooq</groupId>
                    <artifactId>jooq-meta-extensions</artifactId>
                    <version>3.9.1</version>
                </dependency>

                <dependency>
                    <groupId>com.yaswanth</groupId>
                    <artifactId>domain</artifactId>
                    <version>${project.version}</version>
                </dependency>
            </dependencies>

            <configuration>

                <!-- Generator parameters -->
                <generator>

                    <database>
                        <name>org.jooq.util.jpa.JPADatabase</name>
                        <outputSchema>[SCHEMA_A]</outputSchema>
                        <properties>
                            <!-- A comma separated list of Java packages, that contain your entities -->
                            <property>
                                <key>packages</key>
                                <value>com.yaswanth.domain.entity</value>
                            </property>
                        </properties>
                    </database>

                    <target>
                        <packageName>com.yaswanth.domain.entity.jooq</packageName>
                        <directory>target/generated-sources/jooq</directory>
                    </target>
                </generator>
            </configuration>
        </plugin>
    </plugins>
</build>

jooq spring beans config

<bean id="dslContext" class="org.jooq.impl.DefaultDSLContext">
    <constructor-arg ref="jooqConfig" />
</bean>

<bean id="jooqConfig" class="org.jooq.impl.DefaultConfiguration">
    <property name="SQLDialect" value="MYSQL" />
    <property name="dataSource" ref="myDataSource" />
    <property name="settings" ref="settings"/>
</bean>

<bean id="settings" class="org.jooq.conf.Settings">
    <property name="renderSchema" value="true" />
</bean>
4

1 回答 1

1

The <outputSchema/> element cannot stand alone, it must be paired with an <inputSchema/> element. The reason is that if you leave out the <inputSchema/> element, then all schemata in your database are used for code generation, and it would be unclear what the standalone <outputSchema/> element means.

This misconfiguration should probably be reported as a warning in the log files: https://github.com/jOOQ/jOOQ/issues/6186

More information about the code generator's schema mapping feature here: https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-catalog-and-schema-mapping

Note: This is how the jOOQ code generator behaves in general, the fact that the JPADatabase seems to be a bit particular here doesn't matter. Even with JPA annotated entities, you could have a database that includes several schemata if you specify the @Table(schema = "..."), for instance.

于 2017-05-04T17:34:17.650 回答