Java 使用Hibernate创建架构(表)

Java 使用Hibernate创建架构(表),java,Java,我有一个jar文件,其中有一组注释为@Entity的模型。现在,我想在数据库中创建与jar文件中的模型对应的表(通过在本地存储库中安装jar,我可以像在pom文件中使用依赖项一样使用jar文件)。它应该是通用的(例如MySQL、Postgresql、oracle、ms sql server、h2……)。我是冬眠新手。谁能建议我怎么做。在org.hibernate.tool.hbm2ddl.SchemaExport的帮助下,我们可以完成这项任务。 例如: Configuration configu

我有一个jar文件,其中有一组注释为@Entity的模型。现在,我想在数据库中创建与jar文件中的模型对应的表(通过在本地存储库中安装jar,我可以像在pom文件中使用依赖项一样使用jar文件)。它应该是通用的(例如MySQL、Postgresql、oracle、ms sql server、h2……)。我是冬眠新手。谁能建议我怎么做。

在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. 
从你可以使用

hibernate.hbm2ddl.auto

当 SessionFactory已创建。使用createdrop,数据库模式将 当SessionFactory显式关闭时,将删除

验证|更新|创建|创建删除

1) 验证:验证架构,不更改数据库

2) 更新:更新模式

3) 创建:创建模式,销毁以前的数据

4) createdrop:在会话结束时删除架构


希望这有助于

遵循本教程。请更具体地回答您的问题。本教程不解决上述问题。它也很有用。但我不需要放弃模式。谢谢