Learning from the Hibernate framework (I): Starting from an instance, I first learned about the Hibernate framework.

Source: Internet
Author: User

This blog is the first article on Hibernate learning. It mainly introduces the Hibernate framework, briefly introduces the directory structure of hibernate, and finally writes a simple hibernate instance. Through these three steps, we will briefly understand hibernate.

 

Not to mention nonsense, proceed to the topic. The Hibernate framework is a mainstream persistence framework based on JDBC and an excellent ORM implementation. It encapsulates JDBC in a lightweight manner, this greatly simplifies the complex duplicate code at the data access layer, and uses the Java reflection mechanism to implement program transparency. It is intended to start from an object rather than a relational database, this means that it solves Database Operation Problems with object-oriented thinking.

 

The above is the concept of hibernate. The following describes the hibernate directory structure:


 


This is the decompressed hibernate directory file. Here, the doc directory is the document, the lib directory is the library file used, SRC is the hibernate source code, and hibernate3.jar is the packaged bytecode file. Other blog posts will be introduced later.

 

The learning of the Hibernate framework is always dealing with three important files, namely, the hibernate configuration file. cfg. XML file, which is used to configure hibernate attributes, database drivers, database connections, and database dialects. The other is the object link ing configuration file, such as userinfo. HBM. XML. This file maps object to relational database. The last one is our common persistence class, which is the entity in the object reality.

 

With the above foundation, we will implement a hibernate instance step by step.

 

1. Configure the hibernate environment in Eclipse:

First, create a project named hibernate_first, and then customize a library for storing the jar package. The project name is userlibrary. Import the relevant packages, these packages include the corresponding database driver, the jar package under the Lib folder under the hibernate directory, and the hibernate3.jar under the hibernate directory.



2. Import hibernate. cfg. xml and configure the database connection:

In the hibernate directory. cfg. import the XML file to the src directory of the project, and then configure the database connection information. We select MySql as the database for this instance, so we need to go to hibernate. cfg. configure the following information in XML.

 <! Doctype hibernate-configuration Public <br/> "-// hibernate/hibernate configuration DTD 3.0/EN" <br/> "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> </P> <p> <pibernate -configuration> <br/> <session-factory> <br/> <property name = "hibernate. connection. driver_class "> COM. mySQL. JDBC. driver </property> <br/> <property name = "hibernate. connection. URL "> JDBC: mysql: // localhost: 3306/user </property> <br/> <property name =" hibernate. connection. username "> root </property> <br/> <property name =" hibernate. connection. password "> 123 </property> <br/> <property name =" hibernate. dialect "> Org. hibernate. dialect. mysqldialect </property> </P> <p> </session-factory> <br/> </pibernate-configuration>


3. Create object class user. Java

<Br/> Import Java. util. date; </P> <p> public class user {</P> <p> private string ID; </P> <p> private string name; </P> <p> private string password; </P> <p> Public String GETID () {<br/> return ID; <br/>}</P> <p> Public void setid (string ID) {<br/> This. id = ID; <br/>}</P> <p> Public String getname () {<br/> return name; <br/>}</P> <p> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> Public String GetPassword () {<br/> return password; <br/>}</P> <p> Public void setpassword (string password) {<br/> This. password = password; <br/>}</P> <p >}< br/>

 

4. provide user. HBM. XML file to complete the ing of object classes (write an object relationship ing file and put the logic of object relationship ing here. This file includes the object relationships between tables and fields. When operating on objects, the method generated by this file through the Java reflection mechanism will convert the object method to the relational method)

<? XML version = "1.0"?> <Br/> <! Doctype hibernate-mapping Public <br/> "-// hibernate/hibernate DTD ing DTD 3.0/EN" <br/> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <br/> <pibernate-mapping> <br/> <class name = "com. cjq. hibernate. user "> <br/> <ID name =" ID "> <br/> <generator class =" UUID "/> <br/> </ID> <br/> <property name = "name"/> <br/> <property name = "password"/> <br/> </class> <br/> </pibernate-mapping>

The data will be created when ing the object class. The database must be consistent with the data in the configuration file.


5. Add the user. HBM. xml file to the hibernate. cfg. xml file.

<Mapping Resource = "com/cjq/hibernate/user. HBM. xml"/>

6. Compile the running test class

Import Org. hibernate. session; <br/> Import Org. hibernate. sessionfactory; <br/> Import Org. hibernate. cfg. configuration; </P> <p> public class hibernatetest {</P> <p> Public static void main (string [] ARGs) {</P> <p> // read hibernate. cfg. XML file <br/> Configuration CFG = new configuration (). configure (); </P> <p> // create sessionfactory <br/> sessionfactory factory = cfg. buildsessionfactory (); </P> <p> // get the session <br/> session s Ession = NULL; <br/> try {<br/> session = factory. opensession (); <br/> // start the transaction <br/> session. begintransaction (); <br/> User user = new user (); <br/> User. setname ("Zhang San"); <br/> User. setpassword ("123"); </P> <p> // Save the user object <br/> session. save (User); <br/> User U1 = (User) session. load (user. class, new string ("402881e4361533e901361533eb920001"); <br/> system. out. print ("the username for loading data from the database is" + u1.getpassword (); <br/> // submit the transaction <br /> Session. gettransaction (). commit (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/> // roll back the transaction <br/> session. gettransaction (). rollback (); <br/>}finally {<br/> If (session! = NULL) {<br/> If (Session. isopen () {<br/> // close the session <br/> session. close (); <br/>}< br/>}

 

7. Suggestions

It is recommended to add the following configuration items to facilitate observation of hibernate SQL generation:

<Propertyname = "hibernate. show_ SQL"> true </property> <br/> <propertyname = "hibernate. format_ SQL"> true </property>

It is best to add the log4j configuration file and copy the configuration file to SRC to facilitate program debugging.


8. Results

 

Through some concepts, directories, and instance implementations, I now have some knowledge about hibernate. The following blog will give a brief introduction to JDBC and reflection mechanisms, which are the basis of the Hibernate framework, therefore, it is necessary to introduce the Hibernate framework before learning it in depth. Coming soon !!!

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.