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

Java 在Intellij Idea中使用hibernate从实体类生成数据库表(不使用Spring),java,hibernate,jpa,intellij-idea,Java,Hibernate,Jpa,Intellij Idea,请问,在Intellij Idea中使用hibernate可以从实体类生成数据库表吗?我在网上看到的只是从数据库模式生成实体类。但我需要另一种方法,以便在修改任何实体类时轻松更新数据库 这是我的hibernate.cfg.xml文件 } 示例实体类 @Data @Entity @Table(name = "customer") public class Customer { @Id @GeneratedValue @Column(name = "customer_id") private

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

这是我的hibernate.cfg.xml文件

}

示例实体类

@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<>();
}
@数据
@实体
@表(name=“客户”)
公共类客户{
@身份证
@生成值
@列(name=“customer\u id”)
私人长id;
@列(name=“first\u name”,nullable=false)
私有字符串名;
@列(name=“last_name”,nullable=false)
私有字符串lastName;
@嵌入
私人接触;
@嵌入
私人地址;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“customer”)
私集审查;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“customer”)
私人套装产品;
公共客户(字符串名、字符串名、联系人、,
地址(地址){
this.firstName=firstName;
this.lastName=lastName;
this.contact=contact;
this.address=地址;
this.products=new HashSet();
this.reviews=new HashSet();
}

}

如果我没弄错,您想从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;
}
我会建议,相反,你可以添加到你的项目。它允许您使用变更日志管理所有DB模式修改,并且您可以独立于应用程序启动运行表生成/更新

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

如果您使用的是JPA,则可以根据需要将应用程序属性中的“spring.JPA.hibernate.ddl auto”属性设置为“创建”或“更新”。您可以在这里阅读更多关于它的信息是的,使用spring框架和Eclipse IDE可以很容易地工作,但在本例中,我没有使用spring,它是一个普通的Java hibernate应用程序,我使用Intellij。我刚刚更新了我的问题,请看一看。谢谢您是否正在intellij idea中寻找选项/功能?我不明白,您的hbm中有ddlauto,在启动应用程序时应该创建/更新表。确实,我第一次运行应用程序时希望Intellij从实体类创建表,但我注意到它不能,因此,我必须首先创建数据库表,并要求Intellij从它所创建的数据库表生成实体类。因此Intellij仅在应用程序后续运行中检测到实体类列中的任何更改时,才使用hibernate配置文件更新数据库。谢谢你的跟进。当然,我已经把我所有的类都注释成这样了,它在Eclipse和Spring中工作得很好。但它在Intellij中不起作用。我只能从数据库表生成实体类,不能从实体类生成数据库表。我刚刚更新了我的问题,请看一看。谢谢
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<>();
}
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;
}