hibernate List,Set,Map集合的配备实例(转)
hibernate List,Set,Map集合的配置实例(转)
# List,Set,Map的配置实例
#
# OR映射的设置
#
# 一个对象对应一张表
#
# User <-----> Person1
#
# 一个对象对应两张表
#
# User <-----> Person1 , Phone1
#
# Person1 :ID ,Name
#
# Phone1 :PersonID PK,PhoneCode PK
#
# 自然主键或物理主键
#
# 在User对象中需要添加一个映射Phone1表的属性(集合属性)
#
# 集合属性有三种:List,Set,Map
#
# List 有元素下标
# Map 有键值
# Set 没有重复值即可
#
# Phone1 :PersonID PK,PhoneCode PK ---> Set
# Phone2 :PersonID PK, PhoneOrder PK , PhoneCode ---> List
# Phone3 : PersonID PK, PhoneType PK , PhoneCode ----> Map
#
# 下面是Set集合
# <hibernate-mapping>
# <class name="my.User" table="Person1" schema="dbo" catalog="Demo">
# <id name="uid" type="int">
# <column name="ID" />
# <generator class="assigned"></generator>
# </id>
# <property name="uname" type="string">
# <column name="Name" length="50" not-null="true" />
# </property>
# <set name="phoneSet" table="Phone1" >
# <key column="PersonID" />
# <element column="PhoneCode" type="string" />
# </set>
# </class>
# </hibernate-mapping>
#
# 下面是List集合
# <hibernate-mapping package="my" >
# <class name="my.User" table="Person1" >
# <id name="uid" column="ID" type="int" >
# <generator class="assigned" />
# </id>
# <property name="uname" column="Name" type="string" length="50" />
# <list name="phones" table="Phone1">
# <key column="PID" /> //联合主键PID,ORD 其中ORD是索引列如果使用
#
# list集合就必须手动添加该列
# <index column="ORD" />
# <element column="Code" type="string" length="50" />
# </list>
# </class>
# </hibernate-mapping>
# 下面是Map集合
# <hibernate-mapping package="my" >
# <class name="User" table="Person1" >
# <id name="uid" column="ID" type="int" >
# <generator class="assigned" />
# </id>
# <property name="uname" column="Name" type="string" length="50" />
# <map name="phones" table="Phone3" cascade="save-update" inverse="true" >
# <key column="PID" />
# <map-key column="Type" type="string" />
# <element column="Code" type="string" />
# </map>
# </class>
# </hibernate-mapping>
#
# User.java
# public class User {
#
# private int uid;
# private String uname;
#
# //private List phones = new ArrayList();
# //private Set phones = new HashSet();
# private Map phones=new Hashtable();
#
# public User(){}
#
# public User(int id,String name){
# this.setUid(id);
# this.setUname(name);
#
# }
#
# public int getUid() {
# return uid;
# }
# public void setUid(int uid) {
# this.uid = uid;
# }
# public String getUname() {
# return uname;
# }
# public void setUname(String uname) {
# this.uname = uname;
# }
#
# public Map getPhones() {
# return phones;
# }
#
# public void setPhones(Map phones) {
# this.phones = phones;
# }
#
# }
#
# Test.java
#
#
# public class Test {
#
# @SuppressWarnings("unchecked")
# public static void main(String[] args) {
#
# //1:读配置文件
# Configuration config=new Configuration().configure();
# //2:创建会话工厂
# SessionFactory sf=config.buildSessionFactory();
# //3:获取会话对象
# Session se=sf.openSession();
# /*
# -----List--------
# User user=(User)se.get(User.class, 1);
# List phones=new ArrayList();
# phones.add("022-12345678");
# phones.add("13812345678");
# user.setPhones(phones);
# se.beginTransaction().commit();
# -----Set--------
# User u=new User(4,"ddd");
# Set phoneSet=new HashSet();
# phoneSet.add("022-21331111");
# phoneSet.add("13511112222");
# u.setPhoneSet(phoneSet);
# op.AddUser(u);
# */
# //4:保存,修改和删除一条记录
# User u=new User(3,"ccc");
# Map phoneMap=new Hashtable();
# phoneMap.put("家庭电话", "12345678");
# phoneMap.put("工作电话", "22222222");
# u.setPhones(phoneMap);
# addOneUser(se,u);
# //5:
# // Phone p=(Phone)se.get(Phone.class, 4);
# // System.out.println(p.getSn()+":"+p.getCode()+","+p.getUser().getUname());
# showAllUser(se);
# }
#
# public static void delOneUser(Session se,User user){
# //4.2 删除一条记录
# se.delete(user);
# se.beginTransaction().commit();
# }
#
# public static void updOneUser(Session se,User user){
# //4.2 修改一条记录
# se.update(user);
# se.beginTransaction().commit();
# }
#
#
# public static void addOneUser(Session se,User user){
# //4.2 插入一条记录
# se.save(user);
# se.beginTransaction().commit();
# }
#
# public static void showAllUser(Session se){
# //4:获取查询对象
# Query query=se.createQuery("from User");
# //5:获取查询结果
# List list= query.list();
# //6:处理查询结果
#
# // 当集合是Map时
#
# for(Iterator iter=list.iterator();iter.hasNext();){
# User temp=(User)iter.next();
# Map phoneMap=temp.getPhones();
# String strPhones="";
# Iterator ite= phoneMap.values().iterator();
# while(ite.hasNext()){
# String phone =(String)ite.next();
# strPhones += phone+",";
# }
# System.out.println(temp.getUid()+":"+temp.getUname()+","+strPhones);
#
# }
# // 当集合是Set时
#
# // for(Iterator iter=list.iterator();iter.hasNext();){
# // User temp=(User)iter.next();
# // Set phoneSet=temp.getPhones();
# // String strPhones="";
# // Iterator ite= phoneSet.iterator();
# // while(ite.hasNext()){
# // Phone phone=(Phone)ite.next();
# // strPhones += phone.getCode()+",";
# // }
# // System.out.println(temp.getUid()+":"+temp.getUname()
#
# +","+strPhones);
# //
# // }
# }
#
# }
原谅引用地址如下:http://blog.****.net/wslyy99/archive/2008/12/29/3635346.aspx
# List,Set,Map的配置实例
#
# OR映射的设置
#
# 一个对象对应一张表
#
# User <-----> Person1
#
# 一个对象对应两张表
#
# User <-----> Person1 , Phone1
#
# Person1 :ID ,Name
#
# Phone1 :PersonID PK,PhoneCode PK
#
# 自然主键或物理主键
#
# 在User对象中需要添加一个映射Phone1表的属性(集合属性)
#
# 集合属性有三种:List,Set,Map
#
# List 有元素下标
# Map 有键值
# Set 没有重复值即可
#
# Phone1 :PersonID PK,PhoneCode PK ---> Set
# Phone2 :PersonID PK, PhoneOrder PK , PhoneCode ---> List
# Phone3 : PersonID PK, PhoneType PK , PhoneCode ----> Map
#
# 下面是Set集合
# <hibernate-mapping>
# <class name="my.User" table="Person1" schema="dbo" catalog="Demo">
# <id name="uid" type="int">
# <column name="ID" />
# <generator class="assigned"></generator>
# </id>
# <property name="uname" type="string">
# <column name="Name" length="50" not-null="true" />
# </property>
# <set name="phoneSet" table="Phone1" >
# <key column="PersonID" />
# <element column="PhoneCode" type="string" />
# </set>
# </class>
# </hibernate-mapping>
#
# 下面是List集合
# <hibernate-mapping package="my" >
# <class name="my.User" table="Person1" >
# <id name="uid" column="ID" type="int" >
# <generator class="assigned" />
# </id>
# <property name="uname" column="Name" type="string" length="50" />
# <list name="phones" table="Phone1">
# <key column="PID" /> //联合主键PID,ORD 其中ORD是索引列如果使用
#
# list集合就必须手动添加该列
# <index column="ORD" />
# <element column="Code" type="string" length="50" />
# </list>
# </class>
# </hibernate-mapping>
# 下面是Map集合
# <hibernate-mapping package="my" >
# <class name="User" table="Person1" >
# <id name="uid" column="ID" type="int" >
# <generator class="assigned" />
# </id>
# <property name="uname" column="Name" type="string" length="50" />
# <map name="phones" table="Phone3" cascade="save-update" inverse="true" >
# <key column="PID" />
# <map-key column="Type" type="string" />
# <element column="Code" type="string" />
# </map>
# </class>
# </hibernate-mapping>
#
# User.java
# public class User {
#
# private int uid;
# private String uname;
#
# //private List phones = new ArrayList();
# //private Set phones = new HashSet();
# private Map phones=new Hashtable();
#
# public User(){}
#
# public User(int id,String name){
# this.setUid(id);
# this.setUname(name);
#
# }
#
# public int getUid() {
# return uid;
# }
# public void setUid(int uid) {
# this.uid = uid;
# }
# public String getUname() {
# return uname;
# }
# public void setUname(String uname) {
# this.uname = uname;
# }
#
# public Map getPhones() {
# return phones;
# }
#
# public void setPhones(Map phones) {
# this.phones = phones;
# }
#
# }
#
# Test.java
#
#
# public class Test {
#
# @SuppressWarnings("unchecked")
# public static void main(String[] args) {
#
# //1:读配置文件
# Configuration config=new Configuration().configure();
# //2:创建会话工厂
# SessionFactory sf=config.buildSessionFactory();
# //3:获取会话对象
# Session se=sf.openSession();
# /*
# -----List--------
# User user=(User)se.get(User.class, 1);
# List phones=new ArrayList();
# phones.add("022-12345678");
# phones.add("13812345678");
# user.setPhones(phones);
# se.beginTransaction().commit();
# -----Set--------
# User u=new User(4,"ddd");
# Set phoneSet=new HashSet();
# phoneSet.add("022-21331111");
# phoneSet.add("13511112222");
# u.setPhoneSet(phoneSet);
# op.AddUser(u);
# */
# //4:保存,修改和删除一条记录
# User u=new User(3,"ccc");
# Map phoneMap=new Hashtable();
# phoneMap.put("家庭电话", "12345678");
# phoneMap.put("工作电话", "22222222");
# u.setPhones(phoneMap);
# addOneUser(se,u);
# //5:
# // Phone p=(Phone)se.get(Phone.class, 4);
# // System.out.println(p.getSn()+":"+p.getCode()+","+p.getUser().getUname());
# showAllUser(se);
# }
#
# public static void delOneUser(Session se,User user){
# //4.2 删除一条记录
# se.delete(user);
# se.beginTransaction().commit();
# }
#
# public static void updOneUser(Session se,User user){
# //4.2 修改一条记录
# se.update(user);
# se.beginTransaction().commit();
# }
#
#
# public static void addOneUser(Session se,User user){
# //4.2 插入一条记录
# se.save(user);
# se.beginTransaction().commit();
# }
#
# public static void showAllUser(Session se){
# //4:获取查询对象
# Query query=se.createQuery("from User");
# //5:获取查询结果
# List list= query.list();
# //6:处理查询结果
#
# // 当集合是Map时
#
# for(Iterator iter=list.iterator();iter.hasNext();){
# User temp=(User)iter.next();
# Map phoneMap=temp.getPhones();
# String strPhones="";
# Iterator ite= phoneMap.values().iterator();
# while(ite.hasNext()){
# String phone =(String)ite.next();
# strPhones += phone+",";
# }
# System.out.println(temp.getUid()+":"+temp.getUname()+","+strPhones);
#
# }
# // 当集合是Set时
#
# // for(Iterator iter=list.iterator();iter.hasNext();){
# // User temp=(User)iter.next();
# // Set phoneSet=temp.getPhones();
# // String strPhones="";
# // Iterator ite= phoneSet.iterator();
# // while(ite.hasNext()){
# // Phone phone=(Phone)ite.next();
# // strPhones += phone.getCode()+",";
# // }
# // System.out.println(temp.getUid()+":"+temp.getUname()
#
# +","+strPhones);
# //
# // }
# }
#
# }
原谅引用地址如下:http://blog.****.net/wslyy99/archive/2008/12/29/3635346.aspx