Hibernate generates a database table, and hibernate generates a database
First, create an object class.
import java.util.Date;public class ProductionEntity { public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getOrigin() { return origin; } public void setOrigin(String origin) { this.origin = origin; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } private Integer id; private String origin; private Double price; private Date date;}
In Eclipse, right-click the object class and choose New-Other-Hibernate XML Mapping file (hbm. xml)
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
With the entity class and corresponding Hibernate configuration file, an ExportDB class is required to generate a database table.
import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.boot.spi.MetadataImplementor;import org.hibernate.service.ServiceRegistry;import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB { /** * @param args */ public static void main(String[] args) { ServiceRegistry serviceRegistry = (ServiceRegistry) new StandardServiceRegistryBuilder().configure().build(); MetadataImplementor metadataImplementor = (MetadataImplementor) new MetadataSources(serviceRegistry) .buildMetadata(); SchemaExport export = new SchemaExport(serviceRegistry, metadataImplementor); export.create(true, true); }}
The Code of Hibernate 5.x differs from the previous ones. The old method is no longer recommended.
With the above two classes and configuration files, you can run ExportDB. java to generate corresponding classes in the database without any exceptions.
Generated database table