java 如何在jpa持久性xml文件中自动创建表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32873844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:54:02  来源:igfitidea点击:

how to automatic create table in jpa persistence xml file?

javahibernateexceptionjpa

提问by Ummer Iqbal

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.

我正在使用 Eclipse IDE。我也尝试以下两个。

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

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

here my persistence file:

这是我的持久性文件:

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

回答by Neil Stockton

Dont use Hibernate specific options. JPA 2.1 provides

不要使用 Hibernate 特定的选项。JPA 2.1 提供

javax.persistence.schema-generation.database.action

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

可以设置为“ create”、“ drop”、“ drop-and-create”、“ none”。这样你就可以让你的代码 JPA 实现独立

回答by Peter L

Check your entity. Did you miss @Table annotation? The exception clearly says that the table is missing 'hibernate.employee':

检查您的实体。您是否错过了@Table 注释?异常清楚地表明该表丢失'hibernate.employee'

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate.employee' doesn't exist at ...

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate.employee' doesn't exist at ...

If you defined a naming strategy that prepends all tables with hibernate., then make sure that the tables are created in MySql.

如果您定义了一个命名策略,该策略将所有表添加到休眠状态。,然后确保表是在 MySql 中创建的。