英文:
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]
...
然后我逐个注释了其他类并逐个启动,
创建了以下内容:
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();
}
// 其他 getter 和 setter 方法...
}
// 同样的格式,省略了其他类的内容
但是这个类:
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();
}
// 其他 getter 和 setter 方法...
}
// 同样的格式,省略了其他类的内容
以及这个类:
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();
}
// 其他 getter 和 setter 方法...
}
没有被创建。当这两个类被取消注释时,没有任何表被创建。有关模型的问题吗?是否有错误的注解?
英文:
I've mapped 4 classes then started up with this console answer:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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
Then I've commented all others classes and started one-by-one,
this was created:
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;
// }
}
then this other too:
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;
// }
}
but this:
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;
// }
}
and this:
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;
// }
}
did not get created.
here the output when creation works:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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'
to have success on creation I have to comment Prices.java and Reservation.java which doesn't let any tables to be created when uncommented, is there a problem with my modeling? any wrong annotation?
答案1
得分: 0
你需要将 @Id 注解添加到与已成功创建的表类似的 id 字段上。
根据 JPA 2 规范(2.4 主键和实体标识),该类必须具有唯一的 ID。
英文:
You need to add an @Id annotation to the id field similar to the successfully created tables.
In accordance with the JPA 2 specification (2.4 Primary Keys and Entity Identity), the class must have a unique ID.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论