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

夙愿已清 提交于 2019-12-24 02:39:10

问题


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


来源:https://stackoverflow.com/questions/17712220/how-to-create-objects-in-database-if-does-not-exists-with-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!