Question:
How can I configure Hibernate to automatically create or update database tables based on defined entity classes?
Context:
You have created an entity class, ServerNode, and defined a persistence unit in persistence.xml. You also have a script to persist an instance of ServerNode. However, the target database does not yet contain any tables.
Solution:
To enable Hibernate's automatic table creation or update, follow these steps:
In your persistence.xml file, locate the property hibernate.hbm2ddl.auto.
For example:
<property name="hibernate.hbm2ddl.auto" value="create" />
Set the value of hibernate.hbm2ddl.auto to one of the following:
(Optional) To explicitly specify the table name, use the javax.persistence.Table annotation in your entity class.
For example:
@Entity @Table(name = "ServerNodes") public class ServerNode { // ... }
The above is the detailed content of How Can Hibernate Automatically Create or Update Database Tables from Entity Classes?. For more information, please follow other related articles on the PHP Chinese website!