1.

2.

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping
  3. PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping >
  6.  
  7. <class name="mypack.Monkey" table="MONKEYS" >
  8. <id name="id" type="long" column="ID">
  9. <generator class="increment"/>
  10. </id>
  11.  
  12. <property name="name" type="string" >
  13. <column name="NAME" length="15" />
  14. </property>
  15.  
  16. <property name="age" type="int" >
  17. <column name="AGE" />
  18. </property>
  19.  
  20. <idbag name="images" table="IMAGES" lazy="true">
  21. <collection-id type="long" column="ID">
  22. <generator class="increment"/>
  23. </collection-id>
  24. <key column="MONKEY_ID" />
  25. <element column="FILENAME" type="string" not-null="true"/>
  26. </idbag>
  27.  
  28. </class>
  29.  
  30. </hibernate-mapping>

3.

  1. package mypack;
  2.  
  3. import java.io.Serializable;
  4. import java.util.List;
  5. import java.util.ArrayList;
  6.  
  7. public class Monkey implements Serializable {
  8. private Long id;
  9. private String name;
  10. private int age;
  11. private List images=new ArrayList();
  12.  
  13. /** full constructor */
  14. public Monkey(String name, int age,List images) {
  15. this.name = name;
  16. this.age=age;
  17. this.images = images;
  18. }
  19.  
  20. /** default constructor */
  21. public Monkey() {
  22. }
  23.  
  24. /** minimal constructor */
  25. public Monkey(List images) {
  26. this.images = images;
  27. }
  28.  
  29. public Long getId() {
  30. return this.id;
  31. }
  32.  
  33. public void setId(Long id) {
  34. this.id = id;
  35. }
  36.  
  37. public String getName() {
  38. return this.name;
  39. }
  40.  
  41. public void setName(String name) {
  42. this.name = name;
  43. }
  44.  
  45. public int getAge() {
  46. return this.age;
  47. }
  48.  
  49. public void setAge(int age) {
  50. this.age = age;
  51. }
  52.  
  53. public List getImages() {
  54. return this.images;
  55. }
  56.  
  57. public void setImages(List images) {
  58. this.images = images;
  59. }
  60.  
  61. }

4.

  1. package mypack;
  2.  
  3. import org.hibernate.*;
  4. import org.hibernate.cfg.Configuration;
  5. import java.util.*;
  6. import java.sql.*;
  7.  
  8. public class BusinessService{
  9. public static SessionFactory sessionFactory;
  10. static{
  11. try{
  12. Configuration config = new Configuration().configure();
  13. sessionFactory = config.buildSessionFactory();
  14. }catch(RuntimeException e){e.printStackTrace();throw e;}
  15.  
  16. }
  17.  
  18. public void saveMonkey(Monkey monkey){
  19. Session session = sessionFactory.openSession();
  20. Transaction tx = null;
  21. List results=new ArrayList();
  22. try {
  23. tx = session.beginTransaction();
  24. session.save(monkey);
  25. tx.commit();
  26. }catch (RuntimeException e) {
  27. if (tx != null) {
  28. tx.rollback();
  29. }
  30. throw e;
  31. } finally {
  32. session.close();
  33. }
  34. }
  35.  
  36. public Monkey loadMonkey(long id){
  37. Session session = sessionFactory.openSession();
  38. Transaction tx = null;
  39. try {
  40. tx = session.beginTransaction();
  41. Monkey monkey=(Monkey)session.get(Monkey.class,new Long(id));
  42. Hibernate.initialize(monkey.getImages());
  43. tx.commit();
  44. return monkey;
  45. }catch (RuntimeException e) {
  46. if (tx != null) {
  47. // Something went wrong; discard all partial changes
  48. tx.rollback();
  49. }
  50. throw e;
  51. } finally {
  52. // No matter what, close the session
  53. session.close();
  54. }
  55. }
  56.  
  57. public void test(){
  58. List images=new ArrayList();
  59. images.add("image1.jpg");
  60. images.add("image4.jpg");
  61. images.add("image2.jpg");
  62. images.add("image2.jpg");
  63. images.add("image5.jpg");
  64.  
  65. Monkey monkey=new Monkey("Tom",21,images);
  66. saveMonkey(monkey);
  67.  
  68. monkey=loadMonkey(1);
  69. printMonkey(monkey);
  70.  
  71. }
  72.  
  73. private void printMonkey(Monkey monkey){
  74. System.out.println(monkey.getImages().getClass().getName());
  75. Iterator it=monkey.getImages().iterator();
  76. while(it.hasNext()){
  77. String fileName=(String)it.next();
  78. System.out.println(monkey.getName()+" "+fileName);
  79. }
  80. }
  81. public static void main(String args[]){
  82. new BusinessService().test();
  83. sessionFactory.close();
  84. }
  85. }

5.

  1. drop database if exists SAMPLEDB;
  2. create database SAMPLEDB;
  3. use SAMPLEDB;
  4.  
  5. create table MONKEYS (
  6. ID bigint not null,
  7. NAME varchar(15),
  8. AGE int,
  9. primary key (ID)
  10. );
  11.  
  12. create table IMAGES(
  13. ID bigint not null,
  14. MONKEY_ID bigint not null,
  15. FILENAME varchar(15) not null,
  16. primary key (ID)
  17. );
  18.  
  19. alter table IMAGES add index IDX_MONKEY(MONKEY_ID), add constraint FK_MONKEY foreign key (MONKEY_ID) references MONKEYS(ID);

6.

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE hibernate-configuration
  3. PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7. <session-factory>
  8. <property name="dialect">
  9. org.hibernate.dialect.MySQLDialect
  10. </property>
  11. <property name="connection.driver_class">
  12. com.mysql.jdbc.Driver
  13. </property>
  14. <property name="connection.url">
  15. jdbc:mysql://localhost:3306/sampledb
  16. </property>
  17. <property name="connection.username">
  18. root
  19. </property>
  20. <property name="connection.password">
  21. 1234
  22. </property>
  23.  
  24. <property name="show_sql">true</property>
  25.  
  26. <mapping resource="mypack/Monkey.hbm.xml" />
  27. </session-factory>
  28. </hibernate-configuration>

Hibernate逍遥游记-第12章 映射值类型集合-002映射Bag(<idbag><collection-id>)的更多相关文章

  1. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  2. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序(<order-by>\<sort>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  3. Hibernate逍遥游记-第12章 映射值类型集合-004映射Map(<map-key>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  4. Hibernate逍遥游记-第12章 映射值类型集合-003映射List(<list-index>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  5. Hibernate逍遥游记-第12章 映射值类型集合-001映射set(<element>)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  6. 攻城狮在路上(壹) Hibernate(十)--- 映射值类型集合

    一.映射Set(集):未排序,无重复. 实例代码: <set name="images" table="IMAGES" lazy="true&q ...

  7. Hibernate逍遥游记-第9章 Hibernate的映射类型

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  8. Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  9. Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

随机推荐

  1. PHP CodeIgniter(CI)去掉 index.php

    去掉CodeIgniter(CI)默认url中的index.php的步骤: 1.打开apache的配置文件,conf/httpd.conf : LoadModule rewrite_module mo ...

  2. DataSnap如何监控Tcp/IP客户端的连接情况

    一个实例,如果客户端是TCP/IP是短连接的情况就没有必要了. 一.GlobVar.pas单元,定义应用系统全局数据类型及变量: unit GlobVar; interface uses System ...

  3. 11g RAC R2 之Linux DNS 配置

    在动手配置前,最好先了解下DNS的理论,以免犯不必要的错误.这都是被坑后的觉悟 -_-!!! Oracle 11g RAC 集群中引入了SCAN(SingleClientAccessName)的概念, ...

  4. unity--IOC框架资料整理

    今天在网上找了一些unity资料研究,出了好多问题,编译无法通过,经人指点总算成功编译运行,做个笔记,整理如下: 一.下载unity: 二.在项目中添加Microsoft.Practices.Unit ...

  5. Activity的Launch mode详解 singleTask正解

    Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance.以下逐一举例说明他们的区别: standard:Activity ...

  6. oracle-linux下挂载"移动硬盘" NTFS类型

    环境: ORACLE-LINUX 5.7 全新移动硬盘(未使用过) 移动硬盘空间3T 在默认情况下,Linux系统不支持NTFS分区挂载 1.服务器: A服务器和B服务器为一套ORACLE-RAC,移 ...

  7. 快书包CEO徐智明反思:我犯下哪些错误

    新浪科技 刘璨 1月23日,快书包CEO徐智明在微博上公开“叫卖”快书包,在业内引起不小反响.这家创立于2010年要做“网上711”的创业公司,曾以独特的“一小时送达”服务在业内成为关注焦点. “如果 ...

  8. JAVA算术运算符、关系运算符和位运算符

    算术运算符 1.java的算数运算符包括+(加).-(减).*(乘)./(除).%(取余),在运算过程中出现的隐式转换原则和C语言一样:2. 高位数据向低位数据转化要使用强制转化: 关系运算符 1.j ...

  9. JPA学习---第五节:日期和枚举等字段类型的JPA映射

    1.在上一节可在数据库中看到创建出来的表和字段,是通过 Entity bean 来创建的,而创建表名和字段名的规则是怎样的? 有类,代码如下: package learn.jpa.bean; impo ...

  10. Oracle 新建序列值

    create sequence MSG_OUTBOX_ID_SEQ minvalue maxvalue start increment cache ;