"Hibernate Learning Notes" mapping aggregation relationships and combinatorial relationships

Source: Internet
Author: User

Aggregation: The relationship between the whole and the part, which can be separated from the whole. such as Wild Goose and wild goose flock, wild goose leaves Wild Goose Group, also can exist alone.

Solid line with a hollow diamond in the UML notation.

The arrow points to the Hollow diamond pointing to the whole.



Combination: The relationship between the whole and the part, but the part cannot leave the whole and exist alone. such as birds and wings, the wings cannot exist as individual individuals.

Solid line with a solid diamond in the UML notation.

The arrow points to a solid diamond pointing to the whole.

Original address: http://blog.csdn.net/liuyanlinglanq/article/details/8096164




Aggregation (aggregation) Relation: A special case of association relation, is strong association relation. Aggregation is the relationship between the whole and the individual, that is, the relationship between the whole and the part is separable, they can have their own life cycle, some can belong to a number of whole objects, can also be has-a for a number of whole objects, such as computer and CPU, car and wheel, company and employee relations, etc. , in the code level, and the relationship is consistent, can only be differentiated from the semantic level;

An aggregation relationship is also implemented using an instance variable. There is no association or aggregation from Java syntax.

The two classes in the association relationship are at the same level, and the two classes in the aggregation relationship are at an unequal level, one representing the whole and one representing part.




Combinatorial (synthetic) relationship (composition): is also a special case of the relationship, he embodies a kind of contains-a relationship, which is stronger than the aggregation, also known as strong polymerization; he also embodies the relationship between the whole and the part, but at this time the whole and the part are not divided, The end of the whole life cycle means that part of the life cycle is over, like you and your brain; Synthetic relationships cannot be shared. 。 At the code level, the relationship is consistent and can only be differentiated from the semantic level.

Combination is almost the same as aggregation, the only difference is that "part" can not be separated from the "whole" alone, that is, "part" of life can not be longer than the "whole".


Original address: http://justsee.iteye.com/blog/808799





The person class and address class are aggregation relationships:

Suppose we want to define these two objects, for everyone, he has an associated address. The relationship between person and address is has-a. However, we cannot say that this address is an integral part of this person. At the same time, we can set up the address object and the human object to be relatively independent.

In code, the typical code style is as follows:

public class Address {...}        public class Person {private address address;        public person [address] {this.address = address; }        . . .   }

We usually use the person object in the following ways:

Address = new address (); person who = new person (address);

Or:

person who = new person (new address);

We can see that we created a separate address object and then passed this object into the constructor of person. When the person object declares the end of the cycle, the address object may continue to exist if there are other references to it. In other words, their declaration cycle is relatively independent. Original address: http://developer.51cto.com/art/201201/311219.htm




"Mapping Combinatorial Relationships"

Name.java

Package mypack;

public class Name {
	private String firstName;
	Private String lastName;
	
	Public name ()
	{
		
	} public
	
	name (string firstName, String lastName) {
		super ();
		This.firstname = FirstName;
		This.lastname = LastName;
	}
	
	Public String Getfirstname () {return
		firstName;
	}
	public void Setfirstname (String firstName) {
		this.firstname = firstName;
	}
	Public String Getlastname () {return
		lastName;
	}
	public void Setlastname (String lastName) {
		this.lastname = lastName;
	}
	
}


Customer.java

Package mypack;

Import java.sql.Date;
Import Java.sql.Timestamp;
Import Java.util.HashSet;
Import Java.util.Set;

public class Customer {
	private Long ID;
	private name name;
	private String Email;
	private String password;
	private int phone;
	Private Boolean married;
	Private String address;
	Private Character sex;
	Private String description;
	Private byte[] image;
	Private Date birthday;
	Private Timestamp registeredtime;
	Private set<order> orders = new hashset<order> ();
			
	Public Long GetId () {return
		ID;
	}
	public void SetId (Long id) {
		this.id = ID;
	}
	Public name GetName () {return
		name;
	}
	public void SetName (name name) {
		this.name = name;
	}
	...
}


Hibernate divides the attributes in the persisted class into two types: value type and entity (Entity) type.

Value type has no OID. Cannot be persisted individually, its lifecycle depends on the lifecycle of the object of the persisted class to which it belongs, such as ID (long), email (string), name (component type) is a value type property, and the entity type has an OID that can be persisted individually. such as the order object in the Orders collection is an entity type.


Customer.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 ">



database table Customers:




"Mapping Aggregation Relationships"

In the presence of data redundancy, coarse-grained tables need to be split into fine-grained tables with foreign key reference relationships, thus saving storage space; (persistence classes, mappings, database tables, etc., consistent with association relationships)

Without data redundancy, you should reduce the number of tables as much as possible and simplify the referential relationships between tables in order to improve the speed of accessing the database. (Persistent classes, mappings, database tables, and so on, and combination relationships)


There are different starting points for establishing domain model and relational data model. The domain model is composed of program code, which can improve the reusability of code and simplify programming by refining the granularity of the persisted class. The tradeoff between saving data storage space and saving data manipulation time is needed when establishing relational data model.


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.