Sonites
Sonites

Reputation: 51

Generate Database Tables From Entity Classes using hibernate in Intellij Idea (Not using Spring)

Please, is there any way I can generate database tables from entity classes using hibernate in Intellij Idea? All I saw online is generate entity class from database schema. But I need the other way round for easy update to my database when I modify any entity class.

Here is my hibernate.cfg.xml file

<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/my_db?useSSL=false</property>


<property name="connection.username">username</property>
<property name="connection.password">password</property>


<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>

<property name="show_sql">true</property>

<property name="current_session_context_class">thread</property>

<property name="hbm2ddl.auto">create</property>


<property name="connection.pool_size">1</property>
<property name="hibernate.dbcp.initialSize">5</property>
<property name="hibernate.dbcp.maxTotal">20</property>
<property name="hibernate.dbcp.maxIdle">10</property>
<property name="hibernate.dbcp.minIdle">5</property>
<property name="hibernate.dbcp.maxWaitMillis">-1</property>


<!--Declaring entity classes to be mapped to the database-->
<mapping class="com.softpager.estores.entities.Order" />
<mapping class="com.softpager.estores.entities.Customer" />
<mapping class="com.softpager.estores.entities.Users" />
<mapping class="com.softpager.estores.entities.Product" />   

Here is my persistence.xml file

 <persistence-unit name="EStores">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/my_db?useSSL=false"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
        <property name="hibernate.connection.username" value="username"/>
        <property name="hibernate.connection.password" value="password"/>
        <property name="hibernate.archive.autodetection" value="class"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>

        <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
        <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
        <property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
    </properties>

</persistence-unit>

Here is my Main.class

public class Main {
public static void main(String[] args) {
    EntityManagerFactory factory = createEntityManagerFactory("EStores");
    EntityManager entityManager = factory.createEntityManager();

}

}

Sample Entity Class

@Data
@Entity
@Table(name = "customer")
public class Customer {

@Id
@GeneratedValue
@Column(name = "customer_id")
private long id;

@Column(name = "first_name", nullable = false)
private String firstName;

@Column(name = "last_name", nullable = false)
private String lastName;

@Embedded
private Contact contact;

@Embedded
private Address address;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set<Review> reviews;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
private Set<Product> products;


 public Customer(String firstName, String lastName, Contact contact,              
                                                  Address address) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.contact = contact;
    this.address = address;
    this.products = new HashSet<>();
    this.reviews = new HashSet<>();
}

}

Upvotes: 5

Views: 10246

Answers (3)

Samt
Samt

Reputation: 194

use JPA Buddy a IntelliJ IDEA plugin, it's free.

right click on your entity in JPA structure view then click show DDL.

Upvotes: 3

Andrey Oganesyan
Andrey Oganesyan

Reputation: 585

I would suggest against using hbm2ddl.auto, instead you can add Liquibase to your project. It allows you to manage all of your DB schema modifications using changelogs, and you can run table generation/updates independently of your app start-up.

This does add an extra step between you modifying your model and the DB being updated, since you have to add all the changes to your changelogs. I'd recommend using JPA Buddy plugin for that, it can generate changelogs by comparing your Java model to your current database, here is how it looks

Upvotes: 5

Khalil Kabara
Khalil Kabara

Reputation: 168

If i am getting you right, you want to generate database tables from your Java classes right? If so, that's the main concept of ORM. Whenever you create a new POJO and annotate it with @Entity, you are telling hibernate to create a table using data from that class (if one doesn't exist). Find below an example that does just that:

package com.test.simple_crud_application.models;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

@Entity
@Table(name = "users")
public class User implements Serializable
{
    @CreationTimestamp
    @Column(name = "created_at", updatable = false, nullable = false)
    private LocalDateTime createdAt;
    @UpdateTimestamp
    @Column(name = "updated_at", updatable = true, nullable = false)
    private LocalDateTime modifiedAt;

    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    @Column(name = "uid", unique = true, nullable = false)
    private String uid;

    @Column(name = "username", nullable = false)
    private String username;

    @Column(name = "password", nullable = false)
    private String password;
}

Upvotes: 1

Related Questions