Hibernate generates a database table, and hibernate generates a database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.