如何在jpa持久性xml文件中自动创建表? [英] how to automatic create table in jpa persistence xml file?

查看:275
本文介绍了如何在jpa持久性xml文件中自动创建表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eclipse IDE.我也尝试下面的两个操作,但是失败了..当我在mysql数据库中手动创建表时,我的完整程序运行良好...我想针对实体类自动创建表.

I am using eclipse IDE.I also try following two .but failed..when i manually create table in my mysql database then my complete program run fine... I want create table automatic with respect to entity class.

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

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

这是我的持久性文件:

<?xml version="1.0" encoding="UTF-8"?>
  <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JpaTest2" transaction-type="RESOURCE_LOCAL">

     <class>com.jpa.Employee</class>

    <properties>

        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate"/>
        <property name="javax.persistence.jdbc.user" value="umar"/>
        <property name="javax.persistence.jdbc.password" value="umar"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>


    </properties>
</persistence-unit>

推荐答案

不要使用特定于Hibernate的选项. JPA 2.1提供了

Dont use Hibernate specific options. JPA 2.1 provides

javax.persistence.schema-generation.database.action

可以设置为"创建","放置","放置并创建","无"的

". 这样一来,您就可以保持代码JPA实现的独立性

that can be set to "create", "drop", "drop-and-create", "none". That way you keep your code JPA implementation independent

这篇关于如何在jpa持久性xml文件中自动创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆