Hibernate:根據配置檔案自動生成表結構的2種方式
阿新 • • 發佈:2018-12-20
在hibernate中,我們可以利用框架的一些配置屬性和類來自動建立我們需要的表!
1)在配置檔案中加入
<property name="hbm2ddl.auto">true</property>
2)使用工具類SchemaExport
package com.User; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class Test { @org.junit.Test public void test() throws Exception{ Configuration configuration = new Configuration().configure("com/User/hibernate.cfg.xml"); SchemaExport schemaExport = new SchemaExport(configuration); schemaExport.create(true,true); } }