Domanda

Hibernate versione 3.6

Sto cercando di eseguire un'esportazione parziale dello schema utilizzando hbm2dll SchemaExport.Pertanto accedo alla configurazione di ibernazione corrente, recupero il PersistentClass per il quale deve essere generata la tabella e creo una nuova configurazione come segue:

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

Come è visibile dal log (sotto), l'esportazione dello schema sembra essere completata ma sembra che non stia raccogliendo la mappatura aggiunta a livello di programmazione poiché la tabella non è stata creata.

=========== 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]
È stato utile?

Soluzione

OK, ecco cosa ho aggiunto per farlo funzionare:

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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top