我有一个带有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:
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<>();
}
在User.java中,我也使用lombok。有什么建议吗?谢谢。
发布于 2018-06-10 19:13:18
我找到了一个解决方案。问题是Application.java曾经在com.example.BotApp.
包中,现在它在com.example.
中。我不知道,但不知道怎么回事。
发布于 2019-02-06 00:34:48
测试更改表的名称,这对我很有效。
发布于 2019-03-16 11:11:26
在我的例子中,我相信,因为我使用的是hibernate 5,hibernate 5应该会自动解决列表的一些格式问题,一些表没有被创建,但另一些表被创建了。我比较了一些文件来分析differences...and,问题是...我正在使用一个属性,这阻碍了表的创建...
@Column(nullable = false, columnDefinition = "DECIMAL(6,6) DEFAULT 0.00")
private Double longitude;
我已经删除了columnDefinition = "DECIMAL(6,6) DEFAULT 0.00"
,并且表是正常创建的。
https://stackoverflow.com/questions/50774085
复制相似问题