【问题标题】:How to create Objects in database if does not exists with JPA?如果 JPA 不存在,如何在数据库中创建对象?
【发布时间】:2013-07-16 17:45:30
【问题描述】:

我有一个使用 JPA / Hibernate 的 Web 应用程序我想知道如何休眠可以从实体生成不存在的表。 持久化文件如下:

我可以添加什么作为属性?

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
        http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="BankingApp">
        <provider> org.hibernate.ejb.HibernatePersistence</provider>
        <properties>

            <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
            <property name="hibernate.connection.username" value="user"/>
            <property name="hibernate.connection.password" value="password"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>

        </properties>
    </persistence-unit>
</persistence>

【问题讨论】:

  • 我不确定我是否明白你的问题。如何从不存在的实体生成表?您希望它生成所有可能的表吗?
  • @JohnAment 不,我希望从实体生成表
  • 您是否在问如果没有类的实例退出,hibernate 如何生成表?或者如果不存在类定义(带有实体注释/映射)?
  • @user2264997 我想创建实体类和表将在构建后生成。即生成表格形式实体
  • 默认情况下,hibernate 可以在部署时执行此操作,hibernate.hbm2ddl.auto 带有值、创建、更新等。这将自动应用于连接。 hibernate 工具也支持将其输出到文件,请参阅docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/…

标签: java hibernate jpa jdbc persistence


【解决方案1】:

你可以添加这个属性

<property name="hibernate.hbm2ddl.auto" value="update"/> 

此属性的可能值为:

  • validate:仅验证架构
  • 更新:更新架构
  • create:创建架构,覆盖以前的数据
  • create-drop:创建架构并在会话结束时删除架构

【讨论】:

    【解决方案2】:

    我找到了解决方案,只需添加以下属性:

    <property name="hibernate.hbm2ddl.auto" value="create"/>
    

    【讨论】:

      猜你喜欢
      • 2020-12-27
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多