Hibernate ing set attributes

Source: Internet
Author: User

Hibernate ing set attributes

Here we store the Set in the database first. A new table is created. The primary key of the newly created table corresponds to another foreign key, and each value of the set corresponds to a record. Data in the same set is distinguished by records with the same primary key value.

 

package test.hibernate.set;import java.util.Set;public class User {private Integer id;private String name;private Set
 
   addressSet;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set
  
    getAddressSet() {return addressSet;}public void setAddressSet(Set
   
     addressSet) {this.addressSet = addressSet;}}
   
  
 

Package test. hibernate. set; import java. util. hashSet; import java. util. set; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import org. junit. test; public class App {private static SessionFactory sessionFactory = new Configuration ()//. configure ()//. addClass (User. class) // Add the Hibernate object class (load the corresponding ing file ). buildSessionFactory (); @ Testpublic void testSave () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // ---------------------------------------------- // build Object User = new user (); User. setName ("Li Ming"); Set
 
  
Set = new HashSet
  
   
(); Set. add ("Guangdong Province"); set. add ("Shenzhen City"); set. add ("Futian district"); user. setAddressSet (set); // Save the session. save (user); // ------------------------------------------------ session. getTransaction (). commit (); session. close () ;}@ Testpublic void testGet () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // obtain the data User user = (User) session. get (User. class, 1); // displays the data System. out. println (user. getId (); System. out. println (user. getName (); System. out. println (user. getAddressSet (); session. getTransaction (). commit (); session. close ();}}
  
 

 
 
  
      
    
   
   
    
       
        
     
    
   
  
 
Note the set ing.

 

Results of loading twice in a row

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://blog.csdn.net/lindonglian

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.