Hibernate principle, configuration and single table operations, hibernate single table

Source: Internet
Author: User

Hibernate principle, configuration and single table operations, hibernate single table

1. Hibernate configuration document

Hbm2ddl. "create" in auto indicates that the original table will be deleted every time data is modified, and a new table structure will be generated. The original data will no longer exist. "update" indicates that the original data will be updated, the original data is not deleted. Set the hibernate. default_schema attribute to the Database Name and set the database to the default database. In this way, the default database prefix is added to the SQL statement.

Ii. Hibernate Execution Process

 

Note: When executing the session method for table operations, you need to enable a transaction. These methods must be encapsulated in the transaction. After executing the method, you need to commit the transaction and then close the session.

Session can be understood as an object for database operations.

To save an object in a database, you need to call various methods of session, such as save (), update (), delete (), and createQuery.

Iii. transaction

Iv. session details

V. Detailed description of hbm configuration documents (Object link ing file)

  1. Single primary key

The subtab generator under the id tag indicates the primary key generation policy. Two types of policies are frequently used:

If the policy is generated using assigned, but the id is not manually assigned when the object is saved to the database, it will assign a value with the id's initialization value 0 in the materialized class, if a record with the id of 0 exists in the data table, it will fail to be saved. If the generation policy is native, you do not need to assign values manually. It will automatically add IDs in auto_increment mode, even if you assign values manually, it will not work.

2. Basic Types of hibernate

   In the type attribute of the link ing document, either write the hibernate type (first column) or write the java type (second column ).

   3. Object Type

Two test methods are added to test the writing objects to and reading objects from the database. The Code is as follows:

1 @ Test 2 public void testWriteBlob () throws IOException {3 Students s = new Students (); 4 s. setSname ("zhangchu"); 5 s. setGender ("male"); 6 s. setBrithday (new Date (); 7 s. setAddress ("Shanghai"); 8 File file = new File ("D: // image.jpg"); 9 InputStream is = new FileInputStream (file); 10 Blob image = Hibernate. getLobCreator (session ). createBlob (is, is. available (); 11 s. setPictrue (image); 12 session. save (s); 13} 14 15 @ T Est16 public void testReadBlob () throws Exception {17 Students s = (Students) session. get (Students. class, 1); 18 Blob image = s. getPictrue (); 19 InputStream is = image. getBinaryStream (); 20 OutputStream OS = new FileOutputStream ("d:" + File. separator + "dest.jpg"); 21 byte [] buff = new byte [is. available ()]; 22 int temp; 23 while (temp = is. read (buff, 0, is. available ()))! =-1) {24 OS. write (buff, 0, temp); 25 OS. flush (); 26} 27/* 28 * or 29 * in. read (buff); 30 * OS. write (buff); 31 */32 is. close (); 33 OS. close (); 34}

4. Component Properties

If you change the String address attribute in the code of the previous Students class to an Address class defined by yourself, there are three attributes in this class: postcode, phone, address, the Address class also needs to meet the requirements of JavaBean and add the above configuration in the hbm file. (The Students class and test class should also be modified accordingly, which is omitted here .)

  The file directory and the new table structure are as follows:

    5. add, delete, modify, and query a single table using Hibernate

CallSession save () methodYou can save an object to enter the database. It has been demonstrated before.

Query records can be usedGet and load methods of the session, Modification can be usedUpdate method of session, Delete can be usedDelete method of session. The code in the test class is as follows:

1 @ Test 2 public void testGetStudents () {3 Students s = (Students) session. get (Students. class, 2); 4 System. out. println (s); 5} 6 7 @ Test 8 public void testLoadStudents () {9 Students s = (Students) session. load (Students. class, 2); 10 System. out. println (s); 11} 12 13 @ Test14 public void testUpdateStudents () {15 Students s = (Students) session. get (Students. class, 2); 16 s. setGender ("female"); 17 session. update (s); 18} 19 20 @ Test21 public void testDeleteStudents () {22 Students s = (Students) session. get (Students. class, 2); 23 session. delete (s); 24}

The difference between the get method and the load method used to query records:

    

 

      

 

 

    

 

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.