未找到休眠表

问题描述 投票:0回答:3

这是我的实体

@Entity
public class User {

    @Id
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

这是我的 hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <!-- Assume test is the database name -->
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/test
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">
            root
        </property>

        <!-- List of XML mapping files -->
        <mapping class="User"/>

    </session-factory>
</hibernate-configuration>

我有一个名为 test 的空数据库,其用户名和密码为“root”。但是当我尝试创建一个用户并将其保存到数据库时,我收到一个异常:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.user' doesn't exist

我做错了什么?

java hibernate
3个回答
3
投票

这是因为数据库中没有

test.user

您需要在启动应用程序服务器之前创建表

(或)

启用hibernate DDL生成为true,让hibernate在表不存在时创建该表。 (不要在生产或 QA 环境中这样做,因为这不是一个好的做法)。

将其添加到休眠属性列表中。

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

0
投票

在配置文件中添加以下属性。

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

谢谢


0
投票

如果您的表已分区,请确保您的

application.yml
中有以下属性:

spring:
  jpa:
    properties:
        hibernate:
            hbm2ddl:
                extra_physical_table_types: PARTITIONED TABLE
© www.soinside.com 2019 - 2024. All rights reserved.