Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JOOQ生成的pojo缺少GeneratedValue注释_Java_Sql_Spring_H2_Jooq - Fatal编程技术网

Java JOOQ生成的pojo缺少GeneratedValue注释

Java JOOQ生成的pojo缺少GeneratedValue注释,java,sql,spring,h2,jooq,Java,Sql,Spring,H2,Jooq,我正在使用jooq为我的H2db表生成pojo CREATE TABLE PUBLIC.ABC( ID BIGINT自动递增主键, 交易日期, STK_代码VARCHAR(63), 备注文本, 时间戳时间戳不为空 ); 但是生成的代码(如下所示) 缺少@GeneratedValue注释,这使得无法使用spring数据rest存储库插入新记录,因为传入的对象总是抱怨未设置id字段 为了使jooq正常工作,我可以做哪些配置/工作 下面是我用来在编译时生成pojo的相关pom文件部分: <pl

我正在使用jooq为我的H2db表生成pojo

CREATE TABLE PUBLIC.ABC(
ID BIGINT自动递增主键,
交易日期,
STK_代码VARCHAR(63),
备注文本,
时间戳时间戳不为空
);
但是生成的代码(如下所示)

缺少@GeneratedValue注释,这使得无法使用spring数据rest存储库插入新记录,因为传入的对象总是抱怨未设置id字段

为了使jooq正常工作,我可以做哪些配置/工作

下面是我用来在编译时生成pojo的相关pom文件部分:

<plugin>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen-maven</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
        </dependency>
    </dependencies>
    <configuration>
        <!-- JDBC connection parameters -->
        <jdbc>
            <driver>org.h2.Driver</driver>
            <url>jdbc:h2:${user.home}/</url>
        </jdbc>
        <!-- Generator parameters -->
        <generator>
            <database>
                <name>org.jooq.util.h2.H2Database</name>
                <includes>.*</includes>
                <schemata>
                    <schema>
                        <inputSchema>PUBLIC</inputSchema>
                    </schema>
                </schemata>
            </database>
            <target>
                <packageName>org.abc</packageName>
                <directory>target/generated-sources/jooq</directory>
            </target>
            <generate>
                <pojos>true</pojos>
                <jpaAnnotations>true</jpaAnnotations>
            </generate>
        </generator>
    </configuration>
</plugin>

org.jooq
jooq codegen maven
生成
com.h2数据库
氢
${h2.version}
org.h2.Driver
jdbc:h2:${user.home}/
org.jooq.util.h2.h2数据库
.*
公开的
org.abc
目标/生成源/jooq
真的
真的

解决方法

在添加功能之前,遇到相同问题的任何人都可以选择替换程序,插件代码如下所示:

    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>replacer</artifactId>
        <version>1.5.3</version>
        <executions>
            <execution>
                <phase>prepare-package</phase>
                <goals>
                    <goal>replace</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <basedir>${project.basedir}/${jooq.gen.dir}</basedir>
            <filesToInclude>tables/pojos/*.java</filesToInclude>
            <replacements>
                <replacement>
                    <token>@Id</token>
                    <value>@Id @javax.persistence.GeneratedValue</value>
                </replacement>
            </replacements>
        </configuration>
    </plugin>

com.google.code.maven-replacer-plugin
替代者
1.5.3
准备包装
代替
${project.basedir}/${jooq.gen.dir}
tables/pojos/*.java
@身份证
@Id@javax.persistence.GeneratedValue

从jOOQ 3.7开始,这是一个缺失的功能。见:

您有几个解决方案选项:

  • 通过将
    @Id
    替换为
    @Id@javax.persistence.GeneratedValue(javax.persistence.GenerationType.IDENTITY)
    (假设所有主键都是
    自动增量
    )来修补生成的代码
  • 修补jOOQ codegen的
    JavaGenerator
    org.jOOQ.util.JavaGenerator.printColumnJPAAnnotation()
    方法,然后自己添加该代码
  •     <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>1.5.3</version>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <basedir>${project.basedir}/${jooq.gen.dir}</basedir>
                <filesToInclude>tables/pojos/*.java</filesToInclude>
                <replacements>
                    <replacement>
                        <token>@Id</token>
                        <value>@Id @javax.persistence.GeneratedValue</value>
                    </replacement>
                </replacements>
            </configuration>
        </plugin>