'Hibernate doesn't create tables automatically
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.
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[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
Solution 4:[4]
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
Solution 5:[5]
Put property spring.jpa.generate-ddl=true above the spring.jpa.hibernate.ddl.auto = update to resolve the issue.
Solution 6:[6]
test changing the name of the table, this worked for me.
Solution 7:[7]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Egorika Belarus |
Solution 2 | paardhu |
Solution 3 | Urosh T. |
Solution 4 | Dharman |
Solution 5 | pushpendra singh Parmar |
Solution 6 | Diego Santa Cruz Mendezú |
Solution 7 | Andrei Sfat |