映射值类型集合-001映射set(<element>)

Posted shamgod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了映射值类型集合-001映射set(<element>)相关的知识,希望对你有一定的参考价值。

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    <set   name="images"   table="IMAGES"    lazy="true" >
21         <key column="MONKEY_ID" />
22         <element column="FILENAME" type="string"  not-null="true"/>
23    </set>   
24 
25   </class>
26  
27 </hibernate-mapping>

 

3.

 1 package mypack;
 2 
 3 import java.io.Serializable;
 4 import java.util.Set;
 5 import java.util.TreeSet;
 6 
 7 public class Monkey implements Serializable {
 8     private Long id;
 9     private String name;
10     private int age;
11     private Set images=new TreeSet();
12 
13     /** full constructor */
14     public Monkey(String name, int age,Set 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(Set 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 Set getImages() {
54         return this.images;
55     }
56 
57     public void setImages(Set 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       Set images=new HashSet();
57       images.add("image1.jpg");
58       images.add("image4.jpg");
59       images.add("image2.jpg");
60       images.add("image5.jpg");
61       
62       Monkey monkey=new Monkey("Tom",21,images);
63       saveMonkey(monkey);
64       
65       monkey=loadMonkey(1);
66       printMonkey(monkey);  
67 
68   }
69 
70   private void printMonkey(Monkey monkey){
71      System.out.println(monkey.getImages().getClass().getName());
72      Iterator it=monkey.getImages().iterator();
73      while(it.hasNext()){
74         String fileName=(String)it.next();
75         System.out.println(monkey.getName()+" "+fileName);
76      }
77   }
78   public static void main(String args[]){
79     new BusinessService().test();
80     sessionFactory.close();
81   }
82 }
 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    FILENAME varchar(15) not null,
15    primary key (MONKEY_ID,FILENAME)
16 );
17 
18 alter table IMAGES add index IDX_MONKEY(MONKEY_ID), add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);

 

5.

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!DOCTYPE hibernate-configuration
 3  PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
 4  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7     <session-factory>
 8         <property name="dialect">
 9             org.hibernate.dialect.mysqlDialect
10         </property>
11         <property name="connection.driver_class">
12             com.mysql.jdbc.Driver
13         </property>
14         <property name="connection.url">
15             jdbc:mysql://localhost:3306/sampledb
16         </property>
17         <property name="connection.username">
18             root
19         </property>
20         <property name="connection.password">
21             1234
22         </property>
23 
24         <property name="show_sql">true</property>
25 
26         <mapping resource="mypack/Monkey.hbm.xml" />
27     </session-factory>
28 </hibernate-configuration>

 

以上是关于映射值类型集合-001映射set(<element>)的主要内容,如果未能解决你的问题,请参考以下文章

STL初步不定长数组:vector + 集合:set + 映射:map

映射值类型集合-003映射List(<list-index>)

映射值类型集合-002映射Bag(<idbag><collection-id>)

mapstruct使用指南

hibernate中Set集合映射

如何将列表值映射到新对象属性