Hibernate Framework Learning (iii) relational mapping

Source: Internet
Author: User
Tags object model set set table name

Relational mapping in Hibernate refers to the relationship between an entity class and an entity class. and the database table and the relationship between the table is similar to one-to-one, many pairs of a, a lot, many to more than four mapping relationship.

One: Mapping

Two objects are a one-to-ones relationship, as between a person and an identity card.

There are two ways to implement one-to-one mapping, a PRIMARY KEY constraint and a FOREIGN KEY constraint

1, PRIMARY KEY constraint

Database tables do not have additional fields to maintain relationships between them, only through the table's primary key to associate

1) object Model

2) Relational model


2, FOREIGN KEY constraint

A property that joins another object in one object

1) object Model


2) Relational model

3) configuration in XML

Since the Idcard ID entity class is introduced, its configuration file does not change, the person class configuration file needs to do the corresponding configuration, but also because in the human section can see the card information, configuration information, need to pay attention to the introduction of the party using the Many-to-one tag

Main: The Name property in the Many-to-one tag is preceded by the Idcard object primary key field plus the Get field.


Two: many to one

Many-to-one mapping, such as the relationship between students and classes, multiple students correspond to a class.

1, the object model is a reference to a party object that is added to one of the many party objects. The configuration file is shown in the figure above.

2, annotations use

@Entity public
class Person {
	private int id;
	private String name;
	Private Idcard CID;

	@ManyToOne
	@JoinColumn (name= "Cardid") public
	Idcard Getcid () {
		return cid;
	}

Use @joincolumn to specify the field name of the foreign key

Three: A pair of more

A one-to-many mapping, such as the relationship between classes and students, has multiple students in a class.

1, the object model is to add a set set at one end, containing many of the end.

public class classes{
	
	private int cid;
	Private String CNAME;
	
	private Set<student> Studentset = new hashset<student> ();//one-to-many public
	
	int getcid () {
		return CID;
	}
	public void Setcid (int cid) {
		this.cid = cid;
	}
	Public String Getcname () {
		return cname;
	}
	public void Setcname (String cname) {
		this.cname = CNAME;
	}
	Public set<student> Getstudentset () {
		return studentset;
	}
	public void Setstudentset (set<student> studentset) {
		this.studentset = studentset;
	}
}

2, configuration file

1) At one end of the course, use the set label and use the One-to-many tag


2) At one end of the student, use the many-to-one tag


3, annotations

	@OneToMany//Annotate a one-to-many relationship
	@JoinColumn (name= "Classesid")//Annotate a field (named Classessid) public set<student at a multiple end
	> getstudents () {
		return students;
        }

Use @onetomany annotations before the Get method of the Set collection property


Three: Many-to-many

Many-to-many mapping relationships, but also the student curriculum relationship, multiple students corresponding to multiple courses, multiple courses corresponding to multiple students

1, in the object model, both parties join the set set

2,xml configuration file, both sides also use Set label and Many-to-many tag

3, annotations

     @ManyToMany
	    @JoinTable (name= "T_s_c",//Use the Name property of the @jointable tag to annotate the third-party table name
			joincolumns={@JoinColumn (name= " CourseID ")},
     //Use the Joincolumns property to annotate the field name of the current entity class in the third-party table and point to the object
			inversejoincolumns={@JoinColumn (name=" StudentID ")}	
    //Use the Inversejoincolumns property to annotate the current entity class holding the field name of the reference object in the third-party table and pointing to the referenced object table
	) public
	set<student > getstudents () {return Students;}


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.