Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)
1.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping > <class name="mypack.Monkey" table="MONKEYS" >
<id name="id" type="long" column="ID">
<generator class="increment"/>
</id> <property name="name" type="string" >
<column name="NAME" length="15" />
</property> <property name="age" type="int" >
<column name="AGE" />
</property> <map name="images" table="IMAGES" lazy="true" sort="natural">
<key column="MONKEY_ID" />
<map-key column="IMAGE_NAME" type="string"/>
<element column="FILENAME" type="string" not-null="true"/>
</map> </class> </hibernate-mapping>
2.
package mypack; import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap; public class Monkey implements Serializable {
private Long id;
private String name;
private int age;
private Map images=new TreeMap(); /** full constructor */
public Monkey(String name, int age,Map images) {
this.name = name;
this.age=age;
this.images = images;
} /** default constructor */
public Monkey() {
} /** minimal constructor */
public Monkey(Map images) {
this.images = images;
} public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return this.age;
} public void setAge(int age) {
this.age = age;
} public Map getImages() {
return this.images;
} public void setImages(Map images) {
this.images = images;
} }
3.
package mypack; import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;
import java.sql.*; public class BusinessService{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;} } public void saveMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
List results=new ArrayList();
try {
tx = session.beginTransaction();
session.save(monkey);
tx.commit();
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public Monkey loadMonkey(long id){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Monkey monkey=(Monkey)session.get(Monkey.class,new Long(id));
Hibernate.initialize(monkey.getImages());
tx.commit();
return monkey;
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void test(){
Map images=new TreeMap();
images.put("image1","image1.jpg");
images.put("image4","image4.jpg");
images.put("image2","image2.jpg");
images.put("imageTwo","image2.jpg");
images.put("image5","image5.jpg"); Monkey monkey=new Monkey("Tom",21,images);
saveMonkey(monkey); monkey=loadMonkey(1);
printMonkey(monkey); } private void printMonkey(Monkey monkey){
System.out.println(monkey.getImages().getClass().getName());
Map images=monkey.getImages();
Set keys=images.keySet();
Iterator it=keys.iterator();
while(it.hasNext()){
String key=(String)it.next();
String filename=(String)images.get(key);
System.out.println(monkey.getName()+" "+key+" "+filename);
} }
public static void main(String args[]) {
new BusinessService().test();
sessionFactory.close();
}
}
4.
Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)的更多相关文章
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-002映射Bag(<idbag><collection-id>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第12章 映射值类型集合-001映射set(<element>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
随机推荐
- Delphi XE5教程8:使用Delphi命名空间
// Project file declarations... //项目文件声明… program MyCompany.ProjectX.ProgramY; // Unit source file d ...
- 使用jQuery获取GridView的数据行的数量
一个同事在群里抛出了上述的问题,另一个同事给出了答案,试了一下,还不错.贴出代码和效果图: <html xmlns="http://www.w3.org/1999/xhtml" ...
- python学习第六天
一. 模块介绍1. 模块的定义:用一堆代码实现了某个功能的代码集合 包的定义:本质就是一个目录(必须导游一个_init_.py文件),是用来从逻辑上组织模块的.2. 需要多个函数才能完成(函数 ...
- Flex xxx-app.xml配置
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http:/ ...
- C语言的sizeof
今天帮同学想用C实现数组的折半查找,本来算法挺简单的,可是折腾了好几个小时才发现问题在哪,这个sizeof坑人不浅啊. #include<stdio.h> void m(int []); ...
- WCF-Configuration
Host-Configuration <?xml version="1.0"?> <configuration> <configSections> ...
- pthread属性设置
Posix线程中的线程属性pthread_attr_t主要包括scope属性.detach属性.堆栈地址.堆栈大小.优先级.在pthread_create中,把第二个参数设置为NULL的话,将采用默认 ...
- 从零开始学ios开发(五):IOS控件(2),Slider
下面继续学习ios的其他控件,这次会使用到的控件有Slider,当然还有一些之前已经使用过的控件Label. 这次我们不新建一个project了,当然如果你愿意重新创建一个新的项目也完全可以,我们还是 ...
- MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题
MAC升级到最新系统后 本地测试的站点不能正常运行,其原因是mac系统升级后 他的apache的版本也随之升级了版本 Server version: Apache/2.4.9 (Unix) Serve ...
- ibatis.net demo
1. download ibatis.nethttps://code.google.com/p/mybatisnet/ 2. add all dll as reference to your proj ...