Hibernate Map集合映射简单例子

jopen 8年前

Hibernate Map集合映射简单例子

Map集合的特点就是使用了键值对,即Key-Value结构来存放集合元素,这样就需要对应的集合元素数据库表中包含对于Key的列。

建两个相应的表:

email_map:

create table email_map(  id int(11) not null,  email_id varchar(20) not null,  email varchar(100) not null default ''  )engine=innodb default charset=gbk;

user_map:

create table user_map(  id int(11) not null auto_increment,  name varchar(100) not null default '',  primary key(id)  )engine=innodb default charset=gbk;

建立UserMap.java实体类:

package collect.map;    import java.util.HashMap;  import java.util.Map;    public class UserMap implements java.io.Serializable {     // Fields     private Integer id;   private String name;   private Map emails = new HashMap();     // Constructors     /** default constructor */   public UserMap() {   }     /** full constructor */   public UserMap(String name) {    this.name = name;   }     // Property accessors     public Integer getId() {    return this.id;   }     public void setId(Integer id) {    this.id = id;   }     public String getName() {    return this.name;   }     public void setName(String name) {    this.name = name;   }     public Map getEmails() {    return emails;   }     public void setEmails(Map emails) {    this.emails = emails;   }    }
建立映射文件UserMap.hbm.xml:

<?xml version="1.0" encoding="utf-8"?>  <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  <!--       Mapping file autogenerated by MyEclipse Persistence Tools  -->  <hibernate-mapping>      <class name="collect.map.UserMap" table="user_map" catalog="ssh">          <id name="id" type="java.lang.Integer">              <column name="id" />              <generator class="native" />          </id>          <property name="name" type="java.lang.String">              <column name="name" length="100" not-null="true" />          </property>          <map name="emails" table="email_map">           <key column="id"></key>           <map-key column="email_id" type="java.lang.String"></map-key>           <element type="java.lang.String" column="email"></element>          </map>      </class>  </hibernate-mapping>

注解:其中Map类型映射中<map-key>标签指明Map使用了String类型作为Key,它配置键值对中的“键”。这样经Map类型映射配置后,操作map.put("A","B")中的"A"、"B" 对应表email_map中的字段email_id、email。

将该映射文件加入到Hibernate配置文件中,建立测试类Test.java:

package collect.map;    import org.hibernate.Session;  import org.hibernate.SessionFactory;  import org.hibernate.Transaction;  import org.hibernate.cfg.Configuration;    public class Test {     public static void main(String[] args) {    // Configuration管理Hibernate配置    Configuration config = new Configuration().configure();    // 根据Configuration建立 SessionFactory    // SessionFactory用来建立Session    SessionFactory sessionFactory = config.buildSessionFactory();      // 创建实例    UserMap user1 = new UserMap();    user1.setName("user1");    user1.getEmails().put("no.1", "email1");    user1.getEmails().put("no.2", "email2");      UserMap user2 = new UserMap();    user2.setName("user2");    user2.getEmails().put("no.3", "email3");    user2.getEmails().put("no.4", "email4");      // 添加数据    Session session = sessionFactory.openSession();    Transaction tx = null;    try {     tx = session.beginTransaction();                          session.save(user1);     session.save(user2);     tx.commit();    } catch (RuntimeException e) {     if (tx != null)      tx.rollback();     throw e;    } finally {     session.close();    }      // 关闭sessionFactory    sessionFactory.close();     }    }

运行结果:

控制台:

23:10:32,980 DEBUG SQL:346 - insert into ssh.user_map (name) values (?)
23:10:32,997 DEBUG SQL:346 - insert into ssh.user_map (name) values (?)
23:10:33,004 DEBUG SQL:346 - insert into email_map (id, email_id, email) values (?, ?, ?)
23:10:33,007 DEBUG SQL:346 - insert into email_map (id, email_id, email) values (?, ?, ?)
23:10:33,007 DEBUG SQL:346 - insert into email_map (id, email_id, email) values (?, ?, ?)
23:10:33,007 DEBUG SQL:346 - insert into email_map (id, email_id, email) values (?, ?, ?)

注解:从上面日志输出中可以看到,Key和Value的对应关系。
数据库:
Hibernate Map集合映射简单例子