gpt4 book ai didi

java - 如何从实体生成表

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:22 24 4
gpt4 key购买 nike

我目前正在使用 eclipse jpa 工具开发一个 seam 项目;是否可以从我的实体定义自动生成 sql 表?如果是这样,我该如何实现这一目标?

最佳答案

这取决于您使用的 JPA 实现。使用 Hibernate,您可以在 persistence.xmlhibernate.hbm2ddl.auto 属性中指定“create”或“update”:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<persistence-unit name="yourPersistenceUnit" transaction-type="JTA">
<description>Your Persistence Unit</description>
<jta-data-source>java:/DefaultDS</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.transaction.flush_before_completion" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>

hibernate.hbm2ddl.auto 属性的可能值为:

  • create:启动时创建数据库表和索引
  • create-drop:启动时创建数据库表和索引,关闭时删除
  • 更新:应用程序启动时,检查数据库架构并根据需要进行更新,添加缺少的表和列
  • 验证:应用程序启动时,检查数据库架构,如果缺少某些表或列,则会失败。

关于java - 如何从实体生成表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5669136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com