The constructor SchemaExport(Configuration) is deprecated

Hibernate 中new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);生成表未成功

  • 最近在学习Hibernate的基础知识,但是由于所看教程中使用的Hibernate版本较低(3.x),而自己所用的版本为5.x,导致上述采用Annotation自动建表失败,警告:

    The constructor SchemaExport(Configuration) is deprecated

  • 经查资料,这个写法在hibernate 5.x以后为deprecated,而且如果在hibernate 5.x中使用上述写法,将会报错。推荐写法:

// the .jar needed
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; //Note: here is not javax.imageio.spi.ServiceRegistry
import org.hibernate.tool.hbm2ddl.SchemaExport;

// SchemaExport
ServiceRegistry serviceRegistry = (ServiceRegistry) new StandardServiceRegistryBuilder().configure().build();  
MetadataImplementor metadataImplementor = (MetadataImplementor) new MetadataSources(serviceRegistry).buildMetadata();
SchemaExport export = new SchemaExport(serviceRegistry, metadataImplementor);
export.create(true, true);

Note:当采用SchemaExport运行成功,但却没生成表时,很可能是在实体类中未写@Entity!!


If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!

你可能感兴趣的:(Java,Hibernate)