Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)
1.
2.
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 <map name="images" table="IMAGES" lazy="true"> 21 <key column="MONKEY_ID" /> 22 <map-key column="IMAGE_NAME" type="string"/> 23 <element column="FILENAME" type="string" not-null="true"/> 24 </map> 25 26 </class> 27 28 </hibernate-mapping>
3.
1 package mypack; 2 3 import java.io.Serializable; 4 import java.util.Map; 5 import java.util.HashMap; 6 7 public class Monkey implements Serializable { 8 private Long id; 9 private String name; 10 private int age; 11 private Map images=new HashMap(); 12 13 /** full constructor */ 14 public Monkey(String name, int age,Map 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(Map 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 Map getImages() { 54 return this.images; 55 } 56 57 public void setImages(Map images) { 58 this.images = images; 59 } 60 61 }
4.
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 Map images=new HashMap(); 57 images.put("image1","image1.jpg"); 58 images.put("image4","image4.jpg"); 59 images.put("image2","image2.jpg"); 60 images.put("imageTwo","image2.jpg"); 61 images.put("image5","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 Map images=monkey.getImages(); 74 Set keys=images.keySet(); 75 Iterator it=keys.iterator(); 76 while(it.hasNext()){ 77 String key=(String)it.next(); 78 String filename=(String)images.get(key); 79 System.out.println(monkey.getName()+" "+key+" "+filename); 80 } 81 82 } 83 public static void main(String args[]){ 84 new BusinessService().test(); 85 sessionFactory.close(); 86 } 87 }
5.
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 IMAGE_NAME varchar(15) not null, 15 FILENAME varchar(15) not null, 16 primary key (MONKEY_ID,IMAGE_NAME) 17 ); 18 19 alter table IMAGES add index IDX_MONKEY(MONKEY_ID), add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);
6.
You can do anything you set your mind to, man!