Egorika Belarus
Egorika Belarus

Reputation: 165

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.

Upvotes: 6

Views: 29225

Answers (8)

Fabian Abdallah
Fabian Abdallah

Reputation: 1

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

Upvotes: 0

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

Upvotes: 0

Zulu
Zulu

Reputation: 170

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

Upvotes: 1

GtdDev
GtdDev

Reputation: 928

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.

Upvotes: -1

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

Upvotes: -1

Egorika Belarus
Egorika Belarus

Reputation: 165

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.

Upvotes: 7

Urosh T.
Urosh T.

Reputation: 3814

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

Upvotes: 3

paardhu
paardhu

Reputation: 111

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.

Upvotes: 5

Related Questions