zoukankan      html  css  js  c++  java
  • Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)

    1.

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping
     3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 <hibernate-mapping >
     6 
     7   <class name="mypack.Monkey" table="MONKEYS" >
     8     <id name="id" type="long" column="ID">
     9       <generator class="increment"/>
    10     </id>
    11 
    12     <property name="name" type="string" >
    13         <column name="NAME" length="15" />
    14     </property>
    15 
    16    <property name="age" type="int" >
    17         <column name="AGE" />
    18    </property>
    19 
    20    <list   name="images"   table="IMAGES"    lazy="true">
    21         <key column="MONKEY_ID" />
    22         <list-index column="POSITION" />
    23         <element column="FILENAME" type="string"  not-null="true"/>
    24    </list>   
    25 
    26   </class>
    27  
    28 </hibernate-mapping>

    2.

     1 package mypack;
     2 
     3 import java.io.Serializable;
     4 import java.util.List;
     5 import java.util.ArrayList;
     6 
     7 public class Monkey implements Serializable {
     8     private Long id;
     9     private String name;
    10     private int age;
    11     private List images=new ArrayList();
    12 
    13     /** full constructor */
    14     public Monkey(String name, int age,List images) {
    15         this.name = name;
    16         this.age=age;
    17         this.images = images;
    18     }
    19 
    20     /** default constructor */
    21     public Monkey() {
    22     }
    23 
    24     /** minimal constructor */
    25     public Monkey(List images) {
    26         this.images = images;
    27     }
    28 
    29     public Long getId() {
    30         return this.id;
    31     }
    32 
    33     public void setId(Long id) {
    34         this.id = id;
    35     }
    36 
    37     public String getName() {
    38         return this.name;
    39     }
    40 
    41     public void setName(String name) {
    42         this.name = name;
    43     }
    44 
    45     public int getAge() {
    46         return this.age;
    47     }
    48 
    49     public void setAge(int age) {
    50         this.age = age;
    51     }
    52 
    53     public List getImages() {
    54         return this.images;
    55     }
    56 
    57     public void setImages(List images) {
    58         this.images = images;
    59     }
    60 
    61 }

    3.

     1 package mypack;
     2 
     3 import org.hibernate.*;
     4 import org.hibernate.cfg.Configuration;
     5 import java.util.*;
     6 import java.sql.*;
     7 
     8 public class BusinessService{
     9   public static SessionFactory sessionFactory;
    10   static{
    11      try{
    12        Configuration config = new Configuration().configure();
    13        sessionFactory = config.buildSessionFactory();
    14     }catch(RuntimeException e){e.printStackTrace();throw e;}
    15 
    16   }
    17 
    18   public void saveMonkey(Monkey monkey){
    19       Session session = sessionFactory.openSession();
    20       Transaction tx = null;
    21       List results=new ArrayList();
    22       try {
    23        tx = session.beginTransaction();
    24        session.save(monkey);
    25        tx.commit();
    26     }catch (RuntimeException e) {
    27       if (tx != null) {
    28         tx.rollback();
    29       }
    30       throw e;
    31     } finally {
    32       session.close();
    33     }
    34   }
    35 
    36   public Monkey loadMonkey(long id){
    37       Session session = sessionFactory.openSession();
    38       Transaction tx = null;
    39       try {
    40        tx = session.beginTransaction();
    41        Monkey monkey=(Monkey)session.get(Monkey.class,new Long(id));
    42        Hibernate.initialize(monkey.getImages());
    43        tx.commit();
    44       return monkey;
    45     }catch (RuntimeException e) {
    46       if (tx != null) {
    47         tx.rollback();
    48       }
    49       throw e;
    50     } finally {
    51       session.close();
    52     }
    53   }
    54 
    55    public void test(){
    56       List images=new ArrayList();
    57       images.add("image1.jpg");
    58       images.add("image4.jpg");
    59       images.add("image2.jpg");
    60       images.add("image2.jpg");
    61       images.add("image5.jpg");
    62       
    63       Monkey monkey=new Monkey("Tom",21,images);
    64       saveMonkey(monkey);
    65       
    66       monkey=loadMonkey(1);
    67       printMonkey(monkey);  
    68 
    69   }
    70 
    71   private void printMonkey(Monkey monkey){
    72      System.out.println(monkey.getImages().getClass().getName());
    73      List images=monkey.getImages();
    74      for(int i=images.size()-1;i>=0;i--){
    75         String fileName=(String)images.get(i);
    76         System.out.println(monkey.getName()+" "+fileName);
    77      }
    78   }
    79   public static void main(String args[]){
    80     new BusinessService().test();
    81     sessionFactory.close();
    82   }
    83 }

    4.

     1 drop database if exists SAMPLEDB;
     2 create database SAMPLEDB;
     3 use SAMPLEDB;
     4 
     5 create table MONKEYS (
     6    ID bigint not null,
     7    NAME varchar(15),
     8    AGE int, 
     9    primary key (ID)
    10 );
    11 
    12 create table IMAGES(
    13    MONKEY_ID bigint not null,
    14    POSITION int not null,
    15    FILENAME varchar(15) not null,
    16    primary key (MONKEY_ID,POSITION)
    17 );
    18 
    19 alter table IMAGES add index IDX_MONKEY(MONKEY_ID), add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);

     

  • 相关阅读:
    近来有客户要求用table显示一大串数据,由于滚动后就看不到表头,很不方便,所以想到这个效果。
    js如何取当前日期时间/格式为:yyyymmdd hh:mm:ss
    jdk源码分析 – Thread线程类源码分析
    Swing中的MVC
    编程生涯的一次思想总结
    怎样成为高手
    浅谈测试驱动开发(TDD)
    Java RMI之HelloWorld篇(EJB都是建立在rmi基础之上的)
    Swing框架之Model
    EJB 工作原理
  • 原文地址:https://www.cnblogs.com/shamgod/p/5299492.html
Copyright © 2011-2022 走看看