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 ...
随机推荐
- PHP CodeIgniter(CI)去掉 index.php
去掉CodeIgniter(CI)默认url中的index.php的步骤: 1.打开apache的配置文件,conf/httpd.conf : LoadModule rewrite_module mo ...
- DataSnap如何监控Tcp/IP客户端的连接情况
一个实例,如果客户端是TCP/IP是短连接的情况就没有必要了. 一.GlobVar.pas单元,定义应用系统全局数据类型及变量: unit GlobVar; interface uses System ...
- 11g RAC R2 之Linux DNS 配置
在动手配置前,最好先了解下DNS的理论,以免犯不必要的错误.这都是被坑后的觉悟 -_-!!! Oracle 11g RAC 集群中引入了SCAN(SingleClientAccessName)的概念, ...
- unity--IOC框架资料整理
今天在网上找了一些unity资料研究,出了好多问题,编译无法通过,经人指点总算成功编译运行,做个笔记,整理如下: 一.下载unity: 二.在项目中添加Microsoft.Practices.Unit ...
- Activity的Launch mode详解 singleTask正解
Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance.以下逐一举例说明他们的区别: standard:Activity ...
- oracle-linux下挂载"移动硬盘" NTFS类型
环境: ORACLE-LINUX 5.7 全新移动硬盘(未使用过) 移动硬盘空间3T 在默认情况下,Linux系统不支持NTFS分区挂载 1.服务器: A服务器和B服务器为一套ORACLE-RAC,移 ...
- 快书包CEO徐智明反思:我犯下哪些错误
新浪科技 刘璨 1月23日,快书包CEO徐智明在微博上公开“叫卖”快书包,在业内引起不小反响.这家创立于2010年要做“网上711”的创业公司,曾以独特的“一小时送达”服务在业内成为关注焦点. “如果 ...
- JAVA算术运算符、关系运算符和位运算符
算术运算符 1.java的算数运算符包括+(加).-(减).*(乘)./(除).%(取余),在运算过程中出现的隐式转换原则和C语言一样:2. 高位数据向低位数据转化要使用强制转化: 关系运算符 1.j ...
- JPA学习---第五节:日期和枚举等字段类型的JPA映射
1.在上一节可在数据库中看到创建出来的表和字段,是通过 Entity bean 来创建的,而创建表名和字段名的规则是怎样的? 有类,代码如下: package learn.jpa.bean; impo ...
- Oracle 新建序列值
create sequence MSG_OUTBOX_ID_SEQ minvalue maxvalue start increment cache ;