我使用jooq为我的 H2 db表生成pojo
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字段,因此无法使用spring数据库存储器插入新记录。
我可以做些什么配置/解决方法让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>
解决方法
在添加功能之前,对于遇到相同问题的任何人,最终会使用以下代码路由,插件代码如下:
<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>
答案 0 :(得分:1)
从jOOQ 3.7开始,这是一个缺失的功能。请参阅:https://github.com/jOOQ/jOOQ/issues/5009
您有几种解决方法选项:
@Id
替换为@Id @javax.persistence.GeneratedValue(javax.persistence.GenerationType.IDENTITY)
(假设您的所有主键均为AUTO_INCREMENT
)JavaGenerator
org.jooq.util.JavaGenerator.printColumnJPAAnnotation()
方法并自行添加该代码。