【问题标题】:how to automatic create table in jpa persistence xml file?如何在 jpa 持久性 xml 文件中自动创建表?
【发布时间】:2015-09-30 19:08:33
【问题描述】:

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

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

【问题讨论】:

  • 您认为还有其他信息可以帮助人们试图了解这里发生了什么吗?还是这个?
  • markdown 代码正确
  • persistence.xml 中添加&lt;property name="hibernate.hbm2ddl.auto" value="create"/&gt; 后的期望是什么?

标签: java hibernate exception jpa


【解决方案1】:

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

javax.persistence.schema-generation.database.action

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

【讨论】:

  • 似乎对我有用。我同意这是使您的捆绑包更通用的首选解决方案
  • 如何防止它在每次重启时尝试创建同一个表?
  • @KennySteegmans 如果它已经创建了数据库,那么当设置为“create”时,下次重启时不会发生任何事情
【解决方案2】:

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

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

如果你定义了一个命名策略,在所有表前加上hibernate.,那么请确保这些表是在 MySql 中创建的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2014-03-28
    • 2012-03-11
    • 2018-04-26
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多