Pregunta

Hibernar versión 3.6

Estoy intentando realizar una exportación de esquema parcial usando hbm2dll SchemaExport.Por lo tanto accedo a la configuración de hibernación actual, recupero el PersistentClass la tabla se generará y creará una nueva configuración de la siguiente manera:

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();

Como se puede ver en el registro (a continuación), la exportación del esquema parece completarse, pero parece que no recoge la asignación agregada mediante programación ya que la tabla no se crea.

=========== 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]
¿Fue útil?

Solución

Bien, esto es lo que agregué para que funcione:

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);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top