无法使用mysql中的spring数据jpa创建表

时间:2018-01-30 18:57:12

标签: java mysql hibernate spring-boot spring-data-jpa

Spring JPA不会在架构或MySql db下创建表。

#DataSource
spring.datasource.url=jdbc:mysql://localhost:3306/pickalystoredatabase?autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

应用程序属性似乎很完美。我也没有在日志中看到任何错误

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, updatable = false)
    private Long id;
    @Column(name = "username", nullable = false)
    private String username;
    @Column(name = "password", nullable = false)
    private String password;
    @Column(name = "firstname", nullable = false)
    private String firstname;
    @Column(name = "lastname", nullable = false)
    private String lastname;

    @Column(name = "email", nullable = false, updatable = false)
    private String email;
    @Column(name = "phone", nullable = false)
    private String phone;
    @Column(name = "enabled", nullable = false)
    private boolean enabled = true;

    public User() { }
    //getters snad setters
}

我正在使用spring-boot-started-data-jpa maven依赖项。版本1.5.9 RC。

记录供您参考

日志不会显示任何异常,也没有任何有关表创建的信息。

2018-01-31 00:18:53.662  INFO 6640 
--- [  restartedMain] com.pickaly.application.Pickaly          : Starting Pickaly on PHYADAVI-6Q4D5 with PID 6640 (C:\Users\phyadavi\pickaly\Pickaly-Store\target\classes started by phyadavi in C:\Users\phyadavi\pickaly\Pickaly-Store) 2018-01-31 00:18:53.662  INFO 6640 
--- [  restartedMain] com.pickaly.application.Pickaly          : No active profile set, falling back to default profiles: default 2018-01-31 00:18:53.664  INFO 6640 
--- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5db9f92a: startup date [Wed Jan 31 00:18:53 IST 2018]; root of context hierarchy 2018-01-31 00:18:54.243  INFO 6640 
--- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2018-01-31 00:18:54.244  INFO 6640 
--- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat] 2018-01-31 00:18:54.244  INFO 6640 
--- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23 2018-01-31 00:18:54.269  INFO 6640 
--- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 2018-01-31 00:18:54.270  INFO 6640 
--- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 606 ms 2018-01-31 00:18:54.302  INFO 6640
--- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/] 2018-01-31 00:18:54.302  INFO 6640 
--- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-01-31 00:18:54.302  INFO 6640
--- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-01-31 00:18:54.302  INFO 6640
--- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-01-31 00:18:54.302  INFO 6640 
--- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*] 2018-01-31 00:18:54.443  INFO 6640 
--- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default' 2018-01-31 00:18:54.443  INFO 6640 
--- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [    name: default   ...] 2018-01-31 00:18:54.460  INFO 6640 
--- [  restartedMain] org.hibernate.dialect.Dialect          : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 2018-01-31 00:18:54.467  INFO 6640 
--- [  restartedMain] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update 2018-01-31 00:18:54.468  INFO 6640 
--- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2018-01-31 00:18:54.553  INFO 6640 
--- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5db9f92a: startup date [Wed Jan 31 00:18:53 IST 2018]; root of context hierarchy 2018-01-31 00:18:54.564  INFO 6640 
--- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.pickaly.controllers.HomeController.index() 2018-01-31 00:18:54.567  INFO 6640 
--- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-01-31 00:18:54.567  INFO 6640 
--- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-01-31 00:18:54.574  INFO 6640 
--- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-01-31 00:18:54.575  INFO 6640 
--- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-01-31 00:18:54.587  INFO 6640 
--- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-01-31 00:18:54.661  INFO 6640 
--- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729 2018-01-31 00:18:54.705  INFO 6640 
--- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup 2018-01-31 00:18:54.718  INFO 6640
--- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2018-01-31 00:18:54.720  INFO 6640 
--- [  restartedMain] com.pickaly.application.Pickaly          : Started Pickaly in 1.098 seconds (JVM running for 720.446)

请帮忙。

编辑:

除了上面提到的属性,我还使用了属性和注释,如

@EnableAutoConfiguration
@EntityScan(basePackages = {"com.pickaly.domains"})

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect instead of MySQLDBDialect

但这些配置不起作用。

1 个答案:

答案 0 :(得分:0)

尝试将spring.jpa.generate-ddl = true添加到您的应用程序属性中。