2012-12-23 127 views
2

我們目前使用Hibernate 3,我們使用Hibernate Tools爲數據庫模式生成SQL腳本。使用Hibernate生成SQL數據庫創建腳本4

我們用下面的Ant任務

<hibernatetool destdir="${target}"> 
    <jpaconfiguration persistenceunit="@{persistenceUnit}" propertyfile="@{propertyfile}"/> 
    <classpath refid="@{classpathid}"/> 
    <!-- the file name is relative to $destdir --> 
    <hbm2ddl outputfilename="@{output}" format="true" export="false" drop="false"/> 
</hibernatetool> 

我們想切換到休眠4:我們如何能夠實現無休眠的工具類似的東西?

回答

13

您可以直接使用SchemaExport類來生成DDL腳本:

Configuration config = new Configuration(); 

Properties properties = new Properties(); 

properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect"); 
properties.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/Test"); 
properties.put("hibernate.connection.username", "username"); 
properties.put("hibernate.connection.password", "password"); 
properties.put("hibernate.connection.driver_class", "org.postgresql.Driver"); 
properties.put("hibernate.show_sql", "true"); 
config.setProperties(properties); 

config.addAnnotatedClass(MyMappedPojo1.class); 
config.addAnnotatedClass(MyMappedPojo2.class); 
.................. 

SchemaExport schemaExport = new SchemaExport(config); 
schemaExport.setDelimiter(";"); 

/**Just dump the schema SQLs to the console , but not execute them ***/ 
schemaExport.create(true, false); 
+0

+1很好的例子 –

相關問題