Hibernate:根据实体类自动创建/更新数据库表。

时间:2022-09-11 12:40:43

I have the following entity class (in Groovy):

我有以下实体类(在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

}

and my persistence.xml:

和我的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>

and the script:

和脚本:

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()

the database Icarus exists, but currently has no tables. I would like Hibernate to automatically create and/or update the tables based on the entity classes. How would I accomplish this?

数据库Icarus存在,但目前没有表。我希望Hibernate能够根据实体类自动创建和/或更新表。我该怎么做呢?

7 个解决方案

#1


91  

I don't know if leaving hibernate off the front makes a difference.

我不知道把休眠放在前面会不会有什么不同。

The reference suggests it should be hibernate.hbm2ddl.auto

参考文献建议应该是hibernate.hbm2ddl.auto

A value of create will create your tables at sessionFactory creation, and leave them intact.

创建的值将在sessionFactory创建时创建表,并保持它们的完整性。

A value of create-drop will create your tables, and then drop them when you close the sessionFactory.

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

Perhaps you should set the javax.persistence.Table annotation explicitly?

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

Hope this helps.

希望这个有帮助。

#2


72  

You might try changing this line in your persistence.xml from

您可以尝试在您的持久性中更改这一行。xml从

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

to:

:

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

This is supposed to maintain the schema to follow any changes you make to the Model each time you run the app.

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

Got this from JavaRanch

得到这个从JavaRanch

#3


8  

Sometimes depending on how the configuration is set, the long form and the short form of the property tag can also make the difference.

有时,取决于配置的设置方式,属性标记的长形式和短形式也会造成差异。

e.g. if you have it like:

例如:如果你有:

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

try changing it to:

试着改变它:

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

#4


1  

In my case table was not created for the first time without last property listed below:

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

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

used Wildfly's in-memory H2 database

使用Wildfly的内存中的H2数据库

#5


0  

There is one very important detail, than can possibly stop your hibernate from generating tables (assuming You already have set the hibernate.hbm2ddl.auto). You will also need the @Table annotation!

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

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

It has already helped in my case at least 3 times - still cannot remember it ;)

它已经帮助我至少3次了——仍然记不住它;

PS. Read the hibernate docs - in most cases You will probably not want to set hibernate.hbm2ddl.auto to create-drop, because it deletes Your tables after stopping the app.

读hibernate文档——在大多数情况下,您可能不希望设置hibernate.hbm2ddl。自动创建-删除,因为它在停止应用程序后删除您的表。

#6


0  

In applicationContext.xml file:

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

#7


-4  

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时,自动验证或导出模式DDL到数据库。使用create-drop,当SessionFactory显式关闭时,数据库模式将被删除。例如,验证|更新|创建|创建-drop

Please check the link for more details.

详情请查看链接。

#1


91  

I don't know if leaving hibernate off the front makes a difference.

我不知道把休眠放在前面会不会有什么不同。

The reference suggests it should be hibernate.hbm2ddl.auto

参考文献建议应该是hibernate.hbm2ddl.auto

A value of create will create your tables at sessionFactory creation, and leave them intact.

创建的值将在sessionFactory创建时创建表,并保持它们的完整性。

A value of create-drop will create your tables, and then drop them when you close the sessionFactory.

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

Perhaps you should set the javax.persistence.Table annotation explicitly?

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

Hope this helps.

希望这个有帮助。

#2


72  

You might try changing this line in your persistence.xml from

您可以尝试在您的持久性中更改这一行。xml从

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

to:

:

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

This is supposed to maintain the schema to follow any changes you make to the Model each time you run the app.

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

Got this from JavaRanch

得到这个从JavaRanch

#3


8  

Sometimes depending on how the configuration is set, the long form and the short form of the property tag can also make the difference.

有时,取决于配置的设置方式,属性标记的长形式和短形式也会造成差异。

e.g. if you have it like:

例如:如果你有:

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

try changing it to:

试着改变它:

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

#4


1  

In my case table was not created for the first time without last property listed below:

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

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

used Wildfly's in-memory H2 database

使用Wildfly的内存中的H2数据库

#5


0  

There is one very important detail, than can possibly stop your hibernate from generating tables (assuming You already have set the hibernate.hbm2ddl.auto). You will also need the @Table annotation!

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

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

It has already helped in my case at least 3 times - still cannot remember it ;)

它已经帮助我至少3次了——仍然记不住它;

PS. Read the hibernate docs - in most cases You will probably not want to set hibernate.hbm2ddl.auto to create-drop, because it deletes Your tables after stopping the app.

读hibernate文档——在大多数情况下,您可能不希望设置hibernate.hbm2ddl。自动创建-删除,因为它在停止应用程序后删除您的表。

#6


0  

In applicationContext.xml file:

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

#7


-4  

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时,自动验证或导出模式DDL到数据库。使用create-drop,当SessionFactory显式关闭时,数据库模式将被删除。例如,验证|更新|创建|创建-drop

Please check the link for more details.

详情请查看链接。