简体   繁体   English

JOOQ生成DAO以供查看

[英]JOOQ generate DAO for view

Im trying to auto generate dao for a view. 我正在尝试自动生成视图的dao。 I've set the configuration prerly : 我已经预先设置好配置:

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

whitch generates dao's for tables, but not for views. 抽搐会为表生成dao,但不会为视图生成。 As per documentation states : 根据文档说明:

If you're using jOOQ's code generator, you can configure it to generate POJOs and DAOs for you. 如果您使用的是jOOQ的代码生成器,则可以对其进行配置以为您生成POJO和DAO。 jOOQ then generates one DAO per UpdatableRecord, ie per table with a single-column primary key. 然后,jOOQ为每个UpdatableRecord(即,每个具有单列主键的表)生成一个DAO。

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. 公共接口UpdatableRecord<R extends UpdatableRecord<R>>扩展TableRecord<R>可以再次存储回数据库的记录的公共接口。 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", ie 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: 它代表表或视图中的记录-TableRecord的基础表或视图具有“主唯一键”,即主键或至少一个唯一键jOOQ使用“主唯一键”执行各种操作可以在UpdatableRecord上执行:

Witch basically means that DAO's are generated for updatable records, and updatable records can by both tables and views. Witch基本上意味着DAO是为可更新记录生成的,并且可更新记录可以通过表和视图来生成。 However UpdatableRecord and Keys are not generated for my views. 但是,不会为我的视图生成UpdatableRecord和键。 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;

PS PS

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. 我个人怀疑它与文档中提到的“主要唯一键”有关,因为在Keys类中没有生成任何键。

UPDATE 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>

With the help of Lukas Eder answer, I solved it, heres the important parts of the configuration: 在Lukas Eder的帮助下,我解决了配置的重要部分:

                            <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>

As of jOOQ 3.5, jOOQ's code generator does not yet detect a view's underlying primary key information, if such information exists. 从jOOQ 3.5开始,jOOQ的代码生成器尚未检测到视图的基础主键信息(如果存在)。 Few databases can actually reliably provide such information, so perhaps, the Javadoc you cited might be a bit misleading. 实际上,很少有数据库能够可靠地提供此类信息,因此,也许您引用的Javadoc可能会引起误解。 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 如何从Oracle视图中发现基础的主(或唯一)键列

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. 但是,您可以使用<syntheticPrimaryKeys>标志来生成代码的其他主键,以指定代码生成器的主键,也可以使用<overridePrimaryKeys>标志来使用放置在(物化的)视图上的唯一键作为视图的主键。

<!-- 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 设置这些标志会将您生成的Records转换为UpdatableRecords ,因此还将生成相应的DAOs

More information on these code generation flags can be found here: 有关这些代码生成标志的更多信息,可以在这里找到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM