How to create Objects in database if does not exis

2019-07-21 13:56发布

问题:

I have a web application that I use JPA / Hibernate I want to know how to hibernate can generate the non-existent tables from entities. the persistence file is as follows:

What I can add as property ?

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

回答1:

You can add this property

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

The possible values for this property are:

  • validate: Only validates the schema
  • update: Update the schema
  • create: Create the schema, override previous data
  • create-drop: Create the schema and drop the schema at the end of the session


回答2:

I found the solution, simply add the following property:

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