001-List,数组,Set,Map属性的映射
hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">mysqladmin</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="sqlmap/News.hbm.xml"/>
<mapping resource="sqlmap/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Person.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.amazing.hibernate.domain"> <class name="Person" table="person">
<id name="id" type="integer" column="ID">
<generator class="identity"/>
</id>
<property name="name" type="string" column="name"/>
<property name="age" type="integer" column="age"/>
<list name="schools" table="school">
<key column="personId"/>
<list-index column="listIndex"/>
<element type="string" column="schoolName"/>
</list>
</class> </hibernate-mapping>
Person.java:
package com.amazing.hibernate.domain; import java.util.ArrayList;
import java.util.List; public class Person { private Integer id;
private String name;
private int age;
private List<String> schools = new ArrayList<String>(); public Integer getId() {
return id;
}
public void setId(Integer 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 List<String> getSchools() {
return schools;
}
public void setSchools(List<String> schools) {
this.schools = schools;
} }
TestList.java:
package com.amazing.hibernate.domain; import java.util.ArrayList;
import java.util.List; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; public class TestList { @Test
public void testListMapping(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.getCurrentSession();
Transaction txt = session.beginTransaction(); Person p1 = new Person();
p1.setName("tom");
p1.setAge(24);
List<String> schools1 = new ArrayList<String>();
schools1.add("小学");
schools1.add("初中");
p1.setSchools(schools1);
session.save(p1); Person p2 = new Person();
p2.setName("jack");
p2.setAge(28);
List<String> schools2 = new ArrayList<String>();
schools2.add("高中");
schools2.add("大学");
p2.setSchools(schools2);
session.save(p2); txt.commit();
System.out.println("session.isOpen():"+session.isOpen());//false
System.out.println("sf.isClosed():"+sf.isClosed());//false
sf.close();
}
}

若schools为String数组类型,即private String[] schools.映射文件Person.hbm.xml只需要将list元素改为array即可。
若schools为Set类型,即private Set<String> schools.映射文件Person.hbm.xml稍作修改:
<set name="schools" table="school">
<key column="personId"/>
<element type="string" column="schoolName" not-null="true"/>
</set>

若Person类有一个Map属性,即private Map<String,Float> scores.映射文件Person.hbm.xml稍作修改:
<map name="scores" table="score">
<key column="personId" not-null="true"/>
<map-key column="subject" type="string"/>
<element column="grade" type="float"/>
</map>
package com.amazing.hibernate.domain; import java.util.HashMap;
import java.util.Map; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; public class TestDemo { @Test
public void testListMapping(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session session = sf.getCurrentSession();
Transaction txt = session.beginTransaction(); Person p = new Person();
p.setName("tom");
p.setAge(24);
Map<String,Float> scores = new HashMap<String,Float>();
scores.put("语文", 85f);
scores.put("数学", 92f);
p.setScores(scores);
session.save(p); txt.commit();
System.out.println("session.isOpen():"+session.isOpen());
System.out.println("sf.isClosed():"+sf.isClosed());
sf.close();
}
}

001-List,数组,Set,Map属性的映射的更多相关文章
- Hibernate学习---第六节:数组&list&map&set的映射配置
1.实体类,代码如下: package learn.hibernate.bean; import java.util.Date; import java.util.HashMap; import ja ...
- 数组方法map(映射),reduce(规约),foreach(遍历),filter(过滤)
数组方法map(映射),reduce(规约),foreach(遍历),filter(过滤) map()方法返回一个由原数组中每一个元素调用一个指定方法后返回的新数组 reduce()方法接受一个函数作 ...
- Hibernate映射Map属性2
Hibernate在映射Map属性时生成映射文件.需要注意的一些地方.下面是我的一个例子. Java类如下 public class NameAndNumber { private Integer i ...
- Hibernate映射Map属性
看到一篇Hibernate 映射Map属性的文章挺好的转载一下原地址:http://blog.sina.com.cn/s/blog_86f4502c0101fs1x.html Map集合属于有序集合, ...
- java bean、List、数组、map和Json的相互转化
工程 json包为 代码 package com.my.json; public class ChildBean { private String childName; private String ...
- 第9章 集合处理(数组、Map、Set)
目录 1. 数组 1.1 创建数组 1.2 在数组两端添加删除元素 1.3 在数组任意位置添加.删除元素 delete删除数组元素无效 使用splice方法增.删.改元素 1.4 数组的常用操作 数组 ...
- JavaScript中数组类型的属性和方法
除了Object,Array类型应该是ECMAScript中最常用的类型了. ECMAScript的数组虽然也是数据的有序列表,但还是与其他语言中的数组有很大的区别.比如ECMAScript数组每一项 ...
- javascript数组的实例属性(方法)
javascript的所有数组实例对象,除了可以给自己增删属性之外:都会从Array.prototype继承属性(方法).修改Array的原型会影响所有的数组实例. 数组实例的属性: Array.pr ...
- MyBatis传入参数为list、数组、map写法(转载)
MyBatis传入参数为list.数组.map写法 1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item ...
随机推荐
- 故障定位之查找附近点GeoHash研讨
随着移动终端的普及,很多应用都基于LBS功能,附近的某某(餐馆.银行.妹纸等等). 基础数据中,一般保存了目标位置的经纬度:利用用户提供的经纬度,进行对比,从而获得是否在附近. 目标:查找附近的XXX ...
- 创建数据库表默认字段封装SQL
declare @Table_Name varchar(500) declare @strSQL varchar(500) set @Table_Name='UserInfo' --在此处设置要创建的 ...
- Java和Flex整合报错(四)
1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...
- angular路由操作
在单页面应用程序中比如angular应用,我们需要根据url的变化(即:不同的请求),来分配不同的资源.根据请求的URL来决定执行哪个模块,这个过程叫路由,同时,我们需要设计路由规则. 下面给出一个简 ...
- hdu5863 cjj's string game
矩阵快速幂 #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MOD = ...
- python与机器学实践-何宇健 源代码及过程中遇到的问题
# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file.""&q ...
- iOS 双击tabbar刷新页面
/*在继承UITabBarController控制器中*/ #pragma mark <UITabBarControllerDelegate> -(void)tabBarControlle ...
- js去重
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- 强大而容易学的JavaScript初学者可以看看。
基本操作: 第一点:存起数组元素: 单维数组,数组名[下标索引]: 多维数组,数组名[外维数组下标][内部数组下标]: 特性:数组的length属性是具有弹性的,可以自由伸缩: 数组下标从0开始(其实 ...
- java中获取项目在tomcat目录下的路径方法
HttpServletRequest request //获取的是ROOT项目在tomcat下的路径 方法1: String path = request.getSession().getServle ...