为什么即使使用 hibernate.hbm2ddl.auto" value="create",hibernate 也不会自动创建表?

Why hibernate doesn't create tables automatically even with hibernate.hbm2ddl.auto" value="create"?

我试图强制 hibernate 创建一个学生 table 如果它不存在,但没有成功,Hibernate 库应该能够做到这一点,但我收到一条错误消息数据库中没有这样的table,不确定是什么问题:

详情:

学生class

import javax.persistence.Entity;
import javax.persistence.Id;


@Entity
public class Student {
    @Id
    int id;

    int phoneNumber;

    public Student() {
    }

    public Student(int id, int phoneNumber) {
        this.id = id;
        this.phoneNumber = phoneNumber;
    }

    public int getId() {
        return id;
    }

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

    public int getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(int phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

主要class

public class Main {
    public static void main(String[] args) {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ss");
        EntityManager entityManager = entityManagerFactory.createEntityManager();


        Student student = new Student(5,55);


        entityManager.getTransaction().begin();
        entityManager.persist(student);
        entityManager.getTransaction().commit();
    }
}

Persistance.xml 文件

   <?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="ss" transaction-type="RESOURCE_LOCAL">

        <class>com.youssef.Student</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sotamag" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            
        </properties>
    </persistence-unit>
</persistence>

注:

-我添加了以下 属性 <property name="hibernate.hbm2ddl.auto" value="create"/> 以强制休眠创建 table,但我仍然收到错误消息,指出没有这样的 table 在数据库中。

-我正在使用 MySQL 作为数据库 `

如果您使用的是 MySQL5 或更高版本,您可以尝试将方言更改为:

<property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</property>