Hibernate逍遥游记-第12章 映射值类型集合-002映射Bag(<idbag><collection-id>)
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> <idbag name="images" table="IMAGES" lazy="true">
<collection-id type="long" column="ID">
<generator class="increment"/>
</collection-id>
<key column="MONKEY_ID" />
<element column="FILENAME" type="string" not-null="true"/>
</idbag> </class> </hibernate-mapping>
3.
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;
} }
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) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
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());
Iterator it=monkey.getImages().iterator();
while(it.hasNext()){
String fileName=(String)it.next();
System.out.println(monkey.getName()+" "+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(
ID bigint not null,
MONKEY_ID bigint not null,
FILENAME varchar(15) not null,
primary key (ID)
); alter table IMAGES add index IDX_MONKEY(MONKEY_ID), add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);
6.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sampledb
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
1234
</property> <property name="show_sql">true</property> <mapping resource="mypack/Monkey.hbm.xml" />
</session-factory>
</hibernate-configuration>
Hibernate逍遥游记-第12章 映射值类型集合-002映射Bag(<idbag><collection-id>)的更多相关文章
- 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章 映射值类型集合-003映射List(<list-index>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- 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逍遥游记-第9章 Hibernate的映射类型
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 ...
随机推荐
- c#中的结构与枚举
结构 与c++不同的是,结构应该定义在命名空间或者类里面,成员变量叫字段,字段并且有访问控制符,每个字段前要加一个下划线 例子 using System; using System.Collectio ...
- WPF-控件-编辑圆角TextBox
使用模板 代码如下: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xm ...
- Ubuntu下Apache+php+mysql网站架设详解
目录 1 基础 2 安装 2.1 安装LAMP 2.2 图形化管理软件(可选) 2.2.1 安装webmin 2.2.2 安装phpmyadmin 3 配置文件路径 3.1 常用命令 3.2 配置ap ...
- 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据
jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...
- sharepoint 认证
MCPD http://www.microsoft.com/learning/en/us/mcpd-certification.aspx#fbid=YktyKIYXeFg Exam 70-573: T ...
- Eclipse 3.7(代号Indigo) 中文字体太小解决办法(转)
升级到3.7Eclipse最直观的反映就是,中文怎么那么小啊---- 相当不方便. 其实这是Eclipse的默认字体换了,以前的一直是Courier New,现在修改字体也找不到了,算了不找了. 这次 ...
- HTTP - 内容编码
HTTP 应用程序有时在发送之前需要对内容进行编码.例如,在把很大的 HTML 文档发送给通过慢速连接上来的客户端之前,服务器可能就会对它进行压缩,这样有助于减少传输实体的时间. 内容编码过程 内容编 ...
- mysql merge
merge 是一组 myisam 表的组合, 锁住一个 merge 表它会吧底下所有的表全给锁住. 创建只读表 )) engine = merge union (t1,t2); 创建可插入的表, (以 ...
- CI_Autocomplete_2.0.php轻松实现Bebeans与Codeigniter的智能提示
在你的NetBeans项目下建立一个CI_Autocomplete_2.0.php的文件,粘贴以下代码:(codeigniter太旧了,其实性能不行,应该没人更了,换了吧,别学这玩意了,坑人) < ...
- EXT经验--查询items的xtype
前言:EXT由多个组件组成,每个组件可配置多个子组件(items),而每个子组件也可嵌套多个子组件(items)--给人一种子子孙孙无穷匮也的印象,这对于初学者引来一个很重要的问题,特别是阅读他人编写 ...