Hibernate中使用schemaExport生成表结构出错的解决方式

【异常提示代码】
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
我用的是hibernate5.0.11,但是这个版本中SchemaExport构造器已经不被支持使用了,如果是hibernate4.x版本用下面的代码实现是可以的:
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
如果是5.x版本版本,用下面的方式来完成:
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,Java三大框架)