6

I have Maven project with Hibernate/Spring/MySQL. I have basic entity and I want Hibernate to create tables automatically, but Hibernate doesn't create any tables. No exceptions are thrown too, so I have no idea what is wrong here.

application.properties:

# =============================== # = DATA SOURCE # =============================== spring.datasource.url = jdbc:mysql://localhost:3306/task spring.datasource.username = root spring.datasource.password = 12345678 spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 # =============================== # = JPA / HIBERNATE # =============================== spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = create spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect spring.datasource.driver-class-name = com.mysql.jdbc.Driver spring.jpa.properties.hibernate.format_sql = true spring.jpa.properties.hibernate.id.new_generator_mappings = true logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE spring.datasource.tomcat.max-wait=20000 spring.datasource.tomcat.max-active=50 spring.datasource.tomcat.max-idle=20 spring.datasource.tomcat.min-idle=15 spring.datasource.tomcat.test-while-idle=true spring.datasource.tomcat.test-on-borrow=true spring.datasource.tomcat.time-between-eviction-runs-millis=3600000 spring.datasource.tomcat.validation-query=SELECT 1 

User.java:

package com.example.model; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import lombok.Data; @Entity @Table(name = "user") @Data public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private int id; @Column(name = "telegramId") private Integer telegramId; @Column(name = "firstName") private String firstName; @Column(name = "lastName") private String lastName; @OneToMany( mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true ) private List<Message> msg = new ArrayList<>(); } 

In User.java I also use lombok. Any suggestions? Thank you.

2
  • 2
    try to add spring.jpa.generate-ddl=true to your properties Commented Jun 9, 2018 at 12:15
  • Your code looks fine. Have you tried doing some unit testing? Create a Repository to insert / extract data from the DB. Commented Jun 9, 2018 at 18:09

8 Answers 8

7

I found a solution. The problem was that Application.java was in package com.example.BotApp., now it's in com.example.. I don't know, but somehow it helped.

Sign up to request clarification or add additional context in comments.

2 Comments

That means that your entity class was not being picked up by spring boot annotations scanning (which by default scans current package and it's subpackages). It is explained on docs.spring.io/spring-boot/docs/current/reference/html/… . If your main class has it's own package like com.example.boot, you will need to add the scanBasePackages, like so: @SpringBootApplication(scanBasePackages = {"com.example" }) in order to scan everything in other packages
the same occurred to me...when I created the packages as a child of Application.java package, then everything worked
5

you are missing this property in your properties file

hibernate.hbm2ddl.auto="update"

spring.jpa.properties.hibernate.hbm2ddl.auto=update 

hibernate.hbm2ddl.auto is automatically validates and exports DDL to schema when the sessionFactory is created.

By default, It is not doing any creation or modification automatically on db. If user sets values to update or create or validate or create-drop then it is doing DDL schema changes automatically according to given value.

1 Comment

Unfortunately, it doesn't help
3

If you are using spring-data (which I hope you are), remove the @Table annotation, Spring will automatically create a table with the name user just from the @Entity. The minimum config that should go inside your application.propertes is this:

# Hibernate spring.datasource.platform=mysql spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update # Mysql spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://yourdburl:3306/test?createDatabaseIfNotExist=true // creates a schema if doesn't exist spring.datasource.username=root spring.datasource.password=12345678 

spring.jpa.hibernate.ddl-auto=update will update your DB table (if necessary) every time your app connects to it. It should also create it if there is none. Other values are create and create-drop which are pretty much self-explanatory

Comments

1

I have faced the same issue. Following may be one of the reason.

In my case:

Earlier,
configuration class was inside com.abc.school package and my entity class was inside com.abc.entity package.

So , package name for entity class was not correct. It should be com.abc.school.entity

Comments

0

Put property spring.jpa.generate-ddl=true above the spring.jpa.hibernate.ddl.auto = update to resolve the issue.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

If you dont have the same package as the package of your main application then you have to add @EntityScan("package where to find the entitys").

Edit: Normal Spring Boot package scan doesnt recognize outside of the application base package where entities are therefore you can use @EntityScan which allows to scan independently from @ComponentScan to scan for JPA entities. Heres the javadoc for @EntityScan https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/domain/EntityScan.html

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

test changing the name of the table, this worked for me.

Comments

-1

in my case, I believe that, because I was using hibernate 5, hibernate 5 should solve some format problems for the columns table automatically, some tables were not created, but others were. I DIFF the files to analyze the differences...and the problem was... I was using a property and that impeded the creating of the table...

@Column(nullable = false, columnDefinition = "DECIMAL(6,6) DEFAULT 0.00") private Double longitude; 

I've deleted columnDefinition = "DECIMAL(6,6) DEFAULT 0.00", and the tables were created normally.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.