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 ...
随机推荐
- nginx服务器配置多域名
nginx服务器支持配置多站点,我们可以通过配置子域名让你的一个域名下放置多个项目. 那么如何实现这个过程呢? 网络上的许多方案,有些写的过于繁杂,有些则是配置有误,或者说,有些配置项是要根据自己的主 ...
- PHP实现根据浏览器跳转不同语言页面代码
以下是对使用PHP实现根据浏览器跳转不同语言页面的代码进行了介绍,需要的朋友可以过来参考下 代码: <?php /** * 根据不同浏览器跳转不同页面 * 来源:www.jbxue.com * ...
- mysql时间处理
两种方式,一个是在数据库查询的时候就截取,另一个就是在使用的时候截取. 1.数据库 select date_format(日期字段,’%Y-%m-%d’) as ‘日期’ from test 2.ja ...
- Linux&UNIX上卸载GoldenGate的方法
1. Log on to the database server (as oracle) where the GoldenGate software is installed. [root@oracl ...
- 通过 Javacore 了解线程运行状况
Javacore 是一个当前 JVM 运行状态的快照.通过对Javacore 的分析,可以了解在 JVM 中运行的应用程序的当前状态,比如是否“卡”在某一点上,或在某些代码上运行时间太长. Javac ...
- XHTML1.0对HTML4.0的改进
1.XHTML借鉴了XML的写法,语法更加严格: 2.XHTML实现了把页面样式和内容分离了,废弃了HTML4.0中表示样式的标签和属性,推荐使用CSS样式来描述页面的样式. XHTML1.0 分为两 ...
- impdp ORA-29913: error in executing ODCIEXTTABLEOPEN callout
1.数据导出时的日志 ;;; Export: Release :: Copyright (c) , , Oracle and/or its affiliates. All rights reserve ...
- thinkpad t440p 解决无线网卡驱动
$ wget https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1239578/+attachment/4057550/+files/rtl_9 ...
- Java 图形编程 一:入门
package second; import java.awt.Color; import java.awt.Frame; import java.awt.event.WindowAdapter; i ...
- win下php5.5.12装不上memcache扩展
WAMP这个集成环境里,php目录下有个php.ini,apache/bin下也有一个php.ini,环境使用的是apache下的,改apache