Hibernate mapping (i)--basic mapping

Source: Internet
Author: User

The basic understanding of object-relational mapping (ORM) has been reflected in two previous blogs.

ORM Basic Understanding

On the mapping scheme of ORM

What we are learning today is Hibernate's object-relational mapping.


several ways to map hibernate

Basic Mapping

Configuration of mappings in Hibernate in XML, we want to map the object and table to the transformation, only need to configure XML, so learn to use Hibernate, the focus is also in the configuration file configuration.

First we start with the basic mapping, as in the following example:

1. User entity class:

  

Import Java.util.date;public class User {private string Id;private string Name;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;}}

2. mapping File User.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--1, root node of the mapping file, import package name--  


3. Add the mapping file in Hibernate.cfg.xml User.hbm.xml


The above is the most basic mapping implementation, very simple, where the more important point is the primary key generation.

  Why use a primary key to generate?

    Each entity object is unique and needs to be identified, the table is identified by a primary key, and the entity is identified by using <id> in the configuration file, and the primary key must be provided.


primary key generation policy


As you can see from the code above, the <generator class= "UUID"/>uuid generation strategy is used here, and the UUID is just one of the primary key generation strategies.

Hibernate primary key generation adopts the policy mode to design, each generation strategy directly or indirectly implements the Identifiergenerator interface, this interface has only one method Publicserializablegenerate ( Sessionimplementorsession,objectobject) Throwshibernateexception; This method implements the specific generation logic for each class.


Hibernate has a number of primary key generation strategies, but it can be broadly divided into three categories:  

1, do not need to interact with the database can generate ID: Uuid,uuid2,uuid.hex

Description: You do not need to interact with the database, which means that the primary key value is generated in the instantaneous state of the entity object.

Benefits: No need to interact with the database, faster

2. Need to interact with database to generate ID: GUID, identity, sequence, native, foreign

Description: Needs to be generated interactively with the database and needs to be queried once to generate

Identity:mysql,sql server generation mode, self-increment sequence

Sequence:oracle generation mode, self-increment sequence

Native:identity+sequence, shielding the differences in the database

Foreign: Used only when using a one-time association map based on a shared primary key. That is, the primary key of one object is generated by the primary key of another table referenced.

3, without interaction, their own management: assigned

Description: Primary key We set it manually (as required).

Choice of strategy  

The primary key to be unique, the primary key is preferably distributed, the primary key is best not to maintain, and finally to ensure efficiency, so said the recommended UUID, its characteristics is to generate the primary key is unique, and the database is not dependent on the portability of strong. At present really needs to need other generation strategy that depends on the requirements, such as the generation of serial number, there is a certain way to generate, and the only, that the serial number can be used as the primary key, the use of assigned strategy.

Summary

Hibernate mapping is the focus of hibernate, the basic mapping is only the beginning of the map, is the most basic, the next summary of the relationship map, the relationship map is the relationship between entities and entities, according to the requirements to clarify the relationship, mapping is out, it will be very simple.



Hibernate mapping (i)--basic mapping

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.