posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          EJB學(xué)習(xí)日記(12)

          Posted on 2007-09-27 14:25 semovy 閱讀(354) 評論(0)  編輯  收藏 所屬分類: EJB
          實體BEAN的七種關(guān)系之---------一對多雙向
           One-to-Many Bidirectional Relationship

          在實際生活中,一對多的雙向關(guān)系也是有的,我們在這里舉一個人和車的例子,人可以有很多車,車也必須要有一個主人(只要它是合法的),我們可以知道一個人有多少輛車,我們也可以通過任意一輛車牌號查到這輛車的主人是誰,這種關(guān)系不像人和電話,電話是很容易換的,并且很多號碼是不用身份證的,但是車必須要上牌并且要用身份證的,這樣才好管理嘛.下面我們來看代碼吧

          還是一樣,先定義一個Person類(我們的Person已經(jīng)一天一天的變大了:)

          /*
           * Person.java
           * 
           * Created on 2007-9-15, 0:11:58
           * 
           * To change this template, choose Tools | Templates
           * and open the template in the editor.
           
          */

          package lbf.entitybean.test1;

          import java.io.Serializable;
          import java.util.List;
          import javax.persistence.CascadeType;
          import javax.persistence.Entity;
          import javax.persistence.GeneratedValue;
          import javax.persistence.GenerationType;
          import javax.persistence.Id;
          import javax.persistence.JoinColumn;
          import javax.persistence.ManyToOne;
          import javax.persistence.OneToMany;
          import javax.persistence.OneToOne;

          /**
           *
           * 
          @author Admin
           
          */
          @Entity
          public class Person implements Serializable {
              
          private static final long serialVersionUID = 1L;
              
          private Long id;
              
          private String name;
              
          private String sex;
              
          private int age;
              
          private Address address;
              
          private List<Phone> phones;
              
          private IDCard idCard;
              
          private Country country;
              
          private List<Car> cars;
              
              @OneToMany(cascade
          =CascadeType.ALL,mappedBy="person")
              
          public List<Car> getCars() {
                  
          return cars;
              }

              
          public void setCars(List<Car> cars) {
                  
          this.cars = cars;
              }
              
              @ManyToOne(cascade
          =CascadeType.ALL)
              @JoinColumn(name
          ="countryID")
              
          public Country getCountry() {
                  
          return country;
              }

              
          public void setCountry(Country country) {
                  
          this.country = country;
              }

              @OneToOne(cascade
          =CascadeType.ALL)
              
          public IDCard getIdCard() {
                  
          return idCard;
              }

              
          public void setIdCard(IDCard idCard) {
                  
          this.idCard = idCard;
              }
              @OneToMany(cascade
          =CascadeType.ALL)
              
          public List<Phone> getPhones() {
                  
          return phones;
              }

              
          public void setPhones(List<Phone> phones) {
                  
          this.phones = phones;
              }
              @OneToOne(cascade
          ={CascadeType.ALL})
              
          public Address getAddress() {
                  
          return address;
              }

              
          public void setAddress(Address address) {
                  
          this.address = address;
              }
              
          public int getAge() {
                  
          return age;
              }

              
          public void setAge(int age) {
                  
          this.age = age;
              }

              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
          public String getSex() {
                  
          return sex;
              }

              
          public void setSex(String sex) {
                  
          this.sex = sex;
              }
              
          public void setId(Long id) {
                  
          this.id = id;
              }

              @Id
              @GeneratedValue(strategy 
          = GenerationType.AUTO)
              
          public Long getId() {
                  
          return id;
              }

          }

          然后我們再定義一個Car類

          /*
           * Car.java
           * 
           * Created on 2007-9-20, 19:40:39
           * 
           * To change this template, choose Tools | Templates
           * and open the template in the editor.
           
          */

          package lbf.entitybean.test1;

          import java.io.Serializable;
          import java.util.Date;
          import javax.persistence.CascadeType;
          import javax.persistence.Entity;
          import javax.persistence.GeneratedValue;
          import javax.persistence.GenerationType;
          import javax.persistence.Id;
          import javax.persistence.JoinColumn;
          import javax.persistence.ManyToOne;
          import javax.persistence.Temporal;

          /**
           *
           * 
          @author hadeslee
           
          */
          @Entity
          public class Car implements Serializable {
              
          private static final long serialVersionUID = 1L;
              
          private Long id;
              
          private Person person;
              
          private String carNumber;
              
          private String carName;
              
          private Date date;

              
          public String getCarName() {
                  
          return carName;
              }

              
          public void setCarName(String carName) {
                  
          this.carName = carName;
              }

              
          public String getCarNumber() {
                  
          return carNumber;
              }

              
          public void setCarNumber(String carNumber) {
                  
          this.carNumber = carNumber;
              }

              @Temporal(javax.persistence.TemporalType.DATE)
              
          public Date getCarDate() {
                  
          return date;
              }

              
          public void setCarDate(Date date) {
                  
          this.date = date;
              }
              @ManyToOne(cascade
          =CascadeType.ALL,optional=false)
              @JoinColumn(name
          ="personID")
              
          public Person getPerson() {
                  
          return person;
              }

              
          public void setPerson(Person person) {
                  
          this.person = person;
              }
              
          public void setId(Long id) {
                  
          this.id = id;
              }

              @Id
              @GeneratedValue(strategy 
          = GenerationType.AUTO)
              
          public Long getId() {
                  
          return id;
              }

          }

          從這里我們可以看到雙向的一對多其實和雙向的一對一差不多,一個是關(guān)系的主控端,一個是關(guān)系的被維護(hù)端.
          ,在一對一的雙向關(guān)系里面,我們可以讓任意一方做關(guān)系的主控端,任意一方做關(guān)系的被維護(hù)端(mapped 來注釋),但是一對多或者說多一對的雙向關(guān)系中,主控端必須是多的那一方,也就是Car,在它里面起一個外鍵指向Person類的主鍵,然后我們在對它們進(jìn)行處理的時候,必須兩端都要設(shè)置一下,才能保證數(shù)據(jù)的更新是如我們所愿的,比如

          Person p=new Person();
          Car car=new Car();
          ....
          p.setCar(car);
          car.setPerson(p);
          entityManager.persist(p);
          這個時候會把car和p都寫入數(shù)據(jù)庫,當(dāng)我們要把car的主人換成p1時,需要做如下動作
          p.getCars().remove(car);
          car.setPerson(p1);
          p1.setCar(car);
          這樣就可以了,其實只要我們保持一個良好的習(xí)慣,那就是更新關(guān)系的時候,雙方都更新一下,這樣就不容易出錯了.


          主站蜘蛛池模板: 麻栗坡县| 宿州市| 元氏县| 临武县| 囊谦县| 永德县| 绍兴市| 噶尔县| 宁夏| 马尔康县| 定州市| 都昌县| 吉水县| 东乡族自治县| 吉木乃县| 淮滨县| 松潘县| 仪陇县| 乌鲁木齐市| 溧阳市| 丰台区| 呼伦贝尔市| 建昌县| 涞源县| 得荣县| 鄢陵县| 龙南县| 门头沟区| 改则县| 偃师市| 慈溪市| 镇雄县| 北流市| 苗栗市| 安阳县| 威远县| 彭阳县| 吉林省| 宝应县| 华安县| 慈利县|