繁体   English   中英

在 Intellij Idea 中使用 hibernate 从实体类生成数据库表(不使用 Spring)

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

请问,有什么方法可以在Intellij Idea中使用hibernate从实体类生成数据库表? 我在网上看到的只是从数据库模式生成实体类。 但是当我修改任何实体类时,我需要相反的方式来轻松更新我的数据库。

这是我的 hibernate.cfg.xml 文件

<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" />   

这是我的 persistence.xml 文件

 <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>

这是我的 Main.class

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

}

}

示例实体类

@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<>();
}

}

我建议不要使用 hbm2ddl.auto ,您可以将Liquibase添加到您的项目中。 它允许您使用更改日志管理所有数据库架构修改,并且您可以独立于应用程序启动运行表生成/更新。

这确实在修改模型和更新数据库之间增加了一个额外的步骤,因为您必须将所有更改添加到更改日志中。 我建议为此使用JPA Buddy插件,它可以通过将您的 Java 模型与您当前的数据库进行比较来生成更改日志,这是它的外观

如果我说得对,您想从 Java 类生成数据库表,对吗? 如果是这样,那就是 ORM 的主要概念。 每当您创建一个新的 POJO 并使用 @Entity 对其进行注释时,您就是在告诉 hibernate 使用该类中的数据创建一个表(如果不存在)。 在下面找到一个例子:

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;
}

使用 JPA Buddy 一个 IntelliJ IDEA 插件,它是免费的。

JPA 结构视图中右键单击您的实体,然后单击显示 DDL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM