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