Hibernate ing-composite primary key, Component, set ing

Source: Internet
Author: User

Composite primary key ing
Multiple Fields form the primary key, which is rarely used in reality. Generally, the fields related to the primary key are taken out to form a class separately as attributes, and the geter and seter methods are generated, the most important thing is that this class must implement the serialization interface and overwrite the equals and hashcode methods.
For example, FiscalYearPeriod consists of a composite primary key. Code:
[Java]
Package com. snil. hibernate;
 
Import java. util. Date;
 
/**
* Accounting period
* @ Author yuanfubiao
*/
Public class FiscalYearPeriod {
 
Private FiscalYearPeriodPK fiscalYearPeriodPK;
 
// Start date
Private Date beginDate;

// End date
Private Date endDate;

// Status
Private String periodSts;
 
Public FiscalYearPeriodPK getFiscalYearPeriodPK (){
Return fiscalYearPeriodPK;
}
 
Public void setFiscalYearPeriodPK (FiscalYearPeriodPK fiscalYearPeriodPK ){
This. fiscalYearPeriodPK = fiscalYearPeriodPK;
}

Public Date getBeginDate (){
Return beginDate;
}
 
Public void setBeginDate (Date beginDate ){
This. beginDate = beginDate;
}
 
Public Date getEndDate (){
Return endDate;
}
 
Public void setEndDate (Date endDate ){
This. endDate = endDate;
}
 
Public String getPeriodSts (){
Return periodSts;
}
 
Public void setPeriodSts (String periodSts ){
This. periodSts = periodSts;
}
}
The separate class FiscalYearPeriodPK composed of primary key-related fields. Code:
[Java]
Package com. snil. hibernate;
 
Import java. io. Serializable;
 
Public class FiscalYearPeriodPK implements Serializable {

Private int fiscalYear;

Private int fiscalPeriod;
 
@ Override
Public int hashCode (){
Final int prime = 31;
Int result = 1;
Result = prime * result + fiscalPeriod;
Result = prime * result + fiscalYear;
Return result;
}
 
@ Override
Public boolean equals (Object obj ){
If (this = obj)
Return true;
If (obj = null)
Return false;
If (getClass ()! = Obj. getClass ())
Return false;
FiscalYearPeriodPK other = (FiscalYearPeriodPK) obj;
If (fiscalPeriod! = Other. fiscalPeriod)
Return false;
If (fiscalYear! = Other. fiscalYear)
Return false;
Return true;
}
 
Public int getFiscalYear (){
Return fiscalYear;
}
 
Public void setFiscalYear (int fiscalYear ){
This. fiscalYear = fiscalYear;
}
 
Public int getFiscalPeriod (){
Return fiscalPeriod;
}
 
Public void setFiscalPeriod (int fiscalPeriod ){
This. fiscalPeriod = fiscalPeriod;
}
}
The fising file FiscalYearPeriod. hbm. xml of the FiscalYearPeriod object class.
[Html]
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "FiscalYearPeriod" table = "t_fiscal_year_period">
<Composite-id name = "fiscalYearPeriodPK">
<Key-property name = "fiscalYear"/>
<Key-property name = "fiscalPeriod"/>
</Composite-id>
<Property name = "beginDate" type = "date"/>
<Property name = "endDate" type = "date"/>
<Property name = "periodSts"/>
</Class>
</Hibernate-mapping>
The composite-id tag maps two composite primary keys of the FiscalYearPeriod class. In the object model, FiscalYearPeriod holds the reference of FiscalYearPeriodPK.
Component ing
It is also called component ing. In the object model, in order to consider the reuse rate, the design class generally starts from the fine granularity and tries its best to make the meaning clear and hierarchical. The relational model design is the opposite of the object model. For performance, convenience, and other factors, coarse granularity is generally used.
Example:

The ing file between User and Employee.

[Html]
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "User" table = "t_user">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Property name = "name"/>
<Component name = "userContact">
<Property name = "email"/>
<Property name = "address"/>
<Property name = "zipCode"/>
<Property name = "contactTel"/>
</Component>
</Class>
</Hibernate-mapping>
[Html] view plaincopy
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "Employee" table = "t_employee">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Property name = "name"/>
<Component name = "employeeContact">
<Property name = "email"/>
<Property name = "address"/>
<Property name = "zipCode"/>
<Property name = "contactTel"/>
</Component>
</Class>
</Hibernate-mapping>
From the perspective of object model, Contact is a logical Component of User and Employee. The two entity classes hold their references respectively. The main difference between Contact and the object class is that there is no id, Component (Contact) in DDD, it is called a value class.
From the perspective of the relational model, it is finally divided into two tables to store their respective data, which is finer than the granularity of the object model. The <Component> label is used to complete the Component ing.
Set ing
The set ing of set, list, array, and map is completed below. The principle is to map a set into a table.

Object Class Object:
[Java]
Package com. snil. hibernate;
 
Import java. util. ArrayList;
Import java. util. HashSet;
Import java. util. List;
Import java. util. Map;
Import java. util. Set;
 
@ SuppressWarnings ("rawtypes ")
Public class CollectionMapping {
 
Private int id;

Private String name;

Private Set setValues = new HashSet ();


Private List listValues = new ArrayList ();

Private String [] arrayValues;

Private Map mapValues;
 
Public int getId (){
Return id;
}
 
Public void setId (int id ){
This. id = id;
}
 
Public String getName (){
Return name;
}
 
Public void setName (String name ){
This. name = name;
}
 
Public Set getSetValues (){
Return setValues;
}
 
Public void setSetValues (Set setValues ){
This. setValues = setValues;
}
 
Public List getListValues (){
Return listValues;
}
 
Public void setListValues (List listValues ){
This. listValues = listValues;
}
 
Public String [] getArrayValues (){
Return arrayValues;
}
 
Public void setArrayValues (String [] arrayValues ){
This. arrayValues = arrayValues;
}
 
Public Map getMapValues (){
Return mapValues;
}
 
Public void setMapValues (Map mapValues ){
This. mapValues = mapValues;
}
}
Ing file:
[Html]
<Hibernate-mapping package = "com. snail Il. hibernate">
<Class name = "CollectionMapping" table = "t_collection_mapping">
<Id name = "id">
<Generator class = "native"/>
</Id>
<Property name = "name"/>
<Set name = "setValues" table = "t_set_values">
<Key column = "set_id"/>
<Element type = "string" column = "set_value" not-null = "true"/>
</Set>
<List name = "listValues" table = "t_list_values">
<Key column = "list_id"/>
<List-index column = "list_index"/>
<Element type = "string" column = "list_value"/>
</List>
<Array name = "arrayValues" table = "t_array_values">
<Key column = "array_id"/>
<List-index column = "array_index"/>
<Element type = "string" column = "array_value"/>
</Array>
<Map name = "mapValues" table = "t_map_values">
<Key column = "map_id"/>
<Map-key type = "string" column = "map_key"/>
<Element type = "string" column = "map_value"/>
</Map>
</Class>
</Hibernate-mapping>
Author: StubbornPotatoes

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.