Пожалуйста, можно ли как-нибудь сгенерировать таблицы базы данных из классов сущностей, используя hibernate в Intellij Idea? Все, что я видел в сети, - это генерирование класса сущностей из схемы базы данных. Но мне нужно наоборот, чтобы легко обновить базу данных при изменении любого класса сущностей.
Вот мой файл 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" />
Вот моя настойчивость. 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>
Вот мой 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<>();
}
}