[Hibernate] hibernate object generation table, hibernate. cfg. XML for web projects and common projects

Source: Internet
Author: User

 

Note 1: Configure () can always find the configuration file. You do not need to specify the path for it.

Configuration Config = new configuration (); // configuration object config. addFile ("src \ main \ resources \ hibernate. cfg. XML "); // load the configuration file. In fact, config is optional. configure (); // no matter what project, the configuration file under the project directory will be found.

NOTE 2: There are non-basic types in the forward project, such as list <string>. Try not to map this attribute to the database and delete them in the corresponding configuration file.

 

 

Http://blog.csdn.net/xyzroundo/article/details/5612693

Method 1:In hibernate. cfg. set <property name = "hibernate. hbm2ddl. auto "> Update </property>, which is then deployed to the application server, such as Tomcat and other Web containers, so that the Web containers can be loaded to hibernate. cfg. XML to automatically generate database tables!

Note: How can I load a Web container to hibernate. cfg. xml? You can use the following method:

Integrate to springcontext as follows:

<bean id="sessionFactory"         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation"            value="classpath:hibernate.cfg.xml">        </property>    </bean>

Springcontext can set listeners under Web. xml.

Method 2:Call the APIs in the hibernate core to compile a simple class for generating database tables:

Import Org. hibernate. cfg. configuration; import Org. hibernate. tool. hbm2ddl. schemaexport; public class exportdb {public static void main (string [] ARGs) {// read configuration file configuration CFG = new configuration (). configure ("/hibernate. cfg. XML "); // create the schemaexport object schemaexport export = new schemaexport (CFG); // create the database table export. create (True, true); // there is a serious problem. Delete the original data table before creating a new table }}

Run some classes to automatically generate database tables.

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.