Hibernate ing- ing (one-to-one)

Source: Internet
Author: User

Hibernate ing- ing (one-to-one)

A one-to-one relationship is a common relationship in database design. For example, the relationship between the user and userbasic tables is a one-to-one relationship. Based on the different configuration methods of primary keys in database design, the implementation of one-to-one relational database in hibernate is also different, mainly in the following two ways.

1. Primary Key Association

1. Database Design

Create table 'user' ('id' bigint (20) not null AUTO_INCREMENT, 'name' varchar (64) not null, 'email 'varchar (64) not null, 'Phone 'char (11) not null, primary key ('id') ENGINE = InnoDB AUTO_INCREMENT = 10 default charset = utf8; # The primary key of userbasic is used as the primary key of the user associated with the foreign key, that is, the primary keys of both are consistent with create table 'userbasic '('id' bigint (20) not null default '0 ', 'age' tinyint (4) default null, 'address' varchar (128) default null, 'sex' bit (1) default null, primary key ('id '), CONSTRAINT 'fk _ userid' foreign key ('id') REFERENCES 'user' ('id') on update no action) ENGINE = InnoDB default charset = utf8;

2. Configuration File


      
           
                
                 
       
      
     
            
                
             
     
            
                
             
     
            
                
             
     
            
        
   
  
      
           
            
                
                 
      
       User
              
     
            
            
                
             
     
            
                
             
     
            
                
             
     
        
   
  

3. Persistence class

Public class User implements java. io. serializable {private Long id; private String name; private String email; private String phone; private Userbasic userbasic; // omit get/set Method} public class Userbasic implements java. io. serializable {private long id; private User user; private Byte age; private String address; private Boolean sex; // The get/set method is omitted}

4. Summary
Get Method
1) when user get is performed, fetch = join is used by default to obtain userbasic. Delayed loading of userbasic is not supported.
2) The delay loading method is used by default when userbasic get is used, but you can set lazy = false or fetch = join to load immediately.
Save Method
1) The userbasic object can be disabled by default when the user saves the file. To support cascading save, set the cascade attribute to all or other attribute values.
2) The user object must be set during userbasic save, and the user must be saved before saving userbasic because the primary key of userbasic is obtained from the user, by default, the user is saved in cascade mode, and the cascade attribute does not need to be set.
Delete Method
1) user deletion must delete userbasic at the same time. Otherwise, the operation may fail due to foreign key constraints. user deletion must be performed cascade deletion of userbasic in two ways, first, you can set the database deletion constraint to the cascade attribute, and second, set the cascade attribute to all.
2) userbasic deletes only the user by default. To perform cascade deletion, you can set the cascade attribute to all.
Update Method
1) by default, user update does not affect userbasic. If the cascade attribute is set to all, userbasic will be updated cascade.
2) updates to userbasic do not affect the user, even if the cascade attribute is set to all

2. Foreign key Association

1. Database Design

# The user TABLE has a foreign key associated with userbasicCREATE TABLE 'user' ('id' bigint (20) not null AUTO_INCREMENT, 'ubid' bigint (20) not null, 'name' varchar (64) not null, 'email 'varchar (64) not null, 'phone' char (11) not null, primary key ('id '), KEY 'fk _ B '('ubid'), CONSTRAINT 'fk _ B' FOREIGN KEY ('ubid') REFERENCES 'userbasic '('id ')) ENGINE = InnoDB AUTO_INCREMENT = 21 default charset = utf8; create table 'userbasic '('id' bigint (20) not null AUTO_INCREMENT, 'age' tinyint (4) default null, 'address' varchar (128) default null, 'sex' bit (1) default null, primary key ('id') ENGINE = InnoDB AUTO_INCREMENT = 21 default charset = utf8;

2. Configuration File


      
           
                
                 
       
      
     
            
            
                
             
     
            
                
             
     
            
                
             
     
            
                
             
     
        
   
  
      
           
                
                 
       
      
     
            
                
             
     
            
                
             
     
            
                
             
     
            
            
        
   
  

3. Persistence class

Public class User implements java. io. serializable {private Long id; private Userbasic userbasic; private String name; private String email; private String phone; // omit get/set Method} public class Userbasic implements java. io. serializable {private Long id; private Byte age; private String address; private Boolean sex; private User user; // The get/set method is omitted}

4. Summary
Use of property-ref
1) when the primary key is associated, the configuration of one-to-one is name = user class = User, and when the foreign key is associated, one-to-one, instead of using the class, we changed it to property-ref. If a class is used, the following code is used for association query:

userbasic1_.id=user2_.id

This is obviously not the correct association condition. The correct association condition is userbasic. id = user. uid. Only when the class is changed to property-ref = userbasic can the correct association condition be obtained. The following is the correct association condition code:

userbasic0_.id=user1_.ubid

2) The role of property-ref is to specify the associated property either a single field or an object when associating with an object.
Get Method
1) by default, get loads are delayed on both sides. You can set lazy to false to load immediately.
2) The default capture policy of one end is join, and the default fetch policy of the other end is select. You can modify the attribute of fetch to join.
Save Method
1) The one end must exist when the producer side saves. When one does not have a record in the database, it must support cascade, that is, cascade needs to be set to all and other attributes. When one has a record in the database, you can first find and set the cascade attribute, then save
2) There are no restrictions on saving on one end. They can be stored separately or cascade storage. cascade attributes must be set.
Delete Method
1) userbasic must delete the user at the same time. Otherwise, the user cannot be deleted due to foreign key constraints, you can set the database deletion constraint to the cascade attribute or userbasic to set the cascade attribute to all.
2) by default, only the user is deleted. To perform cascade deletion, set the cascade attribute to all.
Update Method
1) by default, only the user is updated. After cascade is set, userbasic is also updated.
2) by default, only userbasic is updated. After cascade is set, userbasic is also updated.

3. Link direction

One-to-one relationship has two directions: one-to-one and two-way. Generally, one-to-one and two-way Association are based on the actual needs of the project. We can remove the link ing at one end, change it to basic property ing to implement one-way Association.
We recommend that you use a foreign key association method to implement one-to-one ing, which is more flexible.

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.