繁体   English   中英

spring 引导不创建所有表 - Spring 引导,MySQL,jpa

[英]spring boot not creating all tables - Spring Boot, MySQL, jpa

我已经映射了 4 个类,然后使用这个控制台答案启动:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.4.RELEASE)

2020-10-27 11:34:29.432  INFO 6612 --- [           main] b.c.n.i.IndividualProjectApplication     : Starting IndividualProjectApplication on Othon-NoteAMD with PID 6612 (D:\programacao\Qualiti\individualProject\target\classes started by othon in D:\programacao\Qualiti\individualProject)
2020-10-27 11:34:29.435  INFO 6612 --- [           main] b.c.n.i.IndividualProjectApplication     : No active profile set, falling back to default profiles: default
2020-10-27 11:34:30.197  INFO 6612 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-10-27 11:34:30.232  INFO 6612 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 20ms. Found 0 JPA repository interfaces.
2020-10-27 11:34:31.420  INFO 6612 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-10-27 11:34:31.437  INFO 6612 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-27 11:34:31.437  INFO 6612 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-27 11:34:31.604  INFO 6612 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-27 11:34:31.604  INFO 6612 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2110 ms
2020-10-27 11:34:31.908  INFO 6612 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-27 11:34:31.922  INFO 6612 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-10-27 11:34:32.942  INFO 6612 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-10-27 11:34:32.997  INFO 6612 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-10-27 11:34:33.026  WARN 6612 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-10-27 11:34:33.118  INFO 6612 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-10-27 11:34:33.412  INFO 6612 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-10-27 11:34:33.606  INFO 6612 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-27 11:34:33.610  INFO 6612 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-10-27 11:34:33.611  INFO 6612 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-10-27 11:34:33.627  INFO 6612 --- [           main] b.c.n.i.IndividualProjectApplication     : Started IndividualProjectApplication in 4.645 seconds (JVM running for 5.232)
2020-10-27 11:34:33.670  INFO 6612 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect

然后我评论了所有其他课程并一一开始,

这是创建的:

package br.com.negromonte.individualProject.model;

import java.sql.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "client")
public class Client {
    
    @Id
    @Column(name = "cpf", unique = true, nullable = false, length = 20)
    private String cpf;
    
    @Column(name = "name", nullable = false, length = 100)
    private String name;
    
    @Column(name = "email", nullable = false, length = 100)
    private String email;
    
    @Column(name = "password",nullable = false, length = 64)
    private String password;

    @Column(name = "phone",nullable = false, length = 20)
    private String phone;
    
    @Column(name = "birth_day", nullable = false, columnDefinition = "DATE")
    private Date birthday;
    
    @Column(nullable = false)
    private boolean fidelity;
    
//  @OneToMany(fetch = FetchType.LAZY, mappedBy = "Client")
//  private List<Reservation> reservations;
    
    public Client() {
        super();
    }

    public Client(String cpf, String name, String email, String password, String phone, Date birthday,
            boolean fidelity) {
        super();
        this.cpf = cpf;
        this.name = name;
        this.email = email;
        this.password = password;
        this.phone = phone;
        this.birthday = birthday;
        this.fidelity = fidelity;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean isFidelity() {
        return fidelity;
    }

    public void setFidelity(boolean fidelity) {
        this.fidelity = fidelity;
    }

//  public List<Reservation> getReservations() {
//      return reservations;
//  }
//
//  public void setReservations(List<Reservation> reservations) {
//      this.reservations = reservations;
//  }
    
}

然后这个也是:

package br.com.negromonte.individualProject.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "hotel")
public class Hotel {
    
    @Id
    @Column(name = "cnpj", unique = true, nullable = false, length = 20)
    private String cnpj;
    
    // Length not informed once in the db is set to 255
    @Column(name = "name", nullable = false)
    private String name;
    
    @Column(name = "email", nullable = false, length = 100)
    private String email;
    
    @Column(name = "phone", nullable = false, length = 20)
    private String phone;
    
    @Column(nullable = false)
    private double rating;
    
//  @OneToMany(fetch = FetchType.LAZY, mappedBy = "Hotel")
//  private List<Reservation> reservations;
//  
//  @OneToMany(fetch = FetchType.LAZY, mappedBy = "Hotel")
//  private List<Prices> prices;
    
    public Hotel() {
        super();
    }

    public Hotel(String cnpj, String name, String email, String phone, double rating) {
        super();
        this.cnpj = cnpj;
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.rating = rating;
    }

    public String getCnpj() {
        return cnpj;
    }

    public void setCnpj(String cnpj) {
        this.cnpj = cnpj;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public double getRating() {
        return rating;
    }

    public void setRating(double rating) {
        this.rating = rating;
    }

//  public List<Reservation> getReservations() {
//      return reservations;
//  }
//
//  public void setReservations(List<Reservation> reservations) {
//      this.reservations = reservations;
//  }
//
//  public List<Prices> getPrices() {
//      return prices;
//  }
//
//  public void setPrices(List<Prices> prices) {
//      this.prices = prices;
//  }
    
    
    
    
}

但是这个:

package br.com.negromonte.individualProject.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "prices")
public class Prices {
    
    @Column(nullable = false)
    private double weekday;
    
    @Column(nullable = false)
    private double weekend;
    
    @Column(name = "weekday_fidelity", nullable = false)
    private double weekdayFidelity;
    
    @Column(name = "weekend_fidelity",nullable = false)
    private double weekendFidelity;
    
//  @ManyToOne(fetch = FetchType.EAGER, optional = false)
//  private Hotel hotels;
    
    public Prices() {
        super();
    }

    public Prices(double weekday, double weekend, double weekdayFidelity, double weekendFidelity) {
        super();
        this.weekday = weekday;
        this.weekend = weekend;
        this.weekdayFidelity = weekdayFidelity;
        this.weekendFidelity = weekendFidelity;
    }

    public double getWeekday() {
        return weekday;
    }

    public void setWeekday(double weekday) {
        this.weekday = weekday;
    }

    public double getWeekend() {
        return weekend;
    }

    public void setWeekend(double weekend) {
        this.weekend = weekend;
    }

    public double getWeekday_fidelity() {
        return weekdayFidelity;
    }

    public void setWeekday_fidelity(double weekdayFidelity) {
        this.weekdayFidelity = weekdayFidelity;
    }

    public double getWeekend_fidelity() {
        return weekendFidelity;
    }

    public void setWeekend_fidelity(double weekendFidelity) {
        this.weekendFidelity = weekendFidelity;
    }

//  public Hotel getHotels() {
//      return hotels;
//  }
//
//  public void setHotels(Hotel hotels) {
//      this.hotels = hotels;
//  }

}

还有这个:

package br.com.negromonte.individualProject.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import java.sql.Timestamp;

@Entity
@Table(name = "reservation")
public class Reservation {
    
    @Column(nullable = false, columnDefinition = "DATETIME")
    private Timestamp checkin;
    
    @Column(nullable = false, columnDefinition = "DATETIME")
    private Timestamp checkout;
    
    @Column(name = "dailys_weekday", nullable = false)
    private int dailysWeekday;
    
    @Column(name = "dailys_weekend", nullable = false)
    private int dailysWeekend;
    
    @Column(name = "total_payed", nullable = false)
    private double totalPayed;
    
    @Column(name = "was_fidelity", nullable = false)
    private boolean wasFidelity;
    
//  @ManyToOne(fetch = FetchType.EAGER, optional = false)
//  private Client client;
    
//  @ManyToOne(fetch = FetchType.EAGER, optional = false)
//  private Hotel hotel;
    
    public Reservation() {
        super();
    }

    public Reservation(Timestamp checkin, Timestamp checkout, int dailysWeekday, int dailysWeekend, double totalPayed,
            boolean wasFidelity) {
        super();
        this.checkin = checkin;
        this.checkout = checkout;
        this.dailysWeekday = dailysWeekday;
        this.dailysWeekend = dailysWeekend;
        this.totalPayed = totalPayed;
        this.wasFidelity = wasFidelity;
    }

    public Timestamp getCheckin() {
        return checkin;
    }

    public void setCheckin(Timestamp checkin) {
        this.checkin = checkin;
    }

    public Timestamp getCheckout() {
        return checkout;
    }

    public void setCheckout(Timestamp checkout) {
        this.checkout = checkout;
    }

    public int getDailysWeekday() {
        return dailysWeekday;
    }

    public void setDailysWeekday(int dailysWeekday) {
        this.dailysWeekday = dailysWeekday;
    }

    public int getDailysWeekend() {
        return dailysWeekend;
    }

    public void setDailysWeekend(int dailysWeekend) {
        this.dailysWeekend = dailysWeekend;
    }

    public double getTotalPayed() {
        return totalPayed;
    }

    public void setTotalPayed(double totalPayed) {
        this.totalPayed = totalPayed;
    }

    public boolean isWasFidelity() {
        return wasFidelity;
    }

    public void setWasFidelity(boolean wasFidelity) {
        this.wasFidelity = wasFidelity;
    }

//  public Client getClient() {
//      return client;
//  }
//
//  public void setClient(Client client) {
//      this.client = client;
//  }
//
//  public Hotel getHotel() {
//      return hotel;
//  }
//
//  public void setHotel(Hotel hotel) {
//      this.hotel = hotel;
//  }
}

没有被创建。 这里是创建工作时的 output:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.4.RELEASE)

2020-10-27 11:41:25.213  INFO 8904 --- [           main] b.c.n.i.IndividualProjectApplication     : Starting IndividualProjectApplication on Othon-NoteAMD with PID 8904 (D:\programacao\Qualiti\individualProject\target\classes started by othon in D:\programacao\Qualiti\individualProject)
2020-10-27 11:41:25.217  INFO 8904 --- [           main] b.c.n.i.IndividualProjectApplication     : No active profile set, falling back to default profiles: default
2020-10-27 11:41:26.016  INFO 8904 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-10-27 11:41:26.051  INFO 8904 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 20ms. Found 0 JPA repository interfaces.
2020-10-27 11:41:27.272  INFO 8904 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-10-27 11:41:27.290  INFO 8904 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-27 11:41:27.291  INFO 8904 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-27 11:41:27.422  INFO 8904 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-27 11:41:27.423  INFO 8904 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2139 ms
2020-10-27 11:41:27.724  INFO 8904 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-27 11:41:27.746  INFO 8904 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-10-27 11:41:28.695  INFO 8904 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-10-27 11:41:28.754  INFO 8904 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-10-27 11:41:28.780  WARN 8904 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-10-27 11:41:28.857  INFO 8904 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-10-27 11:41:29.135  INFO 8904 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-10-27 11:41:29.342  INFO 8904 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-27 11:41:29.345  INFO 8904 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-10-27 11:41:29.346  INFO 8904 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-10-27 11:41:29.362  INFO 8904 --- [           main] b.c.n.i.IndividualProjectApplication     : Started IndividualProjectApplication in 4.646 seconds (JVM running for 5.185)
2020-10-27 11:41:29.375  INFO 8904 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
Hibernate: create table client (cpf varchar(20) not null, birth_day DATE not null, email varchar(100) not null, fidelity bit not null, name varchar(100) not null, password varchar(64) not null, phone varchar(20) not null, primary key (cpf)) engine=InnoDB
Hibernate: create table hotel (cnpj varchar(20) not null, email varchar(100) not null, name varchar(255) not null, phone varchar(20) not null, rating double precision not null, primary key (cnpj)) engine=InnoDB
2020-10-27 11:41:32.030  INFO 8904 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-10-27 11:41:32.040  INFO 8904 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

要在创建上取得成功,我必须评论 Prices.java 和 Reservation.java,它们在未注释时不允许创建任何表,我的建模有问题吗? 任何错误的注释?

需要像创建成功的表一样在id字段上加上@Id注解。

按照JPA 2规范2.4 Primary Keys and Entity Identity ),class必须有一个唯一的ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM