首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-10-31 
Hibernate三种容器(List,Set,Map)映射技术之Map映射??环境: eclipse+myeclipse+mysql.(1) 建表脚本--刪除

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 />
??????? </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的編寫.

1 楼 javadev 2008-06-05   我最常用的是SET映射,List偶尔也用,但感觉比set配置麻烦点,但map不常用。 2 楼 java心如止水 2008-06-05   学好这三种映射关系才能更好运用表的一对一,一对多,多对多

热点排行