ORM Advanced: Hibernate framework construction and development

Source: Internet
Author: User

This section will start by using Hibernate to build the persistence layer. Of course, before deciding to use it, please also consider whether to use it. Learn about Hibernate 's pros and cons.

Hibernate Pros vs .

Hibernate is a persistent ORM framework, first of all resume a Java Project this will not have to say more. After that, we'll build the hibernate framework.

First step: Introduce the relevant Jar package. Contains two parts, core jar Packages (Hibernate_home/hibernate3.jar) and related packages (hibernate_home/ Lib/*.jar) such as.

If not, please download hibernate and unzip

Step Two: Create an entity class *. Java

Package Com.bjpowernode.hibernate;import Java.util.date;public class User {//primary key private string id;//name private string name;//Password private string password;//provides getter and setter method public string GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}

Step three: Provide *.hbm.xml file, which can be said to be a configuration file of the Entity generation table. Hibernate generates database tables based on the entity classes you write and the corresponding mapping files .

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">


Fourth Step: Provide Hibernate.cfg.xml file, complete the basic configuration information, and *.hbm.xml file configuration entered. (can refer to and modify their own) according to their own needs and trade-offs. Cfg.xml is the entry configuration for the entire hibernate

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">


Use the tool class to map the generated entity classes to the database (Generate database tables)

If you have set the <propertyname= "Hbm2ddl.auto" >update</property> then you don't have to write a tool class.

Note: The database needs to be established manually .

One feature of hibernate is its low coupling and easy unit testing. So it's best to set up a unit test class. Test the connectivity of the code.

Fifth step: Write the Unit test class. Whether the debugging was successful. is also the quality of the test code

Package Com.bjpowernode.hibernate;import Org.hibernate.session;import Junit.framework.testcase;public class Usertest extends TestCase {public void testSave1 () {Session session=null;try{session=hibernateutils.getsession ();// Open transaction session.begintransaction (); User User=new User (); User.setname ("Zhang San"); User.setpassword ("123456")      ; Transaction commit session.gettransaction (). commit ();} catch (Exception e) {e.printstacktrace ();//Transaction Rollback session.gettransaction (). rollback (); finally{      //Close sessionhibernateutils.closesession (session);}}}


through the above steps, the Hibernate structure of the building. Developed gradually through self-test. the core of Hibernate is the conversion of or , which is to write the hbm mapping file. Next, we'll explain in a follow-up


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ORM Advanced: Hibernate framework construction and development

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.