【问题标题】:Hibernate: Automatically creating/updating the db tables based on entity classesHibernate:根据实体类自动创建/更新数据库表
【发布时间】:2017-07-31 04:43:58
【问题描述】:

我有以下实体类(在 Groovy 中):

import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType

@Entity
public class ServerNode {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  Long id

  String firstName
  String lastName

}

还有我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="NewPersistenceUnit">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Icarus"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="create"/>
        </properties>
        <class>net.interaxia.icarus.data.models.ServerNode</class>
    </persistence-unit>
</persistence>

和脚本:

import javax.persistence.EntityManager
import javax.persistence.EntityManagerFactory
import javax.persistence.Persistence
import net.interaxia.icarus.data.models.ServerNode

def factory = Persistence.createEntityManagerFactory("NewPersistenceUnit")
def manager = factory.createEntityManager()

manager.getTransaction().begin()

manager.persist new ServerNode(firstName: "Test", lastName: "Server")

manager.getTransaction().commit()

数据库Icarus 存在,但目前没有表。我希望 Hibernate 基于实体类自动创建和/或更新表。我将如何做到这一点?

【问题讨论】:

    标签: java mysql hibernate jpa groovy


    【解决方案1】:

    您可以尝试在您的 persistence.xml 中更改这一行

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

    到:

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

    这应该维护架构以遵循您每次运行应用程序时对模型所做的任何更改。

    JavaRanch得到这个

    【讨论】:

      【解决方案2】:

      我不知道将hibernate 留在前面是否会有所不同。

      reference 建议它应该是hibernate.hbm2ddl.auto

      create 的值将在 sessionFactory 创建时创建您的表,并保持它们不变。

      create-drop 的值将创建您的表,然后在您关闭 sessionFactory 时删除它们。

      也许您应该明确设置javax.persistence.Table 注释?

      希望这会有所帮助。

      【讨论】:

      • 这是 hbm2dll.auto 开头缺少的“休眠”。谢谢!
      • 我刚刚删除了该行,它不会丢弃表格。希望这会有所帮助!
      • 只有在表不存在的情况下才能让休眠模式创建表?
      【解决方案3】:

      有时取决于配置的设置方式,属性标签的长格式和短格式也会有所不同。

      例如如果你喜欢:

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

      尝试将其更改为:

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

      【讨论】:

        【解决方案4】:

        有一个非常重要的细节,可能会阻止您的休眠生成表(假设您已经设置了hibernate.hbm2ddl.auto)。您还需要@Table 注释!

        @Entity
        @Table(name = "test_entity")
            public class TestEntity {
        }
        

        在我的情况下它已经帮助了至少 3 次 - 仍然不记得它;)

        PS。阅读 hibernate 文档 - 在大多数情况下,您可能不想将 hibernate.hbm2ddl.auto 设置为 create-drop,因为它会在停止应用程序后删除您的表。

        【讨论】:

          【解决方案5】:

          在 applicationContext.xml 文件中:

          <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <!-- This makes /META-INF/persistence.xml is no longer necessary -->
                <property name="packagesToScan" value="com.howtodoinjava.demo.model" />
                <!-- JpaVendorAdapter implementation for Hibernate EntityManager.
                     Exposes Hibernate's persistence provider and EntityManager extension interface -->
                <property name="jpaVendorAdapter">
                   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
                </property>
                <property name="jpaProperties">
                   <props>
                      <prop key="hibernate.hbm2ddl.auto">update</prop>
                      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                   </props>
                </property>
             </bean>
          

          【讨论】:

            【解决方案6】:

            在我的情况下,如果没有下面列出的最后一个属性,则不是第一次创建表:

            <properties>
                <property name="hibernate.archive.autodetection" value="class"/>
                <property name="hibernate.show_sql" value="true"/>
                <property name="hibernate.format_sql" value="true"/>
                <property name="hbm2ddl.auto" value="create-drop"/>
                <!-- without below table was not created -->
                <property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
            </properties>
            

            使用 Wildfly 的内存 H2 数据库

            【讨论】:

              【解决方案7】:

              为了支持@thorinkor 的答案,我将扩展我的答案,不仅对实体使用@Table (name = "table_name") 注释,而且实体类的每个子变量都应该使用@Column(name = "col_name ”)。这样可以随时随地无缝更新表格。

              对于那些正在寻找基于 Java 类的休眠配置的人,该规则也适用于基于 Java 的配置(NewHibernateUtil)。希望对其他人有所帮助。

              【讨论】:

                【解决方案8】:
                1. 自动创建表,与注释无关
                2. 为此,我们需要更改“hibernate.cfg.xml”。
                 <property name="hbm2ddl.auto">update</property>  
                

                【讨论】:

                • 该解决方案已在此问题的其他答案中提出。
                猜你喜欢
                • 2010-09-23
                • 2014-01-30
                • 2011-01-22
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-11-12
                • 2011-05-29
                相关资源
                最近更新 更多