Hibernate ing-set ing

Source: Internet
Author: User

Hibernate ing-set ing
1. Introduction to set ing

When a value type set exists in the persistence class, an additional database table is required to store the data of this value type set. This table is called a collection table. For example, if a student table has many hobbies, you need a set of hobbies to store students' hobbies.
Hibernate supports most of the important JDK set interface ing, mainly including the following types.
1. set
The ing type can be the property of the java. util. Set interface. Its elements are stored in an order and cannot be duplicated. You can also map the property of the java. util. SortSet interface. Its elements can be arranged in natural order.
2. list
The ing type can be the property of the java. util. List interface. It needs to save the location of each element with an additional index column in the database table corresponding to the property.
3. array
The ing type can be an array attribute and requires additional indexing mechanisms, but it is rarely used in practical use.
4. bag/idbag
The ing type can be the attributes of the java. util. Collection interface. Its elements can be repeated, but they do not save the order. No additional indexes are required.
5. map
The ing type can be java. util. the attributes of the Map interface. Its elements are saved in the form of key/value pairs, which are unordered. You can also Map the elements to java. util. sortMap interface attributes. Its elements can be sorted in natural order.

2. Set ing Using Data Structures
CREATE TABLE `student` (  `id` bigint(20) NOT NULL AUTO_INCREMENT,  `name` varchar(64) NOT NULL,  `age` tinyint(4) NOT NULL,  `sex` bit(1) NOT NULL,  `address` varchar(128) NOT NULL,  `cardid` varchar(128) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;CREATE TABLE `student_hobby` (  `Id` int(11) NOT NULL AUTO_INCREMENT,  `StudentId` bigint(20) NOT NULL,  `ListIndex` int(11) NOT NULL,  `Description` varchar(256) DEFAULT NULL,  `Times` int(11) DEFAULT NULL,  `Level` int(11) 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 DEFAULT CHARSET=utf8;
Configuration File

      
           
                
                 
       
      
     
            
                
             
     
            
                
             
     
            
                
             
     
            
                
             
     
            
                
             
     
            
            
                
                 
                  
       
        
        
        
              
     
            
                  
                   
       
        
       
      
     
                         
                
                 
       
      
     
            
                
                 
       
      
     
    
                  
                   
       
        
       
      
         
   
  
Object Class Structure
Public class Student implements java. io. serializable {private Long id; private String name; private byte age; private boolean sex; private String address; private String cardid; private Set setHobbies = new HashSet (0 ); private List listHobbies = new ArrayList (0); private Object arrayHobbies [] = new Object [0]; private List bagHobbies = new ArrayList (0 ); private Map mapHobbies = new HashMap (0); // omit get/set Method} public class StudentHobbyComposite {private Integer id; private String description; private Integer times; private Integer level; // The get/set method is omitted. // the equal and hashcode methods must be overwritten}
3. Collection of use problems

The instance class equal and hashcode methods corresponding to composite-element
The entity class corresponding to composite-element must override the equal and hashcode methods. Otherwise, it will be treated as a new object every time it is stored as a set element, as a result, each get operation will first perform the delete operation on the set table, and then execute the insert operation.

Use of list/array base
The starting subscript of list/array is 0. Note that if the starting position of Index in the set table is not 0, the list/array has a NULL element, at this time, we can use base to synchronize the Index subscript of the set table, or remember to start from 0 as the Index subscript of the set table.

A little difference from link ing
When using set ing, we found that the use of set/list and other attributes in the relationship ing is similar. The one-to-many relationship ing is the same as the set ing, the only difference is that when set ing is used, the set table does not need a configuration file. However, when link ing is used, the multiple parties must have a configuration file, this may be the biggest difference from one-to-multiple link ing.

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.