在hibernate中配置完成后,利用SchemaExport导出是报错。
源码如下import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;public class exportDB { public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}}
log4j报错如下:java.sql.SQLWarning: Unknown table 'book'
at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:770)
at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:698)
at com.mysql.jdbc.StatementImpl.getWarnings(StatementImpl.java:2194)
at com.mchange.v2.c3p0.impl.NewProxyStatement.getWarnings(NewProxyStatement.java:350)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:309)
at org.hibernate.tool.hbm2ddl.SchemaExport.drop(SchemaExport.java:284)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:186)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:133)
at com.dawei.vo.exportDB.main(exportDB.java:14)最后一行错误信息指的是export.create(true, true);.
如果把Log4j的配置文件删掉,无报错信息,但表依然建立不上。
在hibernate的配置文件中加入hbm2dllauto属性,设为update。则可以建表成功。
这是怎么回事啊?
源码如下import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;public class exportDB { public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}}
log4j报错如下:java.sql.SQLWarning: Unknown table 'book'
at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:770)
at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:698)
at com.mysql.jdbc.StatementImpl.getWarnings(StatementImpl.java:2194)
at com.mchange.v2.c3p0.impl.NewProxyStatement.getWarnings(NewProxyStatement.java:350)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:309)
at org.hibernate.tool.hbm2ddl.SchemaExport.drop(SchemaExport.java:284)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:186)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:133)
at com.dawei.vo.exportDB.main(exportDB.java:14)最后一行错误信息指的是export.create(true, true);.
如果把Log4j的配置文件删掉,无报错信息,但表依然建立不上。
在hibernate的配置文件中加入hbm2dllauto属性,设为update。则可以建表成功。
这是怎么回事啊?
你的hibernate.cfg.xml中没有配置book吧
<mapping resource="com/hibernate/Book.hbm.xml" />关于hbm2dllauto属性,
hibernate.hbm2ddl.auto value=“create”
看一下hibernate的官方解释:
hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop
有意思吧~!
竟然可以自动创建|更新|验证数据库表结构。
呵呵 不过如果不是此方面的需求建议set value="none".
下面是这几个属性的解释:
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构1.请慎重使用此参数,没必要就不要随便用。如果要用尽量使用update
2.如果发现数据库表丢失,请检查hibernate.hbm2ddl.auto的配置
还有为什么要避免使用它呢,我觉得这个update值实在是好用啊……比Schema强大。
在配置了hbm2dll参数后,用SchemaExport也不再报错了,诧异ing。