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 ...
随机推荐
- Mongodb Java Driver 参数配置解析
要正确使用Mongodb Java Driver,MongoClientOptions参数配置对数据库访问的并发性能影响极大. connectionsPerHost:与目标数据库能够建立的最大conn ...
- hibernate知识点理解
1.只有业务逻辑层出现的问题? 1.切换数据库麻烦 2.sql编写起来麻烦 3.我们的程序员不需要关注数据库,只希望关心业务本身 2.hibernate的好处 1.程序员只关心业务逻辑,使角色更加清楚 ...
- SQLserver Delete from where 与Oracle delete from where 的差异
1.SQLserver 版本: select @@version; Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) Dec 28 2012 20 ...
- Shell 总结
find: –name 'filenme' * ? [] ; –iname; –regex PATTERN; –user username; –group; –uid; –gid; –nouser; ...
- JAVA类与对象(三)----类定义关键字详解
static 表示静态,它可以修饰属性,方法和代码块. 1.static修饰属性(类变量),那么这个属性就可以用类名.属性名来访问,也就是使这个属性成为本类的类变量,为本类对象所共有.这个属性就是全类 ...
- sharepoint 列表的column验证----------SharePoint 2010 List Validation Formula
首先,依次打开-站点->列表名称->列表设置->验证设置: 我们设置一个时间的列不能小于当前时间,并且在编辑的时候不需要验证. =OR([,Created<TODAY())
- 微信诡异的 40029 不合法的oauth_code
最近几天在做微信公共平台开发,之前一切正常运行着,发布一套程序出去之后,发现时不时的报错! 小总结下问题出现原因:微信oauth2.0 接口说明 第一步:用户同意授权,获取code 在确保微信公众账号 ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- IntelliJ IDEA 15 创建maven项目
说明 创建Maven项目的方式:手工创建 好处:参考IntelliJ IDEA 14 创建maven项目二(此文章描述了用此方式创建Maven项目的好处)及idea14使用maven创建web工程(此 ...
- 无废话网页重构系列——(7)布局(区块、栅格)、模块组件(module)
本文作者:大象本文地址:http://www.cnblogs.com/daxiang/p/4654800.html 在构建HTML主干结构后,开始编写“页面布局”和“模块组件”: 页面框架由几个主干结 ...