hibernate List,Set,Map集合的配置实例

  1. List,Set,Map的配置实例
  2. OR映射的设置
  3. 一个对象对应一张表
  4. User    <-----> Person1
  5. 一个对象对应两张表
  6. User    <-----> Person1 , Phone1
  7. Person1 :ID ,Name
  8.     
  9. Phone1 :PersonID PK,PhoneCode PK
  10. 自然主键或物理主键
  11. 在User对象中需要添加一个映射Phone1表的属性(集合属性)
  12. 集合属性有三种:List,Set,Map
  13. List  有元素下标
  14. Map  有键值
  15. Set   没有重复值即可
  16. Phone1 :PersonID PK,PhoneCode PK  ---> Set
  17. Phone2 :PersonID PK,  PhoneOrder PK , PhoneCode  ---> List
  18. Phone3 : PersonID PK,  PhoneType PK , PhoneCode ----> Map
  19. 下面是Set集合
  20. <hibernate-mapping>
  21.     <class name="my.User" table="Person1" schema="dbo" catalog="Demo">
  22.         <id name="uid" type="int">
  23.             <column name="ID" />
  24.             <generator class="assigned"></generator>
  25.         </id>
  26.         <property name="uname" type="string">
  27.             <column name="Name" length="50" not-null="true" />
  28.         </property>
  29.         <set name="phoneSet" table="Phone1" >
  30.             <key column="PersonID" />
  31.             <element column="PhoneCode" type="string" />
  32.         </set>
  33.     </class>
  34. </hibernate-mapping>
  35. 下面是List集合
  36. <hibernate-mapping package="my" >
  37. <class name="my.User" table="Person1" >
  38.         <id name="uid" column="ID" type="int" >
  39.             <generator class="assigned" />
  40.         </id>
  41.         <property name="uname" column="Name" type="string" length="50" />
  42.         <list name="phones" table="Phone1">
  43.             <key column="PID" />     //联合主键PID,ORD 其中ORD是索引列如果使用
  44. list集合就必须手动添加该列
  45.             <index column="ORD" />
  46.             <element column="Code" type="string" length="50"  />
  47.         </list>
  48.     </class>
  49. </hibernate-mapping>
  50. 下面是Map集合
  51. <hibernate-mapping package="my" >
  52.     <class name="User" table="Person1" >
  53.         <id name="uid" column="ID" type="int" >
  54.             <generator class="assigned" />
  55.         </id>
  56.         <property name="uname" column="Name" type="string" length="50" />
  57.         <map name="phones"  table="Phone3"  cascade="save-update" inverse="true" >
  58.             <key column="PID"  />
  59.             <map-key column="Type" type="string"  />
  60.             <element column="Code" type="string" />
  61.         </map>  
  62.     </class>
  63. </hibernate-mapping>
  64. User.java
  65. public class User {
  66.     
  67.     private int uid;
  68.     private String uname;
  69.     
  70.     //private List phones = new ArrayList();
  71.     //private Set phones = new HashSet();
  72.     private Map phones=new Hashtable();
  73.     
  74.     public User(){}
  75.     
  76.     public User(int id,String name){
  77.         this.setUid(id);
  78.         this.setUname(name);
  79.         
  80.     }
  81.     
  82.     public int getUid() {
  83.         return uid;
  84.     }
  85.     public void setUid(int uid) {
  86.         this.uid = uid;
  87.     }
  88.     public String getUname() {
  89.         return uname;
  90.     }
  91.     public void setUname(String uname) {
  92.         this.uname = uname;
  93.     }
  94.     public Map getPhones() {
  95.         return phones;
  96.     }
  97.     public void setPhones(Map phones) {
  98.         this.phones = phones;
  99.     }   
  100. }
  101. Test.java
  102. public class Test {
  103.     @SuppressWarnings("unchecked")
  104.     public static void main(String[] args) {
  105.         
  106.         //1:读配置文件
  107.         Configuration config=new Configuration().configure();
  108.         //2:创建会话工厂
  109.         SessionFactory sf=config.buildSessionFactory();
  110.         //3:获取会话对象
  111.         Session se=sf.openSession();
  112.         /*
  113.                 -----List--------
  114.                 User user=(User)se.get(User.class, 1);
  115.         List phones=new ArrayList();
  116.         phones.add("022-12345678");
  117.         phones.add("13812345678");
  118.         user.setPhones(phones);
  119.         se.beginTransaction().commit();
  120.                 -----Set--------
  121.                 User u=new User(4,"ddd");
  122.         Set phoneSet=new HashSet();
  123.         phoneSet.add("022-21331111");
  124.         phoneSet.add("13511112222");
  125.         u.setPhoneSet(phoneSet);
  126.                 op.AddUser(u);
  127.                 */
  128.         //4:保存,修改和删除一条记录
  129.         User u=new User(3,"ccc");
  130.         Map phoneMap=new Hashtable();
  131.         phoneMap.put("家庭电话""12345678");
  132.         phoneMap.put("工作电话""22222222");
  133.         u.setPhones(phoneMap);
  134.         addOneUser(se,u);
  135.         //5:
  136. //      Phone p=(Phone)se.get(Phone.class, 4);
  137. //      System.out.println(p.getSn()+":"+p.getCode()+","+p.getUser().getUname());
  138.         showAllUser(se);        
  139.     }
  140.     
  141.     public static void delOneUser(Session se,User user){
  142.         //4.2 删除一条记录
  143.         se.delete(user);
  144.         se.beginTransaction().commit();
  145.     }   
  146.     
  147.     public static void updOneUser(Session se,User user){
  148.         //4.2 修改一条记录
  149.         se.update(user);
  150.         se.beginTransaction().commit();
  151.     }   
  152.     
  153.     
  154.     public static void addOneUser(Session se,User user){
  155.         //4.2 插入一条记录
  156.         se.save(user);
  157.         se.beginTransaction().commit();
  158.     }
  159.     
  160.     public static void showAllUser(Session se){
  161.         //4:获取查询对象
  162.         Query query=se.createQuery("from User");
  163.         //5:获取查询结果
  164.         List list= query.list();
  165.         //6:处理查询结果
  166.         
  167. //      当集合是Map时
  168.         
  169.         for(Iterator iter=list.iterator();iter.hasNext();){
  170.         User temp=(User)iter.next();
  171.         Map phoneMap=temp.getPhones();
  172.         String strPhones="";
  173.         Iterator ite= phoneMap.values().iterator();
  174.         while(ite.hasNext()){
  175.             String phone =(String)ite.next();
  176.             strPhones += phone+",";
  177.         }
  178.         System.out.println(temp.getUid()+":"+temp.getUname()+","+strPhones);
  179.         
  180.         }
  181. //      当集合是Set时
  182.         
  183. //      for(Iterator iter=list.iterator();iter.hasNext();){
  184. //          User temp=(User)iter.next();
  185. //          Set phoneSet=temp.getPhones();
  186. //          String strPhones="";
  187. //          Iterator ite= phoneSet.iterator();
  188. //          while(ite.hasNext()){
  189. //              Phone phone=(Phone)ite.next();
  190. //              strPhones += phone.getCode()+",";
  191. //          }
  192. //          System.out.println(temp.getUid()+":"+temp.getUname()
  193. +","+strPhones);
  194. //          
  195. //      }
  196.     }
  197. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值