Rimantas Jacikevicius
Rimantas Jacikevicius

Reputation: 527

JOOQ generate DAO for view

Im trying to auto generate dao for a view. I've set the configuration prerly :

                            <generate>
                                <!--<deprecated>false</deprecated>-->
                                <daos>true</daos>
                            </generate>

whitch generates dao's for tables, but not for views. As per documentation states :

If you're using jOOQ's code generator, you can configure it to generate POJOs and DAOs for you. jOOQ then generates one DAO per UpdatableRecord, i.e. per table with a single-column primary key.

and

public interface UpdatableRecord<R extends UpdatableRecord<R>> extends TableRecord<R> A common interface for records that can be stored back to the database again. Any Record can be updatable, if

it represents a record from a table or view - a TableRecord its underlying table or view has a "main unique key", i.e. a primary key or at least one unique key The "main unique key" is used by jOOQ to perform the various operations that can be performed on an UpdatableRecord:

Witch basically means that DAO's are generated for updatable records, and updatable records can by both tables and views. However UpdatableRecord and Keys are not generated for my views. Is it possible to achieve this on view at all? Or is it not meant to?

Here's one of my views

CREATE OR REPLACE VIEW test AS 
 SELECT test_table.id, test_table.test_id, test_table.foo
   FROM private.test_table;

ALTER TABLE test
  OWNER TO postgres;
GRANT ALL ON TABLE test TO postgres;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE test TO viewers;


CREATE OR REPLACE RULE "TEST_INSERT" AS
    ON INSERT TO test DO INSTEAD  INSERT INTO private.test_table (test_id, foo) 
  VALUES (new.test_id, new.foo);

CREATE OR REPLACE RULE "TEST_UPDATE" AS
    ON UPDATE TO test DO INSTEAD  UPDATE private.test_table SET test_id = new.test_id, foo = new.foo
  WHERE test_table.id = old.id;

P.S.

I have my own suspicion it that it has something to do with "main unique key" mentioned in the documentation, because no keys are generated in the Keys class.

UPDATE

After a promising answer I've received, here's my configuration entry:

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

            <dependencies>
                <dependency>
                    <groupId>postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>9.1-901-1.jdbc4</version>
                </dependency>
            </dependencies>

            <executions>
                <execution>
                    <id>regenerate-jooq-sources</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>

                    <configuration>

                        <!-- JDBC connection parameters -->
                        <jdbc>
                            <driver>org.postgresql.Driver</driver>
                            <url>
                                jdbc:postgresql://localhost:5432/core
                            </url>
                            <user>client</user>
                            <password>***</password>
                        </jdbc>

                        <!-- Generator parameters -->
                        <generator>
                            <name>org.jooq.util.DefaultGenerator</name>
                            <database>
                                <syntheticPrimaryKeys>SCHEMA\.TABLE\.COLUMN(1|2)</syntheticPrimaryKeys>
                                <overridePrimaryKeys>overrid_primmary_key</overridePrimaryKeys>
                                <name>
                                    org.jooq.util.postgres.PostgresDatabase
                                </name>
                                <includes>.*</includes>
                                <excludes></excludes>
                                <inputSchema>public</inputSchema>
                            </database>
                            <generate>
                                <!--<deprecated>false</deprecated>-->
                                <daos>true</daos>
                                <interfaces>true</interfaces>
                            </generate>

                            <target>

                                <packageName>
                                    com.rfid.server.jooq
                                </packageName>
                                <directory>
                                    ${basedir}/src/main/java/
                                </directory>
                            </target>
                        </generator>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Upvotes: 3

Views: 3681

Answers (2)

Rimantas Jacikevicius
Rimantas Jacikevicius

Reputation: 527

With the help of Lukas Eder answer, I solved it, heres the important parts of the configuration:

                            <database>
                                <!--force generating id'sfor everything in public schema, that has an 'id' field-->
                                <syntheticPrimaryKeys>public\..*\.id</syntheticPrimaryKeys>
                                <!--name for fake primary key-->
                                <overridePrimaryKeys>override_primmary_key</overridePrimaryKeys>
                                <name>
                                    org.jooq.util.postgres.PostgresDatabase
                                </name>
                                <includes>.*</includes>
                                <excludes></excludes>
                                <inputSchema>public</inputSchema>
                            </database>
                            <generate>
                                <daos>true</daos>
                            </generate>

Upvotes: 4

Lukas Eder
Lukas Eder

Reputation: 220877

As of jOOQ 3.5, jOOQ's code generator does not yet detect a view's underlying primary key information, if such information exists. Few databases can actually reliably provide such information, so perhaps, the Javadoc you cited might be a bit misleading. More information on that subject can be found in this question:

How to discover the underlying primary (or unique) key columns from an Oracle view

However, you can specify the primary key to the code generator using either the <syntheticPrimaryKeys> flag to generate additional key information, or you can use the <overridePrimaryKeys> flag to use unique keys placed on (materialized) views as the view's primary key.

<!-- A regular expression matching all columns that participate in "synthetic" primary
      keys, which should be placed on generated UpdatableRecords, to be used with

      - UpdatableRecord.store()
      - UpdatableRecord.update()
      - UpdatableRecord.delete()
      - UpdatableRecord.refresh()

     Synthetic primary keys will override existing primary keys. -->
<syntheticPrimaryKeys>SCHEMA\.TABLE\.COLUMN(1|2)</syntheticPrimaryKeys>

<!-- All (UNIQUE) key names that should be used instead of primary keys on
     generated UpdatableRecords, to be used with

      - UpdatableRecord.store()
      - UpdatableRecord.update()
      - UpdatableRecord.delete()
      - UpdatableRecord.refresh()

      If several keys match, a warning is emitted and the first one encountered will
      be used.

      This flag will also replace synthetic primary keys, if it matches. -->
<overridePrimaryKeys>MY_UNIQUE_KEY_NAME</overridePrimaryKeys>

Setting these flags will turn your generated Records into UpdatableRecords, and thus will also generate corresponding DAOs

More information on these code generation flags can be found here:

http://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/

Upvotes: 1

Related Questions