Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)
1.
2.
<?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">
<key column="MONKEY_ID" />
<map-key column="IMAGE_NAME" type="string"/>
<element column="FILENAME" type="string" not-null="true"/>
</map> </class> </hibernate-mapping>
3.
package mypack; import java.io.Serializable;
import java.util.Map;
import java.util.HashMap; public class Monkey implements Serializable {
private Long id;
private String name;
private int age;
private Map images=new HashMap(); /** 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;
} }
4.
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 HashMap();
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();
}
}
5.
drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB; create table MONKEYS (
ID bigint not null,
NAME varchar(15),
AGE int,
primary key (ID)
); create table IMAGES(
MONKEY_ID bigint not null,
IMAGE_NAME varchar(15) not null,
FILENAME varchar(15) not null,
primary key (MONKEY_ID,IMAGE_NAME)
); alter table IMAGES add index IDX_MONKEY(MONKEY_ID), add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);
6.
Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)的更多相关文章
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)
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(十)--- 映射值类型集合
一.映射Set(集):未排序,无重复. 实例代码: <set name="images" table="IMAGES" lazy="true&q ...
- 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 ...
随机推荐
- windows不能在本地计算机启动apache
今天,配置eclipse PHP studio 3.0的时候更改了apache http server 中的httpd.conf文件: 将DocumentRoot 的路径设错了,为一个不存在目录 .更 ...
- 如何去掉html中的超链接
$a= preg_replace("/<a[^>]+>/", "", $a); $a= preg_replace("/<\/a ...
- Swift初步介绍
Swift是本届WWDC大会苹果推出的一门新开发语言,开发者网站上已经放出了这门新语言的介绍.教程和手册,如果手里有一台iOS设备的话,通过苹果的iBooks应用,从它的官方书店里搜索Swift,可以 ...
- Laravel 5 基础(三)- 向视图传送数据(续)
我们不仅仅可以向视图传送一个数据,同样我们可以传送Array public function about() { return view('pages.about')->with([ 'firs ...
- SqlServer ,storedprocedure操作
USE [Role] GO /*Create a table*/ IF OBJECT_ID ('dbo.Users', 'U') IS NOT NULL DROP TABLE Users GO CRE ...
- 【面试虐菜】—— JAVA面试题(3)
1 throws与throw的区别 解析:throws和throw是异常处理时两个常见的关键字,初级程序员常常容易正确理解throw和throws的作用和区别,说明已经能比较深入理解异常处理.Thro ...
- nginx服务器绑定域名和设置根目录
首先进入nginx安装目录的配置目录conf,然后执行 vi conf/nginx.conf 打开nginx的配置文件,找到并修改红字部分 server { listen default_server ...
- CentOS thrift python demo
编辑接口文件 hellowworld.thrift service HelloWorld { string ping(), string say(1:string msg) } 编辑 server.p ...
- android LayoutInflater.inflate()的参数介绍
LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...
- .net视图中日期格式化
昨天在做一个功能,要在界面上按照规定的格式显示一个时间,如果直接在expression那里格式化的话(如下:) @Html.DisplayFor(c => Convert.ToDateTime( ...