user2354035
user2354035

Reputation:

How to create Objects in database if does not exists with JPA?

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>

Upvotes: 3

Views: 13702

Answers (2)

David SN
David SN

Reputation: 3519

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

Upvotes: 11

user2354035
user2354035

Reputation:

I found the solution, simply add the following property:

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

Upvotes: 2

Related Questions