我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          基于JPA的CRUD(OneToMany)

          先建數(shù)據(jù)庫:
          use test;

          create table person
          (
          id 
          int primary key AUTO_INCREMENT,
          username varchar(
          20) not null,
          password varchar(
          20) not null
          );

          create table mail
          (
          id 
          int primary KEY AUTO_INCREMENT,
          email varchar(
          50) not null,
          pid 
          int null
          );
          select 
          * from person;
          select 
          * from mail;
          Person表和Mail表不存在物理方面的OneToMany關系,這
          種關系是通過Hibernate來維護的.
          hibernate.cgf.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE hibernate-configuration PUBLIC
              
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
          <hibernate-configuration>
              
          <session-factory>
                  
          <property name="dialect">
                      org.hibernate.dialect.MySQLDialect
                  
          </property>
                  
          <property name="show_sql">true</property>
                  
          <property name="myeclipse.connection.profile">mysql</property>
                  
          <property name="connection.url">
                      jdbc:mysql:
          //localhost/test
                  </property>
                  
          <property name="connection.username">root</property>
                  
          <property name="connection.password">root</property>
                  
          <property name="connection.driver_class">
                      com.mysql.jdbc.Driver
                  
          </property>
                  
          <property name="transaction.flush_before_completion">true</property>
                  
          <mapping class="com.vo.Person" />
                  
          <mapping class="com.vo.Mail" />
              
          </session-factory>
          </hibernate-configuration>

          Person.java:
          package com.vo;

          import java.io.Serializable;
          import java.util.HashSet;
          import java.util.Set;
          import javax.persistence.CascadeType;
          import javax.persistence.Column;
          import javax.persistence.Entity;
          import javax.persistence.FetchType;
          import javax.persistence.GeneratedValue;
          import javax.persistence.GenerationType;
          import javax.persistence.Id;
          import javax.persistence.JoinColumn;
          import javax.persistence.OneToMany;
          import javax.persistence.Table;

          @SuppressWarnings(
          "serial")
          @Entity
          @Table(name 
          = "person")
          public class Person implements Serializable
          {
              
              
          private Integer id;
              
          private String username;
              
          private String password;
              
          private Set<Mail> mails = new HashSet<Mail>();
              @OneToMany(cascade
          =CascadeType.ALL,fetch=FetchType.LAZY) //映射為單一對多關系
              
          //@Basic(fetch=FetchType.LAZY) //和上面的fecth一樣的效果,都是延時初始
              @JoinColumn(name="pid")             //加入要映射的列(外鍵列)
              public Set<Mail> getMails()
              
          {
                  
          return mails;
              }


              
          public void setMails(Set<Mail> mails)
              
          {
                  
          this.mails = mails;
              }

              @Id
              @GeneratedValue(strategy 
          = GenerationType.AUTO)
              @Column(name
          ="id")
              
          public Integer getId()
              
          {
                  
          return id;
              }


              
          public void setId(Integer id)
              
          {
                  
          this.id = id;
              }


              
          public String getUsername()
              
          {
                  
          return username;
              }

              @Column(name
          ="username",nullable=false)
              
          public void setUsername(String username)
              
          {
                  
          this.username = username;
              }

              @Column(name
          ="password",nullable=false)
              
          public String getPassword()
              
          {
                  
          return password;
              }


              
          public void setPassword(String password)
              
          {
                  
          this.password = password;
              }

          }


          Mail.java:
          package com.vo;

          import java.io.Serializable;

          import javax.persistence.Column;
          import javax.persistence.Entity;
          import javax.persistence.GeneratedValue;
          import javax.persistence.GenerationType;
          import javax.persistence.Id;
          import javax.persistence.Table;

          @SuppressWarnings(
          "serial")
          @Entity
          @Table(name 
          = "mail")
          public class Mail implements Serializable
          {
              
              
          private Integer id;
              
          private String email;

              @Id
              @GeneratedValue(strategy 
          = GenerationType.AUTO)
              @Column(name
          ="id")
              
          public Integer getId()
              
          {
                  
          return id;
              }


              
          public void setId(Integer id)
              
          {
                  
          this.id = id;
              }

              @Column(name
          ="email",nullable=false)
              
          public String getEmail()
              
          {
                  
          return email;
              }


              
          public void setEmail(String email)
              
          {
                  
          this.email = email;
              }

          }


          測試類:
          package com.test;

          import java.util.Iterator;
          import java.util.Set;

          import org.hibernate.Session;
          import org.hibernate.Transaction;
          import org.hibernate.cfg.AnnotationConfiguration;
          import org.junit.After;
          import org.junit.Before;
          import org.junit.Test;

          import com.vo.Mail;
          import com.vo.Person;

          public class PersonTest
          {
              
          private Session session;
              
          private Transaction transaction;

              @Before
              
          public void before()
              
          {
                  session 
          = new AnnotationConfiguration().configure()
                          .buildSessionFactory().openSession();
                  transaction 
          = session.getTransaction();
              }


              @After
              
          public void after()
              
          {
                  session.close();
              }

              
              @Test
              
          public void save()
              
          {
                  transaction.begin();
                  Person person 
          = new Person();
                  person.setUsername(
          "zdw");
                  person.setPassword(
          "admin");
                  
                  Mail m1 
          = new Mail();
                  m1.setEmail(
          "a@live.com");
                  Mail m2 
          = new Mail();
                  m2.setEmail(
          "b@live.com");
                  
                  person.getMails().add(m1);
                  person.getMails().add(m2);
                  session.save(person);
                  transaction.commit();
              }

              
              
              @Test
              
          public void findById()
              
          {
                  Person person 
          = (Person) session.load(Person.class1);
                  Set
          <Mail> mails = person.getMails();
                  
          if(mails.size() > 0)
                  
          {
                      
          for(Iterator<Mail> i = mails.iterator(); i .hasNext();)
                      
          {
                          Mail m 
          = i.next();
                          System.out.println(m.getEmail());
                      }

                  }

              }

              
              
              @Test
              
          public void delete()
              
          {
                  transaction.begin();
                  Person person 
          = (Person) session.load(Person.class1);
                  session.delete(person);
                  transaction.commit();
              }

          }

              

          源碼可在我的網(wǎng)盤下載.
          點此下載

          posted on 2007-11-30 15:42 々上善若水々 閱讀(9170) 評論(0)  編輯  收藏 所屬分類: Hibernate

          主站蜘蛛池模板: 泰和县| 灵寿县| 罗源县| 延安市| 武宁县| 介休市| 廉江市| 梅州市| 博乐市| 华容县| 九龙城区| 蓬莱市| 安乡县| 临邑县| 宣威市| 同仁县| 游戏| 宜川县| 榕江县| 石阡县| 通许县| 孙吴县| 偏关县| 尼勒克县| 岳普湖县| 阿克苏市| 上思县| 达拉特旗| 长武县| 南昌县| 昌宁县| 德惠市| 秦皇岛市| 陇川县| 磐石市| 泾川县| 阳泉市| 兴国县| 临邑县| 松滋市| 蓬溪县|