Java Tour Hibernate (8)--Basic relational mapping

Source: Internet
Author: User

What is the relationship, what is the mapping, the word of the relationship must be no stranger to everyone. For example, you and the teacher is the relationship between teachers and students, you and your parents are father or daughter (mother or daughter relationship).

Relationships are created between some kind of connected object. Anything is possible. For example, you and the tool, you can only use the tool, but the tool can not use you. This relationship is one-way. But in another way. You have the tools, the tools belong to you, then the relationship is two-way, in addition to a person can have multiple tools, and many tools can belong to one person. There are many people have multiple tools, and a number of tools belong to people, based on these characteristics, we will be divided into the following types of relationships:

  One-way relationship:

One2One, (one on one)

Many3one (many to one)

One2many (pair of many)

Many2many (Many-to-many)

  Bidirectional relationship:

One2many (pair of many)

Many2many (Many-to-many)

One2One (one to one)

Basic mapping Prerequisite Knowledge  

Files and entities that must exist

entity class (. java)  

The existence of an entity class is fundamental, because all we have to do is map the entity class to a table, build the entity class, and the primary key and the individual attribute fields before mapping.

Code for example with:

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;}

mapping File (*.hbm.xml)  

A mapping file is usually suffixed with a. Hbm.xml, for example, if we create a user entity, then our mapping file is User.hbm.xml, and the entity is mapped to a table and configured here.

Configuration such as the following:

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

Hibernate configuration file (Hibernate.cfg.xml)  

After we have established the entity and the mapping file. We should configure this in the configuration file, because the configuration file is the gateway to the entire interface and method, so this configuration is especially important.

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

This not only configures the database information, but also configures the mapping file so that the mapping file can be imported into the database through the tool class.

Tool Class  

This is our own hand-written tool class. Used to map an entity class into a table. and save to the database.

Import org.hibernate.cfg.configuration;import org.hibernate.tool.hbm2ddl.schemaexport;/** * Generate DDL for HBM * @author Administrator * */public class Exportdb {public static void main (string[] args) {//default read Hibernate.cfg.xml file configuration C FG = new Configuration (). Configure (); Schemaexport export = new Schemaexport (CFG); Export.create (true, True);}}

 Summary

Knowing the basic information above, it is easy for us to understand Hibernate's seven mapping relationships. The following article will be a one by one commentary on hibernate seven relationships.

Java Tour Hibernate (8)--Basic relational 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.