Java Hibernate 不会自动创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50774085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:48:23  来源:igfitidea点击:

Hibernate doesn't create tables automatically

javamysqlspringhibernate

提问by Egorika Belarus

I have Maven project with Hibernate/Spring/MySQL. I have basic entity and I want Hibernate to create tables automatically, but Hibernate doesn't create any tables. No exceptions are thrown too, so I have no idea what is wrong here.

我有一个带有 Hibernate/Spring/MySQL 的 Maven 项目。我有基本实体,我希望 Hibernate 自动创建表,但 Hibernate 不创建任何表。也没有抛出异常,所以我不知道这里出了什么问题。

application.properties:

应用程序属性:

# ===============================
# = DATA SOURCE
# ===============================
spring.datasource.url = jdbc:mysql://localhost:3306/task
spring.datasource.username = root
spring.datasource.password = 12345678
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ===============================
# = JPA / HIBERNATE
# ===============================
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.properties.hibernate.id.new_generator_mappings = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.datasource.tomcat.test-while-idle=true
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.time-between-eviction-runs-millis=3600000
spring.datasource.tomcat.validation-query=SELECT 1

User.java:

用户.java:

package com.example.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import lombok.Data;

@Entity
@Table(name = "user")
@Data
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @Column(name = "telegramId")
    private Integer telegramId;

    @Column(name = "firstName")
    private String firstName;

    @Column(name = "lastName")
    private String lastName;

    @OneToMany(
            mappedBy = "user",
            cascade = CascadeType.ALL,
            orphanRemoval = true
            )
    private List<Message> msg = new ArrayList<>();
}

In User.java I also use lombok. Any suggestions? Thank you.

在 User.java 我也使用 lombok。有什么建议?谢谢你。

采纳答案by Egorika Belarus

I found a solution. The problem was that Application.java was in package com.example.BotApp., now it's in com.example.. I don't know, but somehow it helped.

我找到了解决方案。问题是 Application.java 在 package 中com.example.BotApp.,现在在com.example.. 我不知道,但不知何故它有所帮助。

回答by paardhu

you are missing this property in your properties file

您的属性文件中缺少此属性

hibernate.hbm2ddl.auto="update"

hibernate.hbm2ddl.auto="更新"

spring.jpa.properties.hibernate.hbm2ddl.auto=update

hibernate.hbm2ddl.auto is automatically validates and exports DDL to schema when the sessionFactory is created.

hibernate.hbm2ddl.auto 在 sessionFactory 创建时自动验证并将 DDL 导出到模式。

By default, It is not doing any creation or modification automatically on db. If user sets values to update or create or validate or create-dropthen it is doing DDL schema changes automatically according to given value.

默认情况下,它不会自动对 db 进行任何创建或修改。如果用户将值设置为更新或创建或验证或创建删除,则它会根据给定值自动执行 DDL 模式更改。

回答by Urosh T.

If you are using spring-data(which I hope you are), remove the @Tableannotation, Spring will automatically create a table with the name userjust from the @Entity. The minimum config that should go inside your application.propertesis this:

如果您正在使用spring-data(我希望您是这样),请删除@Table注释,Spring 将自动创建一个名为user的表,仅从@Entity. 应该进入你的最低配置application.propertes是这样的:

# Hibernate
spring.datasource.platform=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update

# Mysql
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://yourdburl:3306/test?createDatabaseIfNotExist=true // creates a schema if doesn't exist
spring.datasource.username=root
spring.datasource.password=12345678

spring.jpa.hibernate.ddl-auto=updatewill update your DB table (if necessary) every time your app connects to it. It should also create it if there is none. Other values are createand create-dropwhich are pretty much self-explanatory

spring.jpa.hibernate.ddl-auto=update每次您的应用程序连接到它时都会更新您的数据库表(如有必要)。如果没有,它也应该创建它。其他值是 createcreate-drop几乎不言自明

回答by Diego Santa Cruz Mendezú

test changing the name of the table, this worked for me.

测试更改表的名称,这对我有用。

回答by Paulo

in my case, I believe that, because I was using hibernate 5, hibernate 5 should solve some format problems for the columns table automatically, some tables were not created, but others were. I DIFF the files to analyze the differences...and the problem was... I was using a property and that impeded the creating of the table...

就我而言,我相信,因为我使用的是 hibernate 5,所以 hibernate 5 应该自动解决列表的一些格式问题,有些表没有创建,但其他表是。我 DIFF 文件以分析差异......问题是......我正在使用一个属性并且阻碍了表格的创建......

@Column(nullable = false, columnDefinition = "DECIMAL(6,6) DEFAULT 0.00")
private Double longitude;

I've deleted columnDefinition = "DECIMAL(6,6) DEFAULT 0.00", and the tables were created normally.

我已经删除了columnDefinition = "DECIMAL(6,6) DEFAULT 0.00",并且表已正常创建。