Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate:基于实体类自动创建/更新db表_Java_Mysql_Hibernate_Jpa_Groovy - Fatal编程技术网

Java Hibernate:基于实体类自动创建/更新db表

Java Hibernate:基于实体类自动创建/更新db表,java,mysql,hibernate,jpa,groovy,Java,Mysql,Hibernate,Jpa,Groovy,我有以下实体类(在Groovy中): 和my 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.h

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

和my 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>

数据库Icarus存在,但当前没有表。我希望Hibernate根据实体类自动创建和/或更新表。如何实现这一点?

您可以尝试将persistence.xml中的这一行从

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

致:


这是为了维护模式,以跟踪您每次运行应用程序时对模型所做的任何更改


这是从

那里得到的,我不知道把
hibernate
放在前面是否会有所不同

建议它应该是
hibernate.hbm2ddl.auto

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

create drop
将创建表,然后在关闭sessionFactory时删除它们

也许您应该显式地设置注释


希望这能有所帮助。

有时,根据配置的设置,属性标签的长格式和短格式也会有所不同

e、 g.如果您有类似于:

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

尝试将其更改为:

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

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

@Entity
@Table(name = "test_entity")
    public class TestEntity {
}
在我的情况下,它已经帮助了我至少3次-仍然记不起来;)


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

在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>

更新
org.hibernate.dialogue.mysql5dialogue

在我的案例中,如果没有下面列出的last属性,表不是第一次创建的:

<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数据库

为了支持@Thrinkor的答案,我将扩展我的答案,不仅对实体使用@Table(name=“Table_name”)注释,而且实体类的每个子变量都应该用@Column(name=“col_name”)注释。这将导致对表的无缝更新


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

这是hbm2dll.auto开头缺少的“hibernate”。谢谢我刚拆下那条线,它就不会掉桌子了。希望这有帮助!如何使hibernate仅在表不存在时创建表?
@Entity
@Table(name = "test_entity")
    public class TestEntity {
}
<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>
<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>