2016-01-28 76 views
1

我使用jooq生成POJO我H2分貝表JOOQ產生POJO失蹤GeneratedValue註釋

CREATE TABLE PUBLIC.ABC (
    ID   BIGINT AUTO_INCREMENT PRIMARY KEY, 
    TRADE_DATE DATE, 
    STK_CODE VARCHAR(63), 
    REMARKS  TEXT, 
    TIMESTAMP TIMESTAMP NOT NULL 
); 

但(下同)

{ 

    ... 
    @Id 
    @Column(name = "ID", unique = true, nullable = false, precision = 19) 
    public Long getId() { 
     return this.id; 
    } 
    ... 

} 

缺少生成的代碼@ GeneratedValue註釋,這使得它不可能插入新的記錄與春天的數據休息庫,因爲傳入的對象總是抱怨沒有設置id字段。

什麼配置/變通辦法可以讓jooq正常工作?

下面是相關的POM文件部分我用在編譯時產生的POJO:

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

替代方法

最終去了替代品的路線,讓任何低於插件代碼在添加功能之前碰到同樣的問題:

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

回答

1

從第3.7條開始,這是一個缺失的功能。請參閱:https://github.com/jOOQ/jOOQ/issues/5009

您有幾種解決方法選擇:

  1. 使用search-replace Maven plugin通過@Id @javax.persistence.GeneratedValue(javax.persistence.GenerationType.IDENTITY)補丁生成的代碼通過更換@Id(假設你的所有主鍵AUTO_INCREMENT
  2. 補丁jOOQ-CODEGEN的JavaGenerator「 s org.jooq.util.JavaGenerator.printColumnJPAAnnotation()方法並自己添加該代碼。