2015-04-20 17 views
5

Voglio che la mia Spring Application generi automaticamente lo schema e le tabelle DB ... Ho letto alcuni Q & A su questo argomento e ho impostato il mio URL DB su :H2 DB in Spring Boot Hibernate non genera Db Schema

H2DataSource.setUrl ("jdbc: h2: mem: tmp.db; INIT = CREATE SCHEMA SE NON ESISTE GPSTracker");

e ho annotato le mie entità come:

@Entity
@Table (name = "tblGps", lo schema = "GPSTracker")

ma lo schema db non è ancora stato creato.

Ecco l'output del registro. Hibernate sta cercando di creare le tabelle, ma non riesce a trovare lo schema!

Cosa sto sbagliando? Eventuali suggerimenti?

registro di output

2015-04-20 22:29:38.211 INFO 7056 --- [ost-startStop-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ 
    name: default 
    ...] 
2015-04-20 22:29:38.356 INFO 7056 --- [ost-startStop-1] org.hibernate.Version     : HHH000412: Hibernate Core {4.3.8.Final} 
2015-04-20 22:29:38.360 INFO 7056 --- [ost-startStop-1] org.hibernate.cfg.Environment   : HHH000206: hibernate.properties not found 
2015-04-20 22:29:38.362 INFO 7056 --- [ost-startStop-1] org.hibernate.cfg.Environment   : HHH000021: Bytecode provider name : javassist 
2015-04-20 22:29:38.745 INFO 7056 --- [ost-startStop-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {4.0.5.Final} 
2015-04-20 22:29:38.899 INFO 7056 --- [ost-startStop-1] org.hibernate.dialect.Dialect   : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect 
2015-04-20 22:29:39.202 INFO 7056 --- [ost-startStop-1] o.h.h.i.ast.ASTQueryTranslatorFactory : HHH000397: Using ASTQueryTranslatorFactory 
2015-04-20 22:29:39.795 INFO 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export 
2015-04-20 22:29:39.801 ERROR 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: drop table GPSTRACKER.tbl_gps if exists 
2015-04-20 22:29:39.801 ERROR 7056 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : Schema "GPSTRACKER" nicht gefunden 
Schema "GPSTRACKER" not found; SQL statement: 
drop table GPSTRACKER.tbl_gps if exists [90079-185] 

EntityManagerFactory

@Bean 
    public EntityManagerFactory entityManagerFactory() { 

     HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
     vendorAdapter.setGenerateDdl(true); 
     vendorAdapter.setShowSql(true); 
     vendorAdapter.setDatabasePlatform(MyAppSettings.getDbPlattform()); 

     HibernateJpaDialect jpd = new HibernateJpaDialect(); 
     LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 

     factory.setJpaDialect(jpd); 
     factory.setJpaVendorAdapter(vendorAdapter); 
     factory.setPackagesToScan(MyAppSettings.packagesToScan); 
     factory.setDataSource(MyDataSource()); 

     return factory.getObject(); 
    } 

DataSource

DriverManagerDataSource H2DataSource = new DriverManagerDataSource(); 
       H2DataSource.setDriverClassName("org.h2.Driver"); 
       H2DataSource.setUrl("jdbc:h2:mem:tmp.db;INIT=CREATE SCHEMA IF NOT EXISTS GPSTRACKER"); 

       H2DataSource.setUsername("sa"); 
       H2DataSource.setPassword(""); 

@pvgoddijn i c non ricordo esattamente, e non riesco a trovare il codice in questo momento. Ma suppongo che fosse necessario restituire LocalEntityManagerFactory invece di EntityManagerFactory ... o sth così. in bocca al lupo! forse riesco a trovare il codice nei prossimi giorni ...

risposta

2

Durante la creazione dell'origine dati, è necessario impostare la proprietà hbm2ddl.auto in modo che il database possa essere creato/aggiornato all'avvio.

Properties properties = new Properties(); 
    properties.put("hibernate.hbm2ddl.auto", "update"); 
    H2DataSource.setConnectionProperties(properties); 

Si potrebbe anche impostare la proprietà nel file hibernate.cfg.xml

Altri valori possibili sono: validate | aggiornamento | creare | creare-drop

Per ulteriori informazioni su questa e altre proprietà sono disponibili all'indirizzo: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional

+0

grazie, ma per la copia-pasters tra di noi: properties.put ("hibernate.hbm2ddl.auto", "update") ; – codesmith

Problemi correlati