0

I`m new to Spring Boot and trying to create tables from my entities in MySQL database, but unfortunately nothing is created after running my application. Here is my code:

User.java

@Entity
public class User {
    @Id
    @GeneratedValue
    private Long id;

    private String firstName;

    private String lastName;

    @Column(unique = true, nullable = false)
    private String username;

    @Column(nullable = false)
    private String password;

    private String prevPass;

    @Column(nullable = false)
    private Organization organization;

    @ManyToMany
    @Column(nullable = false)
    private Set<Role> roles = new HashSet<>();

    @Column(nullable = false)
    private Boolean enabled = true;

    @Column(nullable = false, unique = true)
    private String email;

    private String position;

    public User() {
    }
}

Role.java

@Entity
public class Role {
    @Id
    @GeneratedValue
    private Long id;

    @Column(unique = true, nullable = false)
    private String code;

    @Column(unique = true, nullable = false)
    private String title;
}

application.properties

spring.datasource.url= jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=sth
spring.jpa.hibernate.ddl-auto=update

pom.xml dependencies

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

I have also used

spring.jpa.hibernate.ddl-auto=create

and 

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

in my application.properties file. I have to mention that my entities are in the same package with the class containing @SpringBootApplication annotaion. Here is the log:

2020-08-10 22:52:14.139  INFO 10011 --- [           main] a.b.c.abc          : Starting abc on user with PID 10011 
2020-08-10 22:52:14.139  INFO 10011 --- [           main] a.b.c.abc          : No active profile set, falling back to default profiles: default
2020-08-10 22:52:15.339  INFO 10011 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-08-10 22:52:15.370  INFO 10011 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23ms. Found 0 JPA repository interfaces.
2020-08-10 22:52:16.292  INFO 10011 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-10 22:52:16.307  INFO 10011 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-10 22:52:16.307  INFO 10011 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-10 22:52:16.495  INFO 10011 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-10 22:52:16.495  INFO 10011 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2180 ms
2020-08-10 22:52:16.886  INFO 10011 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-10 22:52:16.948  INFO 10011 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-08-10 22:52:17.011  WARN 10011 --- [           main] 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
2020-08-10 22:52:17.042  INFO 10011 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.18.Final
2020-08-10 22:52:17.378  INFO 10011 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-08-10 22:52:17.724  INFO 10011 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-08-10 22:52:18.251  INFO 10011 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-10 22:52:18.258  INFO 10011 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-08-10 22:52:18.259  INFO 10011 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-08-10 22:52:18.299  INFO 10011 --- [           main] a.b.c.abc          : Started abc in 4.794 seconds (JVM running for 9.263)
2020-08-10 22:52:18.786  INFO 10011 --- [         task-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-08-10 22:52:18.817  INFO 10011 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect

Can anyone help me please?

6
  • can you show us the logs. when u start the project Commented Aug 10, 2020 at 18:17
  • Can you provide more details like logs or any run time exception that occurred. Commented Aug 10, 2020 at 18:23
  • use spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect (if using springboot 2.x) else use - spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect Commented Aug 10, 2020 at 18:26
  • @Hamza I`ve just added. Commented Aug 10, 2020 at 18:27
  • @AjayKumar unfortunately didn`t work. Commented Aug 10, 2020 at 18:28

2 Answers 2

1

it might be helpful to do the following:

  1. Running class should have @SpringBootApplication or at least one of your Configuration classes should have @EnableAutoConfiguration over it.
  2. You don't need tomcat as a dependency.
  3. Can you update your pom file to show us Spring Boot parent as well?
Sign up to request clarification or add additional context in comments.

2 Comments

this answer shhould works. and spring.jpa.hibernate.ddl-auto=update, works 2 just fine
I already use @SpringBootApplication in my run class. I have updated my pom.xml above. Thanks for your help.
0

I finally solve it. Tables could not be created because I hadn`t used One-to-One relationship for foreign key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.