Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)
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> <list name="images" table="IMAGES" lazy="true">
<key column="MONKEY_ID" />
<list-index column="POSITION" />
<element column="FILENAME" type="string" not-null="true"/>
</list> </class> </hibernate-mapping>
2.
package mypack; import java.io.Serializable;
import java.util.List;
import java.util.ArrayList; public class Monkey implements Serializable {
private Long id;
private String name;
private int age;
private List images=new ArrayList(); /** full constructor */
public Monkey(String name, int age,List images) {
this.name = name;
this.age=age;
this.images = images;
} /** default constructor */
public Monkey() {
} /** minimal constructor */
public Monkey(List 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 List getImages() {
return this.images;
} public void setImages(List 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(){
List images=new ArrayList();
images.add("image1.jpg");
images.add("image4.jpg");
images.add("image2.jpg");
images.add("image2.jpg");
images.add("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());
List images=monkey.getImages();
for(int i=images.size()-1;i>=0;i--){
String fileName=(String)images.get(i);
System.out.println(monkey.getName()+" "+fileName);
}
}
public static void main(String args[]){
new BusinessService().test();
sessionFactory.close();
}
}

4.
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,
POSITION int not null,
FILENAME varchar(15) not null,
primary key (MONKEY_ID,POSITION)
); alter table IMAGES add index IDX_MONKEY(MONKEY_ID), add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);
Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)的更多相关文章
- 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章 映射值类型集合-004映射Map(<map-key>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- 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 ...
随机推荐
- hi,mongo!(1)
用了很多年的关系型数据库,想换一种思路,学习一下最近比较火的mongo数据库. 一.下载.安装mongo 下载地址:http://www.mongodb.org/downloads(官网) 官网的mo ...
- php学习日志(5)-解决Windows Live Writer错误:WindowsLive.Writer.CoreServices.HttpRequestHelper的类型初始值设定发生异常
以前用Windows Live Writer写日志都好好的,前几天用写完日志,点击发布,突然弹出意外错误:“WindowsLive.Writer.CoreServices.HttpRequestHel ...
- PHP自动识别字符集并完成转码详解
话不多说,直接上函数,这个函数是用来对字符串进行检查和转码的.需要的朋友可以过来参考下 因为自己使用字符编码一般的是utf-8编码,但如果对方的博客使用gb2312编码的话,POST过来就会出现乱码( ...
- vmware虚拟机上网:host-only
host-only配置 首先主机:vmware1要能共享本地连接这个网络,共享后vmware的ip会自动设置为如图 其次,vmware网络设置如图 最后,虚拟机设置如图 这样,主机与虚拟机之间就能pi ...
- DataSnap如何监控Tcp/IP客户端的连接情况
一个实例,如果客户端是TCP/IP是短连接的情况就没有必要了. 一.GlobVar.pas单元,定义应用系统全局数据类型及变量: unit GlobVar; interface uses System ...
- MAC 平台 QT编写iphone程序,加载iphone模拟器失败解决办法
本日这么多年一直做C++开发,最近要做QT项目,被QT做界面的新特性所吸引.QSS QML的确是亮点. 还有一个就是跨平台这方面,自己玩了玩. 用的QT 的开发包是在官网上下载 qt-opensour ...
- Nginx + Tomcat 动静分离实现负载均衡(转)
0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 # 定义Nginx运行的用户 和 用户组 如果对 ...
- 【BZOJ】【1833】【ZJOI2010】count 数字计数
数位DP Orz iwtwiioi 学习了一下用记忆化搜索来捉题的新姿势……但没学会TAT,再挖个坑(妈蛋难道对我来说数位DP就是个神坑吗……sigh) //BZOJ 1833 #include< ...
- boost序列化
#include <iostream> #include <boost/serialization/serialization.hpp> #include <boost/ ...
- Windows Server 2008下共享资源访问走捷径 (不用用户名 和 密码 访问共享)
1. 启用来宾帐号2. 共享目录添加“Guest”帐号3. “gpedit.msc”,打开对应系统的组策略编辑窗口;在该编辑窗口的左侧显示区域,依次展开“本地计算机策略”/“计算机配置”/“Windo ...