Hibernate SchemaExport 不生成 sql 脚本
Hibernate SchemaExport doesn't generate sql script
谁能帮我解释为什么下面的代码不能生成 SQL 创建脚本?
Hibernate 版本是 5.2.9.Final。
我尝试了不同的方法,使用了确切的 class 名称、包位置、与现有数据库的连接,但不幸的是,没有一种方法适合我。
我在互联网上找到的所有代码片段看起来都像我的。
Class<PostgreSQL95Dialect> dialect = PostgreSQL95Dialect.class;
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySetting(Environment.DIALECT, dialect)
.build();
MetadataSources metadataSources = new MetadataSources(registry);
Set<Class<?>> entities = new Reflections("").getTypesAnnotatedWith(Entity.class);
entities.forEach(e -> metadataSources.addAnnotatedClass(e.getClass()));
System.out.println(String.format("entities : %d", entities.size()));
Metadata metadata = metadataSources.buildMetadata();
new SchemaExport()
.setFormat(true)
.setDelimiter(";")
.setOutputFile(String.format("ddl_%s.sql ", dialect.getSimpleName()))
.execute(EnumSet.of(TargetType.STDOUT, TargetType.SCRIPT), SchemaExport.Action.BOTH, metadata);
StandardServiceRegistryBuilder.destroy(registry);
代码执行结果如下
Apr 27, 2017 7:26:43 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.9.Final}
Apr 27, 2017 7:26:43 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
entities : 144
Apr 27, 2017 7:26:44 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Apr 27, 2017 7:26:44 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl makeLobCreatorBuilder
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Apr 27, 2017 7:26:44 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@5b239d7d
Apr 27, 2017 7:26:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Apr 27, 2017 7:26:44 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@74455848'
如有任何帮助,我将不胜感激。
hibernate 存在配置问题,请再次检查示例代码下方 hibernate.refer 的配置。
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">explorecalifornia</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.connection.password">abc123</property>
</session-factory>
</hibernate-configuration>
终于找到祸根了:)
我创建了一个测试实体并尝试仅为它生成脚本并且代码有效!
所以唯一可能出错的地方是在反射的帮助下得到 类 。我感到羞耻,我是多么的不专心。
而不是写
entities.forEach(e -> metadataSources.addAnnotatedClass(e.getClass()));
我需要这个
entities.forEach(e -> metadataSources.addAnnotatedClass(e));
第一个总是给
class java.lang.Class
谁能帮我解释为什么下面的代码不能生成 SQL 创建脚本? Hibernate 版本是 5.2.9.Final。 我尝试了不同的方法,使用了确切的 class 名称、包位置、与现有数据库的连接,但不幸的是,没有一种方法适合我。 我在互联网上找到的所有代码片段看起来都像我的。
Class<PostgreSQL95Dialect> dialect = PostgreSQL95Dialect.class;
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySetting(Environment.DIALECT, dialect)
.build();
MetadataSources metadataSources = new MetadataSources(registry);
Set<Class<?>> entities = new Reflections("").getTypesAnnotatedWith(Entity.class);
entities.forEach(e -> metadataSources.addAnnotatedClass(e.getClass()));
System.out.println(String.format("entities : %d", entities.size()));
Metadata metadata = metadataSources.buildMetadata();
new SchemaExport()
.setFormat(true)
.setDelimiter(";")
.setOutputFile(String.format("ddl_%s.sql ", dialect.getSimpleName()))
.execute(EnumSet.of(TargetType.STDOUT, TargetType.SCRIPT), SchemaExport.Action.BOTH, metadata);
StandardServiceRegistryBuilder.destroy(registry);
代码执行结果如下
Apr 27, 2017 7:26:43 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.9.Final}
Apr 27, 2017 7:26:43 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
entities : 144
Apr 27, 2017 7:26:44 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Apr 27, 2017 7:26:44 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl makeLobCreatorBuilder
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Apr 27, 2017 7:26:44 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@5b239d7d
Apr 27, 2017 7:26:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Apr 27, 2017 7:26:44 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@74455848'
如有任何帮助,我将不胜感激。
hibernate 存在配置问题,请再次检查示例代码下方 hibernate.refer 的配置。
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">explorecalifornia</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.connection.password">abc123</property>
</session-factory>
</hibernate-configuration>
终于找到祸根了:)
我创建了一个测试实体并尝试仅为它生成脚本并且代码有效! 所以唯一可能出错的地方是在反射的帮助下得到 类 。我感到羞耻,我是多么的不专心。 而不是写
entities.forEach(e -> metadataSources.addAnnotatedClass(e.getClass()));
我需要这个
entities.forEach(e -> metadataSources.addAnnotatedClass(e));
第一个总是给
class java.lang.Class