Hibernate integration and spring Integration

Source: Internet
Author: User

Hibernate integration and spring Integration

Hibernate is an excellent persistence framework that simplifies the process of saving objects to the database, or reading data from the database and encapsulating the data into objects. Hibernate uses simple configuration and encoding to replace the tedious jdbc program code.

The following describes how to integrate hibernate.

Download the hibernate. jar package and reference it in the project.
<? 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"> Create and compile object class configuration files

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<Generator class = "/> Configure the primary key generation policy. The values are as follows:

Increment: For Primary keys of the long, short, and int types, the value of the primary key is generated in the mode of automatic growth. The increment is 1.

Sequence: for databases that support sequences, sequence can be used to assign values to the primary key. param sub-elements are used to specify the sequence name.

Identity: this policy can be used for databases that support identity columns. However, you must set this column as an ID column in Data Control.

Native: hibernate automatically determines which policy to use

Assigned: The primary key is generated by an application. hibernate does not need to participate.

After compiling the object configuration file, add reference to the configuration file in the hibernate. cfg. xml file.

Configuration cfgt = new Configuration (). configure (); // create the configuration object to hibernate. cfg. the information in the xml file is encapsulated in the cfgt object SessionFactory = cfgt. buildSessionFactory (); // create the command factory Session = factory. openSession (); // open the command Object Transaction tsc = session. beginTransaction (); // starts the transaction session. save (stu); save the information in stu to the database (stu is the object class object created) tsc. commit (); // submit the transaction session. close (); // close the command object

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.