我正在开发一个使用 MySQL、Hibernate、Java、GWT 的项目
我正在创建一个实体类,其中我根据 mysql 中的字段创建每个字段 就像 MySQL 中有一个 UserInfo 表,其中包含字段、id、name、password 和许多其他字段
现在当我在 java/hibernate 中制作实体类时 我必须用手创建每个字段,即 id、名称、密码等
我只是想知道是否有一种方法可以让我的实体类自动检测数据库表中的所有字段,并在 java 本身中生成所有字段并将它们与数据库字段链接。
谢谢
最佳答案
hibernate 有一个名为“hibernate.hbm2ddl.auto”的属性。
hibernate.hbm2ddl.auto
Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
e.g. validate | update | create | create-drop
您可以在开始创建 sessionfactory 时设置适当的值。然后您将看到在您的数据存储中创建了表,当然,表创建是基于您的实体类的。
如果你想从数据库表生成java类(实体)。您可能想检查一下 hibernate 逆向工程:
http://docs.jboss.org/tools/3.3.0.M5/en/hibernatetools/html/reverseengineering.html
关于java - Hibernate:如何自动创建具有所有数据库字段的实体类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10108896/