Hibernate framework for beginners

Source: Internet
Author: User

Today, I first learned about Hibernate and some principles of hibernate. Hibernate is An ORM-based persistence layer framework that encapsulates JDBC very lightweight objects, allowing Java programmers to manipulate the database using the object programming thinking as they wish. ORM (object relation map) is the object relationship ing. There are two ing methods: ① annotation ing and ② xml configuration file ing. The first method is relatively simple, however, the coupling ratio is high. The second method is more complex, but the coupling ratio is much lower. Because our development systems are pursuing high cohesion and low coupling, the second method is more commonly used.

Using hibernate to achieve "three copies, three copies, six preparations, and seven steps" is a phrase.

Three copies, three copies, and six preparations: three copies refer to the JDBC driver package, Hibernate development kit, and log4j package, configuration three refers to the configuration of pojo class, the ing between configuration class and database table, and the configuration file (used for database connection ).

First, we will talk about three copies, three copies, and six preparations. We don't need to talk about copying JDBC driver packages. Because Hibernate is also based on JDBC, it is only encapsulated, so JDBC driver packages are essential. Since Hibernate is used, the hibernate Development Kit is also essential to provide various types. The log4j package is used to record log information. This is also very useful because later systems will be released to the server. It is impossible for you to stay next to the server every moment to see if there are any problems, with logs, you can save a lot of trouble. You only need to view the log file regularly. All problems are recorded in the log file. You don't need to talk about how to configure pojo in the Three-configuration. It's similar to the previous one. It's okay to have a table corresponding to a pojo class. Because Hibernate is a framework based on object link ing, the ing Configuration between pojo class objects and Relational Tables is essential. You need to configure three things: ing between tables and classes, ing between attributes and fields, and ing between tables and tables (if the two tables have a primary foreign key relationship, there is only one table here, so no configuration is required). The configuration code is as follows:

Student Class in pojo

 1 package com.pojo; 2  3 import java.io.Serializable; 4 import java.util.Date; 5  6 public class Student implements Serializable { 7     private int sid; 8     private String sname; 9     private Date sage;10     public int getSid() {11         return sid;12     }13     public void setSid(int sid) {14         this.sid = sid;15     }16     public String getSname() {17         return sname;18     }19     public void setSname(String sname) {20         this.sname = sname;21     }22     public Date getSage() {23         return sage;24     }25     public void setSage(Date sage) {26         this.sage = sage;27     }28     29 }

The ing code of Student. HBM. xml between student class and student table:

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <! Doctype hibernate-mapping public "-// hibernate/hibernate DTD ing DTD 3.0 // en" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 5 6 

 

Finally, the configuration of the configuration file hibernate. cfg. XML (with visualized interface operations) is used to establish a database connection. The Code is as follows:

 1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5  6 

So far, the preparation has been completed, and the next step is to manipulate the database. There are seven steps to operate the database:

① Read the configuration file

② Create a session Factory

③ Open the session

④ Start transaction: the transaction must be used to add, delete, and modify data in hibernate. query is not required.

⑤ Process transactions, save, delete, and update data tables)

⑥ Commit a transaction or roll back a transaction

7. Close the session.

The following is an example of inserting data into student:

1 package COM. test; 2 3 4 5 import Java. SQL. date; 6 7 Import Org. hibernate. session; 8 Import Org. hibernate. sessionfactory; 9 Import Org. hibernate. transaction; 10 Import Org. hibernate. cfg. configuration; 11 12 Import COM. pojo. student; 13 14 public class insert data {15 16/** 17 * @ Param args18 */19 public static void main (string [] ARGs) {20 // 1. read configuration file 21 configuration CFG = new configuration (). configure (); 22 23 // 2. create session factory 24 sessionfactory factory = cfg. buildsessionfactory (); 25 26 // 3. open session27 session = factory. opensession (); 28 29 // 4. new transaction: you must use a transaction to add, delete, and modify data in hibernate. You do not need to use 30 transaction TR = session for queries. begintransaction (); 31 32 try {33 // 5. transaction 34 student s = new student (); 35 S. setsname ("xinxin"); 36 s. setsage (date. valueof ("1992-10-09"); 37 session. save (s); 38 // 6. commit transaction 39 tr. commit (); 40} catch (exception e) {41 // 6. the transaction rolls back 42 tr. rollback (); 43} 44 // 7. close SESSION 45 session. close (); 46} 47 48}

The following is an example of the query table Student:

1 package COM. test; 2 3 Import Java. util. list; 4 5 import Org. hibernate. query; 6 Import Org. hibernate. session; 7 Import Org. hibernate. sessionfactory; 8 Import Org. hibernate. cfg. configuration; 9 10 Import COM. pojo. student; 11 12 public class query all {13 14/** 15 * @ Param args16 */17 public static void main (string [] ARGs) {18 // read the configuration file 19 configuration CFG = new configuration (). configure (); 20 // create a session Factory 21 sessionfactory factory = cfg. buildsessionfactory (); 22 // open session23 session = factory. opensession (); 24 // hql for batch query (Table Name Change class name, field name change attribute name) 25 query = session. createquery ("from student where Sid =? "); 26 // when the question mark is assigned here, it is the 27 query starting from 0. setinteger (0, 2); 28 list <student> List = query. list (); 29 for (Student: List) {30 system. out. println (student. getsname (); 31} 32 session. close (); 33} 34 35}

You can use the get () method of the session to query data for a record based on the primary key. to query multiple records, you must use hql (Hibernate query language). hql is similar to SQL statements, you only need to change the field name to the class name or the attribute name.

 

 

  

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.