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 ...
随机推荐
- Python中从B类中调用A类的方法。
好久没上了,Python还在学--最近进度有点慢... 下面代码记录了一个不太好理解的点,自己写了个小例子,总算是理顺了. B类想要调用A类,自己在网上看了一下其他人的回复:创建A类的实例,直接调用这 ...
- 部署Java Web项目报错(二)
在编写HighCharts折线时,并且数据源是请求CSV,运行项目时出现错误 Uncaught TypeError: Cannot read property 'prototype' of undef ...
- tar (child): jdk-7u71-linux-x64.tar.gz:无法 open: 没有那个文件或目录
1 错误描述 youhaidong@youhaidong:~$ sudo mkdir /usr/lib/jvm [sudo] password for youhaidong: youhaidong@y ...
- TortoiseSVN设置忽略文件和目录文件夹
TortoiseSVN设置忽略文件和目录文件夹 在多数项目中你总会有文件和目录不需要进行版本控制.这可能包括一些由编译器生成的文件,*.obj,*.lst,或许是一个用于存放可执行程序的输出文件夹. ...
- RHEL部署ipa红帽身份验证
1.先下载必须包 yum install -y ipa-server bind bind-dyndb-ldap 2.初始化ipa基本配置 ipa-server-install * Configure ...
- hdu5751 Eades
今天热身考到FFT,完全忘光了,模板敲错了... 晚上温习下以前的题目 这题就是从最大值每次分割现在的区间,这样递归的区间最大值会更小,对于每种最大值都是卷积做 #include<bits/st ...
- spring+jidi读取property的配置文件
在Spring项目中,你可能需要从properties文件中读入配置注入到bean中,例如数据库连接信息,memcached server的地址端口信息等,这些配置信息最好独立于jar包或者war包, ...
- eclipse远程调试Tomcat方法(测试成功并且说说遇到的坑)
转自:http://blog.csdn.net/afgasdg/article/details/9236877 PS:文中容易引起歧义的地方已经做了修改:另外,本人参照以下步骤操作,结果无法调试,后来 ...
- linux和windows通用的路径
String path=request.getSession().getServletContext().getRealPath("/")+"js" + Sys ...
- python与机器学实践-何宇健 源代码及过程中遇到的问题
# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file.""&q ...