到目前为止,我们一直试图以编程方式使用 hibernate 4.3.11 中的将hbm2ddl导出到文件的功能,但未成功。对于我们创建的任务 一个小的standallone实用程序,如下所示:
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.MetadataImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateSchemaGenerationUtil {
public static void main(String[] args) {
ServiceRegistry serviceReg = buildCfg();
MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(serviceReg).buildMetadata();
SchemaExport schemaExport = new SchemaExport(metadata);
schemaExport.setOutputFile("hbm2schema.sql");
schemaExport.setHaltOnError(true);
System.out.println("Creating Output sql file at location: hbm2schema.sql");
schemaExport.create(true, false);
((StandardServiceRegistryImpl) serviceReg).destroy();
System.out.println("Done...");
}
public static StandardServiceRegistryImpl buildCfg() {
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml")
.build();
}
}
使用的hibernate.cfg.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/my_rsp_request_creation_test?useSSL=false</property>
<property name="hibernate.connection.username">bla</property>
<property name="hibernate.connection.password">bla-bla</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.jdbc.batch_size">25</property>
<property name="hibernate.order_inserts">true</property>
<property name="hibernate.order_updates">true</property>
<property name="hibernate.max_fetch_depth">3</property>
<mapping resource="request_component.hbm.xml"/>
<mapping resource="request_contract.hbm.xml"/>
<mapping resource="request_dimensions.hbm.xml"/>
<mapping resource="request_product_attributes.hbm.xml"/>
<mapping resource="request_product_certificate.hbm.xml"/>
<mapping resource="request_product_characteristics_no_units.hbm.xml"/>
<mapping resource="request_product_characteristics_with_units.hbm.xml"/>
<mapping resource="request_product_custom_characteristics.hbm.xml"/>
<mapping resource="request_product_delivery_address.hbm.xml"/>
<mapping resource="request_product_payment_type.hbm.xml"/>
<mapping resource="request_product_risk.hbm.xml"/>
<mapping resource="request_product_version.hbm.xml"/>
<mapping resource="request_product_versioned_characteristics_no_units.hbm.xml"/>
<mapping resource="request_product_versioned_characteristics_with_units.hbm.xml"/>
<mapping resource="request_product_versioned_custom_characteristics.hbm.xml"/>
<mapping resource="request_product.hbm.xml"/>
<mapping resource="request_sanctions.hbm.xml"/>
<mapping resource="request_standby_date.hbm.xml"/>
<mapping resource="rsp_request_product.hbm.xml"/>
</session-factory>
</hibernate-configuration>
程序运行无错误,但生成的hbm2schema.sql为空。 实际上,输出看起来像这样:
Οκτ17,2018 11:10:28Πorg.hibernate.Version logVersion INFO: HHH000412:Hibernate Core {4.3.11.Final} 2018年11月17日11:10:28 org.hibernate.cfg。环境信息:HHH000206: 找不到hibernate.propertiesΟκτ17,2018 11:10:28ΠΜ org.hibernate.cfg.Environment buildBytecodeProvider信息:HHH000021: 字节码提供者名称:javassistΟκτ17,2018 11:10:29ΠΜ org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 配置WARN:HHH000402:使用Hibernate内置连接池 (非用于生产!)Οκτ17,2018 11:10:29ΠΜ org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator信息:HHH000401:使用驱动程序[com.mysql.jdbc.Driver]在 网址 [jdbc:mysql:// localhost:3306 / my_rsp_request_creation_test?useSSL = false] Οκτ17,2018 11:10:29ΠΜ org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator信息:HHH000046:连接属性:{user = bla, password = ****}Οκτ17,2018 11:10:29ΠΜ org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator信息:HHH000006:自动提交模式:错误Οκτ17,2018 11:10:29ΠΜ org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 配置信息:HHH000115:休眠连接池大小:20(min = 1) Οκτ17,2018 11:10:30ΠΜorg.hibernate.dialect.Dialect信息: HHH000400:使用方言:org.hibernate.dialect.MySQL5InnoDBDialect 在以下位置创建输出sql文件:hbm2schema.sqlΟκτ17,2018 上午11:10:30 org.hibernate.tool.hbm2ddl.SchemaExport执行INFO: HHH000227:正在运行hbm2ddl模式导出Οκτ17,2018 11:10:30ΠΜ org.hibernate.tool.hbm2ddl.SchemaExport执行信息:HHH000230: 模式导出完成Οκτ17,2018 11:10:30ΠΜ org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl 停止INFO:HHH000030:清理连接池 [jdbc:mysql:// localhost:3306 / my_rsp_request_creation_test?useSSL = false] 完成...
事实上,根据 schemaExport.create(true,false)的javadoc,生成的sql应该甚至显示在屏幕上,因此不仅输出文件为空,而且没有导出也显示在屏幕上。对这个人有什么建议吗?
谢谢