Hibernate base Mapping

Source: Internet
Author: User
Tags map class

before we say Hibernate mapping, let's look at the object-relational mapping ORM. The implementation idea of ORM is to map the data of tables in the relational database into objects, which are presented in the form of objects . This allows developers to translate the operations of the database into operations on those objects. Let's see a picture.

With this graph, we can see that the business entity, which behaves as relational data in the database, behaves as an object in memory. An application handles objects easily, but it is difficult to handle relational data. ORM does the mapping between relational data and object data, and can generate SQL statements automatically through mapping relationships, and serves as a bridge between the business logic layer and the data layer.

Hibernate Mappings

Hibernate file
    1. Mapping Class (*.java): It is the structure that describes a database table, and the fields in the table are described as attributes in the class, which in the future can be implemented to map the records in the table into objects of that class.
    2. mapping File (*.hbm.xml): It is the relationship between a specified database table and a mapping class, including mapping classes and database tables, corresponding relationships between table fields and class property types, and the correspondence between table fields and class property names.
    3. Database configuration file (*.properties/*.cfg.xml): It is the connection information that is required to specify the connection to the database, such as which database to connect to, the user name to log in to the database, the login password, and the connection string. Of course, you can also put the Map class address mapping information here.

Basic mapping:

See the operation

1 Mapping entity classes

//the importance of the default empty constructor Public classUser { PublicUser () {//TODO auto-generated Constructor stub    }         PublicUser (String id,string name) { This. id=ID;  This. name=name; }    PrivateString ID;  PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicDate Getcreatetime () {returnCreatetime; }     Public voidsetcreatetime (Date createtime) { This. Createtime =Createtime; }     PublicDate Getexprietime () {returnExprietime; }     Public voidsetexprietime (Date exprietime) { This. Exprietime =Exprietime; }    PrivateString name; PrivateString password; PrivateDate Createtime; PrivateDate exprietime;}

Design principles for entity classes:

* Implement a default constructor with no parameters

* Provide an identity

* It is not recommended to use the final modifier entity class (because the entity class generation proxy object is inherited when loading data with load delay)

* It is recommended to generate getter and setter methods for entity classes (if not used, attribute field identification is required)

2 mapping files User.hbm.xml

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://hibernate.sourceforge.ne T/hibernate-mapping-3.0.dtd "><hibernate-mapping> <!--1, class and table mappings, the Name property is the entity name, the Table property is the name of the tables (table can be omitted, then name is the mapped table name) -     <classname= "Com.bjpowernode.hibernate.User">    <!-2, primary key mapping, the name attribute is the identifier attribute of the entity class, corresponding to the table's primary key, which is denoted by column (column can also be omitted)-->          <IDname= "id"Access= "Field">            <!--The primary key generator, the class attribute represents the build strategy, and is selected according to different requirements -               <Generatorclass= "UUID"/>        </ID>         <!--3, other properties of the mapping property -          < Propertyname= "Name"length= "Max"Unique= "true" />        < Propertyname= "Password"/>        < Propertyname= "Createtime"/>        < Propertyname= "Exprietime"/>        <Filtername= "Testfiltere"condition= "ID <: myID"></Filter>    </class></hibernate-mapping>

3hibernate.cfg.xml configuration file

    <!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hiber Nate.sourceforge.net/hibernate-configuration-3.0.dtd ">        <hibernate-configuration>        <session-factory>            < Propertyname= "Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</ Property>            < Propertyname= "Hibernate.connection.url">Jdbc:mysql://localhost:3306/hibernast_test</ Property>            < Propertyname= "Hibernate.connection.username">Root</ Property>            < Propertyname= "Hibernate.connection.password">Hanhan</ Property>            < Propertyname= "Hibernate.dialect">Org.hibernate.dialect.MySQLDialect</ Property>            <!--Print SQL -            < Propertyname= "Hibernate.show_sql">True</ Property>            <!--in the absence of a table, when creating a Sessionfactroy, the table will be created (Update method, do not delete the original data) -            < Propertyname= "Hibernate.hbm2ddl.auto">Update</ Property>                    <MappingResource= "Com/bjpowernode/hibernate/user.hbm.xml"/>        </session-factory>    </hibernate-configuration>

Hibernate 's primary key generation policy type

Manual :Assigned

Hibernate active :uuid

Database Interaction:

Need to interact with the database to generate IDs: GUID, identity, sequence, native, foreign

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

Guid,identity:MySQL,How SQL Server is generated

Sequence:Oracle,DB2 generation mode, self-increment sequence

Native:identity+sequence, cross-platform

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.

Summarize:

Hibernate Basic Mapping: The focus is on the primary key generation strategy, according to different databases choose different ways, important understanding.

Hibernate base 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.