我有一个 jar 文件,其中有一组注释为 @Entity 的模型。现在我想在数据库中创建与 jar 文件中的模型相对应的表(我可以通过在本地存储库上安装 jar 来使用 jar 文件作为 pom 文件中的依赖项)。它应该是通用的(例如 MySQL、Postgresql、oracle、ms sql server、h2...)。我是 hibernate 新手。任何人都可以建议我如何做到这一点。
最佳答案
在 org.hibernate.tool.hbm2ddl.SchemaExport 的帮助下我们可以完成这个任务。
示例:
Configuration configuration = new Configuration();
//we can set property here e.g configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");.....
configuration = configuration.configure();
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.create(false, true); //1st parameter indicated to show query on console and 2nd parameter is to create(update) table in database.
关于java - 使用 Hibernate 创建架构(表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147052/