Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

java - Spring Application won't create table in MySQL/PostgreSQL

I am an intern in a company and I have a task to make a blog so that a person can register and post articles, but I have not dealt with this language before and I need a little help. I starting my application and it connects fine with data source (I tried with PostgreSQL and MySQL) but tables won't be created.

2021-01-28 12:02:40.851  INFO 3964 --- [  restartedMain] com.mikov.blog.BlogApplication           : Starting BlogApplication using Java 15.0.2 on DESKTOP-GCFHK8P with PID 3964 (C:UsersxjustDesktopBlogargetclasses started by xjust in C:UsersxjustDesktopBlog)
2021-01-28 12:02:40.851  INFO 3964 --- [  restartedMain] com.mikov.blog.BlogApplication           : No active profile set, falling back to default profiles: default
2021-01-28 12:02:40.917  INFO 3964 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-01-28 12:02:40.917  INFO 3964 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-01-28 12:02:41.636  INFO 3964 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-01-28 12:02:41.636  INFO 3964 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 3 ms. Found 0 JPA repository interfaces.
2021-01-28 12:02:42.150  INFO 3964 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-01-28 12:02:42.158  INFO 3964 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-01-28 12:02:42.158  INFO 3964 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-01-28 12:02:42.239  INFO 3964 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-01-28 12:02:42.239  INFO 3964 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1322 ms
2021-01-28 12:02:42.368  INFO 3964 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-01-28 12:02:42.405  INFO 3964 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.25.Final
2021-01-28 12:02:42.483  INFO 3964 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-01-28 12:02:42.536  INFO 3964 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-01-28 12:02:42.789  INFO 3964 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-01-28 12:02:42.805  INFO 3964 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2021-01-28 12:02:42.984  INFO 3964 --- [  restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-01-28 12:02:42.990  INFO 3964 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-01-28 12:02:43.021  INFO 3964 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2021-01-28 12:02:43.068  WARN 3964 --- [  restartedMain] 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
2021-01-28 12:02:43.179  INFO 3964 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-28 12:02:43.326  INFO 3964 --- [  restartedMain] .s.s.UserDetailsServiceAutoConfiguration : 

Using generated security password: 7a6bb800-bfbb-4e4f-b97f-ab3f91ceac2d

2021-01-28 12:02:43.410  INFO 3964 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@576d3766, org.springframework.security.web.context.SecurityContextPersistenceFilter@b2d6f01, org.springframework.security.web.header.HeaderWriterFilter@2fb2f762, org.springframework.security.web.csrf.CsrfFilter@67459a, org.springframework.security.web.authentication.logout.LogoutFilter@6831ce52, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@25f444ee, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@604dba03, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@4762d3a5, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@5b35e5d8, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@5f2a8206, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4f2ee756, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@330df5b4, org.springframework.security.web.session.SessionManagementFilter@1565b1d6, org.springframework.security.web.access.ExceptionTranslationFilter@7d11d81, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@45812c0b]
2021-01-28 12:02:43.472  INFO 3964 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-01-28 12:02:43.495  INFO 3964 --- [  restartedMain] com.mikov.blog.BlogApplication           : Started BlogApplication in 3.05 seconds (JVM running for 3.923)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mikov</groupId>
    <artifactId>blog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>blog</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>15</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>

    </dependencies>

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

</project>

My Application:

@SpringBootApplication
public class BlogApplication {

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

}

Controller:

@Controller
public class UserController {

    @Autowired
    RoleRepository roleRepository;

    @Autowired
    UserRepository userRepository;

    @GetMapping("/register")
    public String register(Model model){
        model.addAttribute("view","user/register");
        return "base-layout";
    }
}

Application.properties:

spring.datasource.url = jdbc:mysql://localhost:3306/javablog
spring.datasource.username =root
spring.datasource.password =password
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl

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

spring.thymeleaf.cache = false
question from:https://stackoverflow.com/questions/65935057/spring-application-wont-create-table-in-mysql-postgresql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add necessary annotations in the models User and Role.

@Entity, @Table and @Id.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...