【问题标题】:Why hibernate doesn't create tables automatically even with hibernate.hbm2ddl.auto" value="create"?为什么即使使用 hibernate.hbm2ddl.auto" value="create",hibernate 也不会自动创建表?
【发布时间】:2022-01-14 20:25:39
【问题描述】:

如果不存在,我试图强制休眠创建一个学生表,但没有成功,休眠库应该能够做到这一点,但我收到一个错误,说在数据库,不知道是什么问题:

详情:

学生班

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

主类

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>

注意:

-我添加了以下属性&lt;property name="hibernate.hbm2ddl.auto" value="create"/&gt; 来强制休眠创建表,但我仍然收到错误消息说数据库中没有这样的表。

-我使用 MySQL 作为数据库 `

【问题讨论】:

    标签: java hibernate jpa jpa-2.0 hibernate-mapping


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-09-08
      • 2019-03-20
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2011-04-05
      • 1970-01-01
      • 2018-07-06
      相关资源
      最近更新 更多