简体   繁体   中英

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:\Users\xjust\Desktop\Blog\target\classes started by xjust in C:\Users\xjust\Desktop\Blog)
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

Add necessary annotations in the models User and Role .

@Entity , @Table and @Id .

    import javax.persistence.*;
    import java.util.HashSet;
    import java.util.Set;
    
    @Entity
    @Table(name = "users")
    public class User {
        private Integer id;
    
        private String email;
    
        private String fullName;
    
        private String password;
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        @Column(name = "email", unique = true, nullable = false)
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        @Column(name = "fullName", nullable = false)
        public String getFullName() {
            return fullName;
        }
    
        public void setFullName(String fullName) {
            this.fullName = fullName;
        }
    
        @Column(name = "password", length = 60, nullable = false)
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public User(String email, String fullName, String password) {
            this.email = email;
            this.password = password;
            this.fullName = fullName;
    
            this.roles = new HashSet<>();
        }
    
        public User() {
        }
    
        private Set<Role> roles;
    
        @ManyToMany(fetch = FetchType.EAGER)
        @JoinTable(name = "users_roles")
        public Set<Role> getRoles() {
            return roles;
        }
    
        public void setRoles(Set<Role> roles) {
            this.roles = roles;enter code here
        }
    }

package softuniBlog.entity;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "roles")
public class Role {

    private Integer id;

    private String name;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private Set<User> users;

    @ManyToMany(mappedBy = "roles")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public Role() {
        this.users = new HashSet<>();

    }
}

I already use them

Here is image about how PostgreSQL works with it. enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM