我正在尝试通过在我的项目中使用它来学习JOOQ
。
这是我的实体
@Entity
public class User {
@Id
private String id;
private String userExternalId;
@Column(unique = true)
private String email;
private String clientId;
private String clientSecret;
private boolean active;
public User(@Nonnull final String userExternalId, @Nonnull final String email,
@Nonnull final String clientId, @Nonnull final String clientSecret) {
id = UUID.randomUUID().toString();
this.userExternalId = userExternalId;
this.email = email;
this.clientId = clientId;
this.clientSecret = clientSecret;
active = true;
}
public User() {
}
@Nonnull
public String getId() {
return id;
}
@Nonnull
public String getUserExternalId() {
return userExternalId;
}
@Nonnull
public String getEmail() {
return email;
}
@Nonnull
public String getClientId() {
return clientId;
}
@Nonnull
public String getClientSecret() {
return clientSecret;
}
public boolean isActive() {
return active;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", externalUserId='" + userExternalId + '\'' +
", email='" + email + '\'' +
", clientId='" + clientId + '\'' +
", clientSecret='" + clientSecret + '\'' +
", active=" + active +
'}';
}
}
这是UserService
类
public class UserService {
private final EntityManager entityManager;
public UserService(@Nonnull final EntityManager entityManager) {
this.entityManager = entityManager;
}
@Nonnull
public User createUser(@Nonnull final User user) {
entityManager.persist(user);
return user;
}
}
我的persistence/pom.xml
看起来像
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>oauth</artifactId>
<groupId>com.self</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>persistence</artifactId>
<name>OAuth: Persistence</name>
<properties>
<hibernate-jpa-2.0-api.version>1.0.1.Final</hibernate-jpa-2.0-api.version>
<junit.version>4.11</junit.version>
<hibernate-entitymanager.version>4.3.6.Final</hibernate-entitymanager.version>
<h2.version>1.4.180</h2.version>
<querydsl-version>3.4.2</querydsl-version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>${hibernate-jpa-2.0-api.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-entitymanager.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Specify the maven code generator plugin -->
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.4.1</version>
<!-- The plugin should hook into the generate goal -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<!-- Manage the plugin's dependency. In this example, we'll use a H2 database -->
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
<!-- Specify the plugin configuration.
The configuration format is the same as for the standalone code generator -->
<configuration>
<!-- JDBC connection parameters -->
<jdbc>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:mem:oauth</url>
<user>sa</user>
<password></password>
</jdbc>
<!-- Generator parameters -->
<generator>
<name>org.jooq.util.DefaultGenerator</name>
<database>
<name>org.jooq.util.h2.H2Database</name>
<includes>.*</includes>
<excludes></excludes>
<inputSchema>public</inputSchema>
</database>
<target>
<packageName>com.self.oauth.persistence.entities</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
</plugin>
</plugins>
</build>
</project>
当我运行mvn -pl persistence
时,日志看起来像
➜ oauth git:(master) ✗ mvn -pl persistence
[WARNING]
[WARNING] Some problems were encountered while building the effective settings
[WARNING] 'usePluginRegistry' is deprecated and has no effect. @ /Users/harith/.m2/settings.xml
[WARNING]
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building OAuth: Persistence 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ persistence ---
[INFO] Deleting /Users/harith/code/IdeaProjects/others/oauth/persistence/target
[INFO]
[INFO] --- jooq-codegen-maven:3.4.1:generate (default) @ persistence ---
[INFO] Using this configuration:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.4.0.xsd">
<jdbc>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:mem:oauth</url>
<user>sa</user>
</jdbc>
<generator>
<name>org.jooq.util.DefaultGenerator</name>
<database>
<name>org.jooq.util.h2.H2Database</name>
<includes>.*</includes>
<excludes></excludes>
<includeExcludeColumns>false</includeExcludeColumns>
<recordVersionFields></recordVersionFields>
<recordTimestampFields></recordTimestampFields>
<syntheticPrimaryKeys></syntheticPrimaryKeys>
<overridePrimaryKeys></overridePrimaryKeys>
<dateAsTimestamp>false</dateAsTimestamp>
<unsignedTypes>true</unsignedTypes>
<inputSchema>public</inputSchema>
<outputSchemaToDefault>false</outputSchemaToDefault>
</database>
<target>
<packageName>com.self.oauth.persistence.entities</packageName>
<directory>/Users/harith/code/IdeaProjects/others/oauth/persistence/target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
[INFO] License parameters
[INFO] ----------------------------------------------------------
[INFO] Thank you for using jOOQ and jOOQ's code generator
[INFO]
[INFO] Database parameters
[INFO] ----------------------------------------------------------
[INFO] dialect : H2
[INFO] URL : jdbc:h2:mem:oauth
[INFO] target dir : /Users/harith/code/IdeaProjects/others/oauth/persistence/target/generated-sources/jooq
[INFO] target package : com.self.oauth.persistence.entities
[INFO] includes : [.*]
[INFO] excludes : []
[INFO] includeExcludeColumns : false
[INFO] ----------------------------------------------------------
[INFO]
[INFO] DefaultGenerator parameters
[INFO] ----------------------------------------------------------
[INFO] strategy : class org.jooq.util.DefaultGeneratorStrategy
[INFO] deprecated : true
[INFO] generated annotation : true
[INFO] JPA annotations : false
[INFO] validation annotations : false
[INFO] instance fields : true
[INFO] records : true
[INFO] pojos : false
[INFO] immutable pojos : false
[INFO] interfaces : false
[INFO] daos : false
[INFO] relations : true
[INFO] global references : true
[INFO] ----------------------------------------------------------
[INFO]
[INFO] Generation remarks
[INFO] ----------------------------------------------------------
[INFO] none
[INFO]
[INFO] ----------------------------------------------------------
[INFO] Emptying : /Users/harith/code/IdeaProjects/others/oauth/persistence/target/generated-sources/jooq/com/self/oauth/persistence/entities
[WARNING] No schemata were loaded : Please check your connection settings, and whether your database (and your database version!) is really supported by jOOQ. Also, check the case-sensitivity in your configured <inputSchema/> elements : [public]
[INFO] Generating schemata : Total: 0
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ persistence ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ persistence ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/harith/code/IdeaProjects/others/oauth/persistence/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ persistence ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ persistence ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/harith/code/IdeaProjects/others/oauth/persistence/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ persistence ---
[INFO] Surefire report directory: /Users/harith/code/IdeaProjects/others/oauth/persistence/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.self.oauth.persistence.entities.UserServiceTest
Aug 09, 2014 9:28:15 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Aug 09, 2014 9:28:15 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Aug 09, 2014 9:28:15 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Aug 09, 2014 9:28:15 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: oauthTest
...]
Aug 09, 2014 9:28:15 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Aug 09, 2014 9:28:15 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 09, 2014 9:28:15 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 09, 2014 9:28:15 AM org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl processProperties
WARN: HHH000059: Defining hibernate.transaction.flush_before_completion=true ignored in HEM
Aug 09, 2014 9:28:15 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Aug 09, 2014 9:28:15 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 09, 2014 9:28:15 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.h2.Driver] at URL [jdbc:h2:mem:oauth]
Aug 09, 2014 9:28:15 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=sa, password=****}
Aug 09, 2014 9:28:15 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Aug 09, 2014 9:28:15 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Aug 09, 2014 9:28:15 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Aug 09, 2014 9:28:15 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Aug 09, 2014 9:28:15 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Aug 09, 2014 9:28:15 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Aug 09, 2014 9:28:15 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Aug 09, 2014 9:28:15 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: User
Aug 09, 2014 9:28:15 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: User
Aug 09, 2014 9:28:15 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: User
Aug 09, 2014 9:28:15 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate:
insert
into
User
(active, clientId, clientSecret, email, userExternalId, id)
values
(?, ?, ?, ?, ?, ?)
Aug 09, 2014 9:28:16 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Aug 09, 2014 9:28:16 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Aug 09, 2014 9:28:16 AM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Aug 09, 2014 9:28:16 AM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: oauthTest
...]
Aug 09, 2014 9:28:16 AM org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl processProperties
WARN: HHH000059: Defining hibernate.transaction.flush_before_completion=true ignored in HEM
Aug 09, 2014 9:28:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 09, 2014 9:28:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.h2.Driver] at URL [jdbc:h2:mem:oauth]
Aug 09, 2014 9:28:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=sa, password=****}
Aug 09, 2014 9:28:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Aug 09, 2014 9:28:16 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Aug 09, 2014 9:28:16 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Aug 09, 2014 9:28:16 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Aug 09, 2014 9:28:16 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Aug 09, 2014 9:28:16 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Aug 09, 2014 9:28:16 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Aug 09, 2014 9:28:16 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: OAUTH.PUBLIC.USER
Aug 09, 2014 9:28:16 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [clientid, clientsecret, userexternalid, active, id, email]
Aug 09, 2014 9:28:16 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
Aug 09, 2014 9:28:16 AM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary_key_2, uk_e6gkqunxajvyxl5uctpl2vl2p_index_2]
Aug 09, 2014 9:28:16 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Aug 09, 2014 9:28:16 AM org.hibernate.jpa.internal.EntityManagerFactoryRegistry addEntityManagerFactory
WARN: HHH000436: Entity manager factory name (oauthTest) is already registered. If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
Hibernate:
insert
into
User
(active, clientId, clientSecret, email, userExternalId, id)
values
(?, ?, ?, ?, ?, ?)
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.112 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ persistence ---
[INFO] Building jar: /Users/harith/code/IdeaProjects/others/oauth/persistence/target/persistence-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-sortpom-plugin:2.3.0:sort (default) @ persistence ---
[INFO] Sorting file /Users/harith/code/IdeaProjects/others/oauth/persistence/pom.xml
[INFO] Pom file is already sorted, exiting
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ persistence ---
[INFO] Installing /Users/harith/code/IdeaProjects/others/oauth/persistence/target/persistence-1.0-SNAPSHOT.jar to /Users/harith/.m2/repository/com/self/persistence/1.0-SNAPSHOT/persistence-1.0-SNAPSHOT.jar
[INFO] Installing /Users/harith/code/IdeaProjects/others/oauth/persistence/pom.xml to /Users/harith/.m2/repository/com/self/persistence/1.0-SNAPSHOT/persistence-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.892 s
[INFO] Finished at: 2014-08-09T09:28:16-08:00
[INFO] Final Memory: 19M/84M
[INFO] ------------------------------------------------------------------------
问题
为什么资源不是由JOOQ
生成的?我错过了什么?
P.S。我正在使用Java 1.8
答案 0 :(得分:1)
您正在使用以下H2 JDBC网址:
jdbc:h2:mem:oauth
根据H2 database docs,这意味着没有数据被保留:
对于某些用例(例如:快速原型设计,测试,高性能操作,只读数据库),可能不需要保留数据或持久更改数据。此数据库支持内存模式,其中数据不会持久存在。
很可能在Maven插件的连续运行之间没有持久存储数据库。
除此之外,从Maven日志输出看,好像jOOQ代码生成器在 Hibernate的hbm2dll工具之前运行。
解决方法(但可能不是一个合理的解决方案)是使用H2基于文件的"embedded connection":
jdbc:h2:~/test