1. Introduction to Collection Mapping
When there is a collection of value types in a persisted class, an additional database table is required to hold the data for this collection of value types, which is called the collection table. For example, there is a student table, students may have a lot of hobbies, you need a student Hobby collection table to store students ' hobbies information.
Hibernate supports most important JDK collection interface mappings, mainly in the following categories.
1. Set
You can map a property of type Java.util.Set interface whose elements are not in order and are not allowed to be duplicated, or a property of type Java.util.SortSet interface. Its elements can be sorted in natural order.
2. List
You can map a property of type Java.util.List interface, which requires the location of each element to be saved with an additional index column in the database table corresponding to the binding property.
3. Array
You can map types to an array of attributes and require additional indexing mechanism support, but it is rarely used in practical use.
4. Bag/idbag
You can map a property of type Java.util.Collection interface. Its elements can be duplicated but not saved in order, and no additional index support is required.
5. Map
You can map a property of type Java.util.Map interface, its elements are stored as key/value pairs, are unordered, or you can map a property of type Java.util.SortMap interface, and its elements can be sorted in natural order.
2. Set map use
- Data
CREATE TABLE ' Student '(' id 'bigint -) not NULLAuto_increment,' name ' varchar( -) not NULL,' age 'tinyint4) not NULL,' Sex ' bit(1) not NULL,' Address ' varchar( -) not NULL,' Cardid ' varchar( -) not NULL,PRIMARY KEY(' id ')) Engine=innodb auto_increment=5 DEFAULTCharset=utf8;CREATE TABLE ' Student_hobby '(' Id ' int( One) not NULLAuto_increment,' StudentID 'bigint -) not NULL,' ListIndex ' int( One) not NULL,' Description ' varchar( the)DEFAULT NULL,' Times ' int( One)DEFAULT NULL,' Level ' int( One)DEFAULT NULL,PRIMARY KEY(' Id '),KEY ' Fk_sid '(' StudentID '),CONSTRAINT ' Fk_sid ' FOREIGN KEY(' StudentID ')REFERENCES ' Student '(' id ') on UPDATE NO ACTION) Engine=innodb auto_increment=3 DEFAULTCharset=utf8;
<hibernate-mapping package ="COM.STUDY.HIBERNATE.HBM"> <class name="Student" table="Student" Catalog ="Hibernate"> <ID name="id" type="Java.lang.Long"> <column name="id" /> <generator class="Identity" /> </ID> < property name="name" type="string"> <column name= "name" length=" not-null" = "true" /> </Property > < property name=' age ' type=' byte '> <column name="Age" not-null="true" /> </Property > < property name="Sex" type="boolean"> <column name="Sex" not-null="true" /> </Property > < property name="Address" type="string"> <column name="Address" length=" not-null" = " True " /> </Property > < property name="Cardid" type="string"> <column name="Cardid" length=" not-null" = " True " /> </Property > <!--Here the set element is an entity, not a single element, you need to be aware of composite--> <set name="sethobbies" table="Student_hobby " Lazy="false"> <key column="StudentID" /> <!--<element column= "Desc" type= "string"/> -- <composite-element class="Studenthobbycomposite"> < property name="Times"></Property > < property name="description"></Property > < property name=' level '></Property > </composite-element> </Set> <list name="listhobbies" table="Student_hobby " Lazy="false"> <key column="StudentID"/> <list-index column="ListIndex" /> <element column="Description" type="string" /> </list> <array name="arrayhobbies" table="Student_hobby" Cascade ="persist"> <key column="StudentID"/> <list-index column="ListIndex" /> <element column="Description" type="string" /> </array> <bag name="baghobbies" table="Student_hobby" order-by ="level ASC" lazy="false"> <key column="StudentID"/> <element column="Description" type="string"/> </bag> <map name="maphobbies" table="Student_hobby " lazy= "false"> <key column="StudentID"/> <map-key column="Id" type="Java.lang.Integer" / > <element column="Description" type="string" /> </map> </class></hibernate-mapping>
Public class Student implements Java. io. Serializable { PrivateLong ID;PrivateString name;Private byteAgePrivate BooleanSexPrivateString address;PrivateString Cardid;PrivateSet sethobbies =NewHashSet (0);PrivateList listhobbies =NewArrayList (0);PrivateObject arrayhobbies[] =Newobject[0];PrivateList baghobbies =NewArrayList (0);PrivateMap maphobbies =NewHashMap (0);//Omit Get/set method} Public class studenthobbycomposite { PrivateInteger ID;PrivateString description;PrivateInteger times;PrivateInteger level;//Omit Get/set method, //Equal and Hashcode methods must be overridden}
3. Use problem set
Instance class equal and Hashcode methods corresponding to Composite-element
Composite-element the corresponding entity class, you must override the equal and Hashcode methods, otherwise the set element is stored as a new object each time, resulting in a delete operation on the collection table for each get. The insert operation is then performed.
Use of List/array base
The starting subscript for List/array is 0, which should be noted that if the starting position of index in the set table is not 0, it will cause a problem with the null element in the List/array, at which point we can use base to synchronize the subscript of index in the collection table. Or the lower mark of index in the collection table starts at 0.
A little bit different from the relationship map
When using a collection map, we find that the same attributes as Set/list are used in the relational mappings, one-to-many in the relational mappings, and the same as the set mappings, except that the collection table is not required when using the collection map, but the other side of the relationship map must have a configuration file , this may be the biggest difference from one-way-to-many relationship mappings.
Hibernate mapping Relationship-collection mappings