Can't generate table from entity on Spring boot :( [closed]

ⅰ亾dé卋堺 提交于 2020-08-10 19:19:02

问题


Please help, i'm so hopeless. Although my application run without error but it still can't generate table:( There are my project structure:

this is file pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sb</groupId>
    <artifactId>sb</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

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

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>

        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This is class Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

this is class Entity

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "category")
public class CategoryEntity {
    
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long id;
    
    @Column(name = "name")
    private String name;
    
    @Column(name = "code")
    private String code;

}

This is file application.properties

server.port=8081

spring.datasource.url = jdbc:mysql://localhost:3306/springboot_database
spring.datasource.username = root
spring.datasource.password = hoa123456
spring.jpa.hibernate.ddl-auto = create-drop 

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

And it runs without error but it still can't generate table in database

2020-07-27 20:35:41.460  INFO 8352 --- [           main] sb.Application                           : Starting Application on DESKTOP-M8ASORT with PID 8352 (E:\java_hoapm\springbootproject\workspace\target\classes started by Lenovo in E:\java_hoapm\springbootproject\workspace)
2020-07-27 20:35:41.463  INFO 8352 --- [           main] sb.Application                           : No active profile set, falling back to default profiles: default
2020-07-27 20:35:41.508  INFO 8352 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2bbaf4f0: startup date [Mon Jul 27 20:35:41 ICT 2020]; root of context hierarchy
2020-07-27 20:35:42.995  INFO 8352 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8081 (http)
2020-07-27 20:35:43.009  INFO 8352 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-07-27 20:35:43.010  INFO 8352 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2020-07-27 20:35:43.139  INFO 8352 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-07-27 20:35:43.139  INFO 8352 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1634 ms
2020-07-27 20:35:43.287  INFO 8352 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2020-07-27 20:35:43.290  INFO 8352 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-07-27 20:35:43.291  INFO 8352 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-07-27 20:35:43.291  INFO 8352 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-07-27 20:35:43.292  INFO 8352 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-07-27 20:35:44.143  INFO 8352 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2020-07-27 20:35:44.156  INFO 8352 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2020-07-27 20:35:44.263  INFO 8352 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2020-07-27 20:35:44.264  INFO 8352 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2020-07-27 20:35:44.266  INFO 8352 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2020-07-27 20:35:44.292  INFO 8352 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2020-07-27 20:35:44.371  INFO 8352 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-07-27 20:35:44.529  WARN 8352 --- [           main] o.h.b.i.SessionFactoryBuilderImpl        : Unrecognized hbm2ddl_auto value : create-drop   .  Supported values include create, create-drop, update, and validate.  Ignoring
2020-07-27 20:35:44.714  INFO 8352 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-07-27 20:35:45.006  INFO 8352 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2bbaf4f0: startup date [Mon Jul 27 20:35:41 ICT 2020]; root of context hierarchy
2020-07-27 20:35:45.088  INFO 8352 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/new],methods=[DELETE]}" onto public void sb.api.NewsAPI.deleteNews(long[])
2020-07-27 20:35:45.089  INFO 8352 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/new],methods=[POST]}" onto public sb.DTO.NewsDTO sb.api.NewsAPI.createNews(sb.DTO.NewsDTO)
2020-07-27 20:35:45.089  INFO 8352 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/new],methods=[PUT]}" onto public sb.DTO.NewsDTO sb.api.NewsAPI.updateNews(sb.DTO.NewsDTO)
2020-07-27 20:35:45.091  INFO 8352 --- [           main] 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)
2020-07-27 20:35:45.091  INFO 8352 --- [           main] 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)
2020-07-27 20:35:45.124  INFO 8352 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-07-27 20:35:45.124  INFO 8352 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-07-27 20:35:45.163  INFO 8352 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-07-27 20:35:45.437  INFO 8352 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-07-27 20:35:45.494  INFO 8352 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2020-07-27 20:35:45.498  INFO 8352 --- [           main] sb.Application                           : Started Application in 4.282 seconds (JVM running for 4.554)

回答1:


try to insert the bellow property in application.properties:

spring.jpa.generate-ddl=true



回答2:


Check this line in your application.properties and remove the right whitespaces

spring.jpa.hibernate.ddl-auto = create-drop


来源:https://stackoverflow.com/questions/63116577/cant-generate-table-from-entity-on-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!