• Hibernate逍遥游记-第12章 映射值类型集合-002映射Bag(<idbag><collection-id>)


    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>
  • 相关阅读:
    获取地址栏的key-value形式的值(包括重复的key值)形成对象--重复的变成数组形式
    vue---定时循环setInterval
    动画效果----webkit-animation-----动画背景变化transition:background-color 1s linear;
    background-clip--背景是否填充
    box-sizing盒子的大小,修改文字种类而保持字体大小不变font-size-adjust,----块级和内联display---盒子阴影box-shadow---对盒子中容纳不下的内容的显示overflow
    地图api学习第一天
    文字添加阴影
    css循环样式: nth-child(n)
    es6语法
    将中国标准时间--转换为yyyy-MM-dd 时分秒
  • 原文地址:https://www.cnblogs.com/shamgod/p/5299470.html
Copyright © 2020-2023  润新知