Hibernate 不创建表并自动更改

Posted

技术标签:

【中文标题】Hibernate 不创建表并自动更改【英文标题】:Hibernate is not creating tables and altered automatically 【发布时间】:2020-11-16 00:04:27 【问题描述】:

尝试使用 hibernate 创建表,但它已创建并删除。

Console:

Hibernate: `create table category (catid integer not null auto_increment, catdesc varchar(255), cattitle varchar(255), primary key (catid)) type=MyISAM`
Hibernate: create table category_product (Category_catid integer not null, p_pid integer not null) type=MyISAM
Hibernate: create table product (pid integer not null auto_increment, pDesc varchar(255), pDiscount integer not null, pName varchar(255), pPhoto varchar(255), pPrice integer not null, pQuantity integer not null, c_catid integer, primary key (pid)) type=MyISAM
Hibernate: create table user (id integer not null auto_increment, address varchar(255), mail varchar(255), name varchar(255), pass varchar(255), ph_no integer not null, primary key (id)) type=MyISAM
Hibernate: alter table category_product drop index UK_52hqb12vl2fe0l0d78hk2d88h
Hibernate: alter table category_product add constraint UK_52hqb12vl2fe0l0d78hk2d88h unique (p_pid)
Hibernate: `alter table category_product add constraint FKsqii9ofndgvr0cscbvb5e4uds foreign key (p_pid) references product (pid)`
Hibernate: alter table category_product add constraint FK29xlj9k6tq2niv1udd6a1msmd foreign key (Category_catid) references category (catid)
Hibernate: alter table product add constraint FKsw4ctbskvoom8us7igpgu6fqt foreign key (c_catid) references category (catid)

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
    <session-factory>
            <property name="hibernate.dialect">org.hibernate.dialect.mysqlDialect</property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/shoppingcart?useSSL=false</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root123</property>
            <property name="show_sql">true</property>      
            <property name="hbm2ddl.auto">update</property>
            <mapping class="com.sourav.model.User"/>     
            <mapping class="com.sourav.model.Product"/>
            <mapping class="com.sourav.model.Category"/> 
    </session-factory>
</hibernate-configuration>

模型类:

import java.util.List;

import javax.persistence.*;

@Entity
@Table(name="category")
public class Category 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int catid;
    private String cattitle;
    private String catdesc;
    @OneToMany
    private List<Product> p;
    public int getCatid() 
        return catid;
    
    public void setCatid(int catid) 
        this.catid = catid;
    
    public String getCattitle() 
        return cattitle;
    
    public void setCattitle(String cattitle) 
        this.cattitle = cattitle;
    
    public String getCatdesc() 
        return catdesc;
    
    public void setCatdesc(String catdesc) 
        this.catdesc = catdesc;
    
    public List<Product> getP() 
        return p;
    
    public void setP(List<Product> p) 
        this.p = p;
    
    public Category(String cattitle, String catdesc, List<Product> p) 
        super();
        this.cattitle = cattitle;
        this.catdesc = catdesc;
        this.p = p;
    
    
    

Hibernate: 创建表类别 (catid integer not null auto_increment, catdesc varchar(255), cattitle varchar(255), primary key (catid)) type=MyISAM Hibernate:创建表 category_product (Category_catid integer not null, p_pid integer not null) type=MyISAM Hibernate:创建表产品(pid整数不为空auto_increment,pDesc varchar(255),pDiscount整数不为空,pName varchar(255),pPhoto varchar(255),pPrice整数不为空,pQuantity整数不为空,c_catid整数,主键(pid)) 类型=MyISAM Hibernate:创建表用户(id integer not null auto_increment,address varchar(255),mail varchar(255),name varchar(255),pass varchar(255),ph_no integer not null,primary key (id)) type=MyISAM 休眠:更改表类别_产品删除索引 UK_52hqb12vl2fe0l0d78hk2d88h 休眠:更改表 category_product 添加约束 UK_52hqb12vl2fe0l0d78hk2d88h 唯一(p_pid) 休眠:更改表 category_product 添加约束 FKsqii9ofndgvr0cscbvb5e4uds 外键 (p_pid) 引用产品 (pid) Hibernate:更改表 category_product 添加约束 FK29xlj9k6tq2niv1udd6a1msmd 外键(Category_catid)引用类别(catid) Hibernate:更改表产品添加约束 FKsw4ctbskvoom8us7igpgu6fqt 外键 (c_catid) 参考

【问题讨论】:

【参考方案1】:

我已经通过删除解决了这个问题

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

【讨论】:

以上是关于Hibernate 不创建表并自动更改的主要内容,如果未能解决你的问题,请参考以下文章

自动创建表并从 json 文件插入数据

在hibernate中导入import.sql不成功

hibernate自动创建表报表不存在

TypeORM - 如何在生产模式下创建新表并自动运行迁移?

Hibernate 在创建表之前尝试更改表

使用自动创建表时,Hibernate 不会自动为数据库创建序列