我的应用程序中有5个实体,Hibernate不在数据库中创建其中之一。
我试图检查是否在实体类中没有创建任何错误,但是我没有发现与其他任何不同的地方。我也尝试使用dll自动更新并在休眠的application.proporties文件中创建文件,但是一切都失败了。日志显示所有表都已创建,所有外键也已设置。没有错误信息。
这些是我的实体: 订购(未创建的商品):
@Entity
@Table(name = "order")
@Data
@Builder
@AllArgsConstructor
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_id")
private int orderId;
@Column(name = "status")
@NotNull
@Size(max = 45)
private String status;
@Column(name = "total_price")
@NotNull
private BigDecimal totalPrice;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
public Order(){
//for JPA
}
}
OrderList:
@Entity
@Table(name = "order_list")
@Data
@Builder
@AllArgsConstructor
public class OrderList {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_list_id")
private int orderListId;
@Column(name = "quantity")
@NotNull
private int quantity;
@Column(name = "price")
@NotNull
private BigDecimal price;
@ManyToOne
@JoinColumn(name = "order_id")
private Order order;
@ManyToOne
@JoinColumn(name = "product_id")
private Product product;
public OrderList() {
//for JPA
}
}
产品:
@Entity
@Table(name = "product")
@Data
@Builder
@AllArgsConstructor
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id")
private int productId;
@Column(name = "name")
@NotNull
private String name;
@Column(name = "price")
@NotNull
private BigDecimal price;
@Column(name = "items_number")
@NotNull
private int itemsNumber;
@Column(name = "description")
@NotNull
private String description;
@Column(name = "picture")
private String picture;
@Column(name = "is_accessory")
private Boolean isAccessory;
public Product(){
//for JPA
}
}
角色:
@Entity
@Table(name = "role")
@Getter
@Setter
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "role_id")
private int roleId;
@Column(name = "name")
@NotNull
private String name;
public Role() {
//for JPA
}
}
用户:
@Entity
@Table(name = "user")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private int userId;
@Column(name = "name")
@NotNull
@Size(max = 45)
private String name;
@Column(name = "surname")
@NotNull
@Size(max = 45)
private String surname;
@Column(name = "email")
@NotNull
@Size(max = 100)
private String email;
@Column(name = "phone_number")
@NotNull
@Size(max = 9)
private String phoneNumber;
@Column(name = "password")
@NotNull
private String password;
@Column(name = "active")
@NotNull
private int active;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
public User(){
//for JPA
}
}
这是我的application.proporties文件:
#General
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.mvc.static-path-pattern=/resources/**
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
server.tomcat.additional-tld-skip-patterns=hk2-utils.jar,javax.annotation-api.jar,javax.inject.jar,hk2-api.jar,config-types.jar,hk2-core.jar,hk2-config.jar,tiger-types.jar,validation-api.jar,jboss-logging.jar,classmate.jar,hk2-locator.jar,javassist.jar,hk2-runlevel.jar,class-model.jar,asm-all-repackaged.jar
#Data source
spring.datasource.url=jdbc:mysql://localhost:3306/phoneshop?serverTimezone=Europe/Warsaw
spring.datasource.username=root
spring.datasource.password=
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
#Hibernate
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
#Spring Security
spring.queries.users-query=select u.email, u.password, u.active from user u where u.email=?
spring.queries.roles-query=select u.email, r.name from user u inner join user_role ur on(u.user_id=ur.user_id) inner join role r on(ur.role_id=r.role_id) where u.email=?
#Logging
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
我想我会得到6个表:Order,Order_List,product,user,role,user_role。我得到所有5,但数据库中的Order表。正如我提到的,日志中没有出现错误,并且所有表都被列为在启动期间创建的表。第二次启动后,虽然日志仅显示创建表顺序和更改所有表,但是结果是相同的-数据库中没有表顺序。这些是日志:
2019-07-19 11:58:25.524 INFO 8352 --- [ main] a.phonesshop.PhoneShopApplication : Starting PhoneShopApplication on DESKTOP-SJ9KPMI with PID 8352 (C:\Informatyka\GitHub\
Phones_Shop\target\classes started by Kosiarz in C:\Informatyka\GitHub\Phones_Shop)
2019-07-19 11:58:25.605 INFO 8352 --- [ main] a.phonesshop.PhoneShopApplication : No active profile set, falling back to default profiles: default
2019-07-19 11:58:27.567 INFO 8352 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-07-19 11:58:27.692 INFO 8352 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 109ms. Found 1 repository interfaces.
2019-07-19 11:58:28.694 INFO 8352 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfigurati
on' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$dbf5061d] is not eligible for getting processed by all BeanPostPro
cessors (for example: not eligible for auto-proxying)
2019-07-19 11:58:28.745 INFO 8352 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springfram
ework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$5b75534f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-07-19 11:58:29.812 INFO 8352 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-07-19 11:58:29.879 INFO 8352 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-07-19 11:58:29.880 INFO 8352 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-07-19 11:58:30.478 INFO 8352 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for
this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2019-07-19 11:58:30.509 INFO 8352 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-07-19 11:58:30.509 DEBUG 8352 --- [ main] o.s.web.context.ContextLoader : Published root WebApplicationContext as ServletContext attribute with name [org.spring
framework.web.context.WebApplicationContext.ROOT]
2019-07-19 11:58:30.509 INFO 8352 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4756 ms
2019-07-19 11:58:31.032 INFO 8352 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-07-19 11:58:31.323 INFO 8352 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
Hibernate:
create table order (
order_id integer not null auto_increment,
status varchar(45) not null,
total_price decimal(19,2) not null,
user_id integer,
primary key (order_id)
) engine=MyISAM
Hibernate:
alter table order
add constraint FKt7abetueht6dd1gs9jyl3o4t7
foreign key (user_id)
references user (user_id)
Hibernate:
alter table order_list
add constraint FKk4y5ik56wtwk92guiuvqesqdt
foreign key (order_id)
references order (order_id)
Hibernate:
alter table order_list
add constraint FKcj44h53c87hr23l208dyshj1n
foreign key (product_id)
references product (product_id)
Hibernate:
alter table user_role
add constraint FKa68196081fvovjhkek5m97n3y
foreign key (role_id)
references role (role_id)
Hibernate:
alter table user_role
add constraint FK859n2jvi8ivhui0rl0esws6o
foreign key (user_id)
references user (user_id)
2019-07-19 11:58:34.263 INFO 8352 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-07-19 11:58:35.980 DEBUG 8352 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/**/favicon.ico] in 'faviconHandlerMapping'
2019-07-19 11:58:36.191 INFO 8352 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-19 11:58:36.233 DEBUG 8352 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 Respo
nseBodyAdvice
2019-07-19 11:58:36.309 WARN 8352 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be perf
ormed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-07-19 11:58:36.404 DEBUG 8352 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : 13 mappings in 'requestMappingHandlerMapping'
2019-07-19 11:58:36.458 DEBUG 8352 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /resources/**] in 'resourceHandlerMapping'
2019-07-19 11:58:36.483 DEBUG 8352 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 1 @ExceptionHandler, 1 ResponseBodyAdvice
2019-07-19 11:58:36.624 INFO 8352 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2019-07-19 11:58:37.341 INFO 8352 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/resources/**'], []
2019-07-19 11:58:37.342 INFO 8352 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/statics/**'], []
2019-07-19 11:58:37.343 INFO 8352 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/css/**'], []
2019-07-19 11:58:37.344 INFO 8352 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/js/**'], []
2019-07-19 11:58:37.345 INFO 8352 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/images/**'], []
2019-07-19 11:58:37.346 INFO 8352 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/incl/**'], []
2019-07-19 11:58:37.523 INFO 8352 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.
async.WebAsyncManagerIntegrationFilter@4247e401, org.springframework.security.web.context.SecurityContextPersistenceFilter@ed4d413, org.springframework.security.web.header.HeaderWriterFi
lter@fc15460, org.springframework.security.web.authentication.logout.LogoutFilter@3c0fbebc, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@3a7a2a0a,
org.springframework.security.web.savedrequest.RequestCacheAwareFilter@44259747, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@e942467, org.springfr
amework.security.web.authentication.AnonymousAuthenticationFilter@5d8f1409, org.springframework.security.web.session.SessionManagementFilter@27fc657d, org.springframework.security.web.ac
cess.ExceptionTranslationFilter@7f5c1aad, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@2ff90fe6]
2019-07-19 11:58:37.799 INFO 8352 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-19 11:58:37.813 INFO 8352 --- [ main] a.phonesshop.PhoneShopApplication : Started PhoneShopApplication in 13.213 seconds (JVM running for 20.669)
如何使表顺序真正在数据库中创建?