Start with hibernate learning from XML and start with hibernatexml.

Source: Internet
Author: User

Start with hibernate learning from XML and start with hibernatexml.
The previous blog briefly introduced how to build the Hibernate environment, but it has not yet begun to enter the persistence design stage. This blog began designing hibernate.

See the hibernate schematic: The hibernate design consists of three steps: entity class design, entity class ing file writing, and hibernate configuration file writing.



I. entity Design

In the past, we used process-oriented programming and dealing with databases. We started to use datatable, dataset, etc. We gradually started to access object-oriented design and began to use object-oriented ideas to wrap our own code. Object-Oriented Design Philosophy, everything is object-oriented. Therefore, object class design is the core of object-oriented design.
Only by transferring entities in the business logic can the "demand data" be saved to the database. The entities in the project are bridges between user requirements and data (databases.

The object-oriented design is not the focus here. hibernate's object design is our usual entity design.

Look at an instance of unit:


using System;using System.Collections;namespace UIEntity{    public class UnitEntity    {        protected int _unitId;        /// <summary>        /// </summary>        public virtual int unitId        {            get            {                return _unitId;            }            set            {                _unitId = value;            }        }        protected String _unitName;        /// <summary>        /// </summary>        public virtual String unitName        {            get            {                return _unitName;            }            set            {                if (value != null && value.Length > 254)                    throw new ArgumentOutOfRangeException("Invalid value for unitName", value, value.ToString());                _unitName = value;            }        }        protected String _unitDesc;        /// <summary>        /// </summary>        public virtual String unitDesc        {            get            {                return _unitDesc;            }            set            {                if (value != null && value.Length > 254)                    throw new ArgumentOutOfRangeException("Invalid value for unitDesc", value, value.ToString());                _unitDesc = value;            }        }        protected String _address;        /// <summary>        /// </summary>        public virtual String address        {            get            {                return _address;            }            set            {                if (value != null && value.Length > 254)                    throw new ArgumentOutOfRangeException("Invalid value for address", value, value.ToString());                _address = value;            }        }        protected IList _personEntities;        public virtual  IList personEntities        {            get            {                if(_personEntities==null)                {                    _personEntities=new ArrayList();                }                return _personEntities;            }            set            {                _personEntities = value;            }                   }    }}

Ii. Compiling object class ing files

The object class ing file ends with "hbm. xml. Different from the configuration file "cfg. xml ".
Why does a ing file appear? As mentioned in the previous blog, hibernate is a hero of object ing files that can persist object classes to the database, hibernate uses these "conventions" to read and write entity information to the database.


Principles: The object class ing file corresponds to the object class to be mapped to the database.

That is to say: if you need to persist an object class to the database, there must be a ing file for this class. However, the entity class and the ing file are not one-to-one, multiple object classes can be persisted to the database through a ing file.

Representation:
One object class and one ing File
Multiple object classes and one ing File

1. An object is represented by a ing file. For the entity class Unit, we need a Unit. hbm. xml.

<?xml version="1.0" encoding="utf-8" ?>In most cases, we use this one-to-one representation, but in inheritance ing, we can have another form (inheritance ing has three forms, which are not described here, it only introduces the same hbm. an example of ing an xml file to multiple object classes ).


2. Multiple object classes, one ing File

Background:
The abstract class Animal of an Animal has two sub-classes: pig and bird. The purpose is to map a specific class into a data table. Because Animal is an abstract class, we do not map two child classes to two data tables.
In this xml, we use abstract to mark that the Animal class does not need to be mapped to a database table, while bird and pig need to be mapped to a database table.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">         

Iii. Compiling the hibernate configuration file. We mentioned in the previous blog that the cfg. xml file is written.

Hibernate. cfg. xml contains many contents, which are divided into three parts:

1. Database-related ---- necessary information

Database DRIVER: hibernate. connection. driver_class
Database: hibernate. connection. url
Username: hibernate. connection. username
Password: hibernate. connection. password
Database dialect: hibernate. dialect

2. Common configuration information

<! -- Configure the cache provider -->
<Property name = "hibernate. cache. provider_class"> org. hibernate. cache. EhCacheProvider </property>

<! -- Enable Level 2 cache, which is also its default configuration -->
<Property name = "hibernate. cache. use_second_level_cache"> true </property>
............

3. ing file information-required

<Mapping resource = "cassie/Hibernate/CollectionList. hbm. xml"/>

With the ing file address, the hibernate configuration is complete. Otherwise, none of our object classes can be mapped.

Common examples are as follows:

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

The above is the main three-step task of using hibernate. In fact, after writing a blog, I think there are only two steps: Writing xml files in the two steps. Because entity classes must be designed in any case. The role of these two xml files is the most obvious rule in hibernate: Convention. Xml can be identified and mapped to the database only after it is written according to the rules set by hibernate. This is an understanding of hibernate xml files. From a macro perspective, we can understand the difference between hibernate xml and previously used xml: there are just a few more labels related to hibernate.

The next blog introduces the meaning of the elements contained in these xml files.



Is it necessary to learn about hibernate xml configuration?

I have already worked. Share some experience.
You should focus on Annotation, but you should be familiar with the xml configuration, because you didn't decide what to use, the company you went, if they use any configuration, You have to configure it. So it is better to get familiar with it by the way.

Where should I learn more about java ee, such as jsp, servlet, spring, struct, hibernate, and XML?

JSP-JavaBean-XML-Servlet-JSTL-EL-JTL-Struts-Hibernate-Spring

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.