hibernate中使用schemaExport生成数据表报错解决方法

来源:互联网 发布:linux统计登陆用户数 编辑:程序博客网 时间:2024/04/26 18:29
【错误详情】
java.lang.UnsupportedOperationException: Attempt to use unsupported SchemaExport constructor accepting org.hibernate.cfg.Configuration; one of the forms accepting org.hibernate.boot.spi.MetadataImplementor should be used instead。

【解决方法】
1.很多教材是教你这么写的:
SchemaExport schemaExport = new SchemaExport(new Configuration().configure());schemaExport.create(true, true);

因为我们现在基本都是使用hibernate 5.x,所以当你这么写的时候,IDE会提醒你这种方法已经不推荐使用了。
2.解决方法:将以上代码改成以下代码:
ServiceRegistry serviceRegistry = (ServiceRegistry) new StandardServiceRegistryBuilder().configure().build();MetadataImplementor metadataImplementor = (MetadataImplementor) new MetadataSources(serviceRegistry).buildMetadata();SchemaExport export = new SchemaExport(serviceRegistry,metadataImplementor);export.create(true, true);

重新运行,(如果你的hibernate和数据库驱动的jar已经正确导入),应该是OK了。


0 0
原创粉丝点击