Relational mapping in the ORM advanced Hibernate

Source: Internet
Author: User

ORM Introduction to the advanced ORM

ORM advanced Hibernate Introduction and Framing

ORM the advanced Hibernate the three main objects

Analysis of three states of objects in ORM advanced Hibernate

One -to-one relationship mapping in ORM advanced Hibernate


Mapping can be said to be a very important content in hibernate, through mapping allows programmers to stop thinking complex SQL statements, but more focused on the implementation of business logic. The mapping is done through an XML configuration file and we can modify it! Let's take a look at how to complete the mapping!

Single Table Mapping

Each entity corresponds to a table and has no relationship with other entities, which is the simplest way to look at how entities are mapped to a database. The code of our entity user

<span style= "FONT-SIZE:18PX;" >/** * User Entity * @author Hongjie * */public class User {   //primary key   private int id;   User name   private String username;   Password   private String  passwords;   public int getId () {      return ID;   }   public void SetId (IntID) {      this.id = ID;   }   Public String GetUserName () {      return username;   }   public void Setusername (stringusername) {      this.username = username;   }   Public String getpasswords () {      return passwords;   }   public void Setpasswords (stringpasswords) {      this.passwords = passwords;   }} </span>

Code for the map file code User.hbm.xml

<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0"? ><! DOCTYPE hibernate-mappingpublic   "-//hibernate/hibernate Mapping dtd3.0//en"   "/HTTP/ Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd "><!--   package corresponding entity-->< Hibernate-mappingpackage= "Com.tgb.domain" ><!--table is the name of the tables mapped to the database, default to the entity name and   <class name= "User" table= "T_users" >      <id name= "id" type= "java.lang.Integer" >         <generator class= "native"/>      </id>      <property name= "username"/>      <property name= "passwords"/>   </class></ Hibernate-mapping></span>

With such a simple two configuration file, we can map our entities to the database! This is also the simplest way to map. But in practice, there are very few entities that are not related to each other.

Pair-to- one relationship mapping

There is a one-to-one relationship, such as a user can only have one ID card information, we want to separate the identity card information, so that our user entity and Identity card entity is a one-to-one relationship, it must be someone first, then have identity card information, so users should be the main, identity card information from, User maintainer ID information. Hibernate one-to-one relationship has two forms: a shared primary key approach, and the other is the only foreign key mode. How do we show this relationship? Take a look at the code.

shared primary key modeAs the name implies, is a person's two records using the same primary key, when we create a user, when creating the ID information, he will automatically fetch his corresponding user information of the primary key. This allows the same primary key to be used. let's look at the code below,
package com.tgb.domain;/** * User entity * @author Hongjie * */public class User {//primary key private int id;//User name private string username;//password private string passwords;//card information private card Card;public card Getcard () {return card;} public void Setcard (card) {this.card = card;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String Getpasswords () {return passwords;} public void Setpasswords (String passwords) {this.passwords = passwords;}} 
</pre><pre name= "code" class= "HTML" >public class Card {//primary key private int cardid;//ID information private String cardinfo;//User Information private users user;public user GetUser () {return user;} public void SetUser (user user) {this.user = user;} public int Getcardid () {return cardId;} public void Setcardid (int cardId) {this.cardid = cardId;} Public String Getcardinfo () {return cardinfo;} public void Setcardinfo (String cardinfo) {this.cardinfo = Cardinfo;}}
Above is the code of two entities, we can see in the user has a card reference, in the card also has a user reference, this is a two-way association, according to any one of the information can be taken to another information. Take a look at the code of our configuration file
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ HIBERNATE-MAPPING-3.0.DTD "><!--package-->
</pre><pre name= "code" class= "HTML" ><?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ HIBERNATE-MAPPING-3.0.DTD "><!--package-->
With the configuration file above, we created a one-to-one unique primary key affinity mapping. And we're looking at another way of doing it.Foreign Key Association. Foreign Key Associationwe Pojo class, or the top two class, we just modified the configuration file, the configuration file is as follows
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ HIBERNATE-MAPPING-3.0.DTD "><!--package-->
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--   package-->

We can see that we simply modified the configuration file and turned him into a unique foreign key constraint. This also enables one-to-one mapping. The next blog will explain the many-to-one mapping, please look forward to.




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

Relational mapping in the ORM advanced Hibernate

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.