Hibernate三种容器(List,Set,Map)映射技术之Map映射

 

 环境: eclipse+myeclipse+mysql.

(1) 建表脚本

–刪除表
drop table if exists phone  ;
drop table if exists person ;

–新建表
create table person
(
 id int not null AUTO_INCREMENT primary key ,
 name  varchar(20) ,
 age int
) ;

create table phone
(
 id int ,
 name varchar(20) not null,
 numbers varchar(11) not null ,
 foreign key (id) references person(id) on delete cascade
) ;

–提交
commit ;

(2) Person.java的编写.

package org.hibernate.mapDemo.vo;

import java.util.Map;

public class Person {
 private int id ;
 private String name ;
 private int age ;
 private Map phone;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public Map getPhone() {
  return phone;
 }
 public void setPhone(Map phone) {
  this.phone = phone;
 }
}

(3) Person.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=”org.hibernate.mapDemo.vo.Person” table=”person” >
        <id name=”id” type=”java.lang.Integer”>
            <column name=”id” />
            <generator class=”increment” />
        </id>
        <property name=”name” type=”java.lang.String”>
            <column name=”name” length=”20″ />
        </property>
        <property name=”age” type=”java.lang.Integer”>
            <column name=”age” />
        </property>
        <map name=”phone” table=”phone”>
         <key column=”id”></key>
         <index column=”name” type=”java.lang.String”></index>
         <element type=”java.lang.String”>
          <column name=”numbers”></column>
         </element>
        </map>
    </class>
</hibernate-mapping>

<4> 測試類的編寫.

package org.hibernate.mapDemo.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapDemo.vo.Person;

public class PersonDao {
 private Session session ;
 public PersonDao()
 {
  session = new Configuration().configure()
     .buildSessionFactory().openSession() ;
 }
 
 
 
 public void insert(Person p)
 {
  session.save(p) ;
  session.beginTransaction().commit() ;
  session.close() ;
 }
 
 public Person findbyid(int id)
 {
  String hql = “from Person as p where p.id=?” ;
  Query q = session.createQuery(hql) ;
  q.setInteger(0, id) ;
  List l = q.list() ;
  if(l.size()>0)
   return (Person)l.get(0) ;
  return null ;
 }
 
 public void update(Person p)
 {
  session.update(p) ;
  session.beginTransaction().commit() ;
  session.close() ;
 }
 
 public static void main(String[] args)
 {
  PersonDao pd = new PersonDao() ;
//插入  
//  Person p = new Person() ;
//  p.setName(“zhangsan”) ;
//  p.setAge(10) ;
//  p.setPhone(new HashMap()) ;
//  Map m = p.getPhone() ;
//  m.put(“lisi”, “123”) ;
//  m.put(“lisi”, “123”) ;
//  m.put(“lisi”, “124”) ;
//  m.put(“wangwu”, “123”) ;
//  m.put(“wangwu”, “126”) ;
//  p.setPhone(m) ;
//  pd.insert(p) ;

//更新  
  Person p = pd.findbyid(1) ;
  p.setName(“lisi”) ;
  p.setAge(1) ;
  
  p.getPhone().put(“lisi”, “lis3i”) ;
  p.getPhone().put(“lisis2”, “lisi”) ;
  
  pd.update(p) ;
  
 }
 
}

注:此處免去了Hibernate.cfg.xml的編寫.

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *