Spring Boot无法在mysql数据库中生成表

时间:2019-05-27 16:20:29

标签: java mysql spring

我正在尝试通过反抗JpaEntities更新数据库架构,但是Spring Boot无法生成数据库表。 日志文件:

application.properties文件:

spring.datasource.url= jdbc:mysql://localhost:3306/banque_db
spring.datasource.username= root
spring.datasource.password=
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto= update
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

客户端实体类:

 @Entity(name="client")
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long code;
private String name;
private String email;
@OneToMany(mappedBy = "compte")
private Collection<Compte> comptes;
// + getters and setters

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“>     4.0.0              org.springframework.boot         弹簧启动启动器父母         2.1.0。发布                    com.Tuto

<artifactId>MaBanque</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MaBanque</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>       
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>       
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

3 个答案:

答案 0 :(得分:1)

实际上,问题在于实体类不在正确的包中。   包含主类和org.entities的org.example 由于某种原因,spring boot不会生成表,因此当我将实体包移动到org.example.entities并运行app时,所有表均已成功生成。 谢谢你们给我的帮助:)

答案 1 :(得分:0)

您必须将spring.jpa.hibernate.ddl-auto = create设置为数据库创建或create-drop

有关更多详细信息,请参见docs

答案 2 :(得分:0)

第一个问题: application.properties文件:

  1. 如果您设置属性spring.jpa.hibernate.ddl-auto = update,它将不会为您创建架构。相反,您将知道更新您当前没有的现有数据库。
  2. spring.jpa.hibernate.ddl-auto默认值为create-drop,但您要覆盖它。
  3. 您的application.properties 必须
  

src / main / resources

文件夹。

==>您应修复:

  

spring.jpa.hibernate.ddl-auto =创建-删除

第二个问题:实体类。您应该在属性顶部添加 @Column ,如下所示:

`@Entity(name="client")
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long code;
@Column
private String name;
@Column
private String email;
@OneToMany(mappedBy = "compte")
private Collection<Compte> comptes;
// + getters and setters`