java - hibernate 中的部分架构导出

标签 java hibernate mapping hbm2ddl schemaexport

hibernate 版本3.6

我正在尝试使用 hbm2dll SchemaExport 进行部分架构导出。因此,我访问当前的 hibernate 配置,检索应为其生成表的 PersistentClass 并创建一个新配置,如下所示:

ClassMetadata classMetadata = sessionFactory.getClassMetadata(SchemaVersion.class);
String entityName = classMetadata.getEntityName();
PersistentClass persistentClass = origCfg.getClassMapping(entityName);
final Configuration cfg = new Configuration();
Properties properties = origCfg.getProperties();
cfg.setProperties(properties);
cfg.createMappings().addClass(persistentClass);
cfg.buildMappings();
Iterator<PersistentClass> mappings = cfg.getClassMappings();
System.out.println("=========== MAPPINGS ===================");
while (mappings.hasNext()) {
    PersistentClass pClass = mappings.next();
    System.out.println(pClass.getClassName());
    System.out.println(pClass.getTable().getName());
}
System.out.println("=========== END MAPPINGS ===============");
Session session = sessionFactory.openSession();
session.doWork(new Work() {
    public void execute(Connection connection) throws SQLException {
        new SchemaExport(cfg, connection).create(true, true);
    }
});
session.close();

从日志(如下)中可以看出,架构导出似乎已完成,但由于未创建表,因此似乎没有获取以编程方式添加的映射。

=========== MAPPINGS ===================
eu.codecamp.utils.lib.persistence.schema.SchemaVersion
fx_schema_version
=========== END MAPPINGS ===============
DEBUG: opened session at timestamp: 5387886841208832 - 2011-09-07 15:27:40,842 [org.hibernate.impl.SessionImpl]
DEBUG: opening JDBC connection - 2011-09-07 15:27:40,843 [org.hibernate.jdbc.ConnectionManager]
INFO: Using dialect: org.hibernate.dialect.MySQLInnoDBDialect - 2011-09-07 15:27:40,885 [org.hibernate.dialect.Dialect]
DEBUG: Processing hbm.xml files - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: Process annotated classes - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: processing fk mappings (*ToOne and JoinedSubclass) - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: processing extends queue - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: processing extends queue - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: processing collection mappings - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: processing native query and ResultSetMapping mappings - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: processing association property references - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: processing foreign key constraints - 2011-09-07 15:27:40,885 [org.hibernate.cfg.Configuration]
DEBUG: Setting dialect [org.hibernate.dialect.MySQLInnoDBDialect] - 2011-09-07 15:27:40,887 [org.hibernate.id.factory.DefaultIdentifierGeneratorFactory]
DEBUG: Processing hbm.xml files - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: Process annotated classes - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: processing fk mappings (*ToOne and JoinedSubclass) - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: processing extends queue - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: processing extends queue - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: processing collection mappings - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: processing native query and ResultSetMapping mappings - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: processing association property references - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: processing foreign key constraints - 2011-09-07 15:27:40,887 [org.hibernate.cfg.Configuration]
DEBUG: Setting dialect [org.hibernate.dialect.MySQLInnoDBDialect] - 2011-09-07 15:27:40,889 [org.hibernate.id.factory.DefaultIdentifierGeneratorFactory]
INFO: Running hbm2ddl schema export - 2011-09-07 15:27:40,892 [org.hibernate.tool.hbm2ddl.SchemaExport]
DEBUG: import file not found: /import.sql - 2011-09-07 15:27:40,893 [org.hibernate.tool.hbm2ddl.SchemaExport]
INFO: exporting generated schema to database - 2011-09-07 15:27:40,893 [org.hibernate.tool.hbm2ddl.SchemaExport]
INFO: schema export complete - 2011-09-07 15:27:40,893 [org.hibernate.tool.hbm2ddl.SchemaExport]
DEBUG: releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] - 2011-09-07 15:27:40,893 [org.hibernate.jdbc.ConnectionManager]
DEBUG: transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! - 2011-09-07 15:27:40,894 [org.hibernate.jdbc.ConnectionManager]

最佳答案

好的,这是我为其添加的内容:

Table origTable = persistentClass.getTable();
String schema = origTable.getSchema();
String catalog = origTable.getCatalog();
String name = origTable.getName();
mappings.addTable(schema, catalog, name, origTable.getSubselect(),
        origTable.isAbstract());
Table table = mappings.getTable(schema, catalog, name);
Iterator<Column> columnIter = origTable.getColumnIterator();
while (columnIter.hasNext()) {
    Column column = columnIter.next();
    table.addColumn(column);
}

关于java - hibernate 中的部分架构导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7335270/

相关文章:

java - 在 spring-boot 应用程序中使用 map 与 DTO

java - 如何更改事务隔离级别

java - JSOUP 检查元素是否为 img

java - 如何在服务层处理Spring JPA的版本

hibernate - Hibernate 标准中的日期限制

hibernate - p :datatable - update ManyToMany property results in LazyInitializationException

.net - 使用正则表达式将字符串映射到功能

java - (Java/Spring) 使用entityType.getJavaType() 访问类元素

mapping - 如何使用 MapStruct 1.2 有条件地映射属性?

插入后 NHibernate 仍会发出更新