Java持久性API(JPA)第5講——關係建立

來源:互聯網
上載者:User
 

目標:建立實體類之間的關係來體現資料庫中實體之間的關聯關係。主要內容:n         建立資料庫表n         添加測試資料n         產生實體類n         關係分析n         主鍵類1、執行個體說明本執行個體類比簡單學產生績管理系統,為了使用說明一對一的關係,每個學生對一個一台電腦。涉及的實體有:學生、電腦、課程、成績。具體資訊參考《建立資料庫表》部分。涉及的關係如下:n         每個電腦屬於一個學生;n         學生和課程之間是多對多的關係;n         學生和成績之間是一對多的關係;n         課程和成績之間是一對多的關係。2、建立資料庫表資料庫表的定義語句如下:create table student(   s_id char(10) not null,   s_name char(10) not null,   s_birthday date,   primary key(s_id));create table computer(   c_id char(10) not null,   s_id char(10),   c_price float,   primary key(s_id),   foreign key(s_id) references student(s_id));create table course(   c_id char(8) not null,   c_name varchar(50) not null,   primary key(c_id));create table score(   s_id char(10) not null,   c_id char(8) not null,   grade float,   primary key(s_id,C_id),  foreign key(s_id) references student(s_id),   foreign key(c_id) references course(c_id));3、添加測試資料    添加測試資料的SQL語句如下:insert into student values(   '0511370101',   '張小菲',   '1987-03-04');insert into student values(   '0511370104',   '劉慧',   '1988-11-04');insert into student values(   '0511370102',   '付方',   '1988-9-9');insert into student values(   '0511370103',   '郭好',   '1987-6-5');  insert into computer values(   'cp001',   '0511370101',   8500);insert into computer values(   'cp002',   '0511370102',   8600);insert into computer values(   'cp003',   '0511370103',   8400);insert into computer values(   'cp004',   '0511370104',   8700);  insert into course values(   'JK0301',   'Java Web開發'); insert into course values(   'JK0302',   'Java EE'); insert into course values(   'JK0401',  '.NET'); insert into course values(   'JK0402',   'C#語言'); insert into score VALUES('0511370101','JK0301',70);insert into score VALUES('0511370101','JK0302',71);insert into score VALUES('0511370101','JK0401',72);insert into score VALUES('0511370101','JK0402',73);insert into score VALUES('0511370103','JK0301',90);insert into score VALUES('0511370103','JK0302',91);insert into score VALUES('0511370103','JK0401',92);insert into score VALUES('0511370103','JK0402',93); insert into score VALUES('0511370104','JK0301',80);insert into score VALUES('0511370104','JK0302',81);insert into score VALUES('0511370104','JK0401',82);insert into score VALUES('0511370104','JK0402',83); 4、使用嚮導產生持久單元    和第1講中的過程相同。5、使用嚮導產生實體類    和第1講中的過程相同。6、關係分析   1)一對一的關係學生和電腦之間的關係是一對一的關係在Student類中有如下表示關係的代碼:    @OneToOne(cascade = CascadeType.ALL, mappedBy = "student")    private Computer computer;在Computer類中有如下表示關係的代碼:    @JoinColumn(name = "s_id", referencedColumnName = "s_id", insertable = false, updatable = false)    @OneToOne    private Student student;     2)一對多的關係學生和成績之間是一對多的關係。在Student類中有如下表示關係的代碼:    @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")    private Collection<Score> scoreCollection;3)多對一的關係成績和學生之間的關係是多對一的關係。在成績類中有如下表示關係的代碼:    @JoinColumn(name = "s_id", referencedColumnName = "s_id", insertable = false, updatable = false)    @ManyToOne    private Student student;7、主鍵類在成績表中,學生學號和課程號組成聯合主鍵。在建立實體類的時候需要一個主鍵類來標識這個實體。主鍵類的代碼如下:/* * ScorePK.java * * Created on 2007年5月30日, 上午1:11 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package jpa; import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Embeddable; /** * Primary Key class ScorePK for entity class Score *  * @author Administrator */@Embeddablepublic class ScorePK implements Serializable {     @Column(name = "s_id", nullable = false)    private String sId;     @Column(name = "c_id", nullable = false)    private String cId;        /** Creates a new instance of ScorePK */    public ScorePK() {    }     /**     * Creates a new instance of ScorePK with the specified values.     * @param cId the cId of the ScorePK     * @param sId the sId of the ScorePK     */    public ScorePK(String cId, String sId) {        this.cId = cId;        this.sId = sId;    }     /**     * Gets the sId of this ScorePK.     * @return the sId     */    public String getSId() {        return this.sId;    }     /**     * Sets the sId of this ScorePK to the specified value.     * @param sId the new sId     */    public void setSId(String sId) {        this.sId = sId;    }     /**     * Gets the cId of this ScorePK.     * @return the cId     */    public String getCId() {        return this.cId;    }     /**     * Sets the cId of this ScorePK to the specified value.     * @param cId the new cId     */    public void setCId(String cId) {        this.cId = cId;    }     /**     * Returns a hash code value for the object. This implementation computes      * a hash code value based on the id fields in this object.     * @return a hash code value for this object.     */    @Override    public int hashCode() {        int hash = 0;        hash += (this.cId != null ? this.cId.hashCode() : 0);        hash += (this.sId != null ? this.sId.hashCode() : 0);        return hash;    }     /**     * Determines whether another object is equal to this ScorePK. The result is      * <code>true</code> if and only if the argument is not null and is a ScorePK object that      * has the same id field values as this object.     * @param object the reference object with which to compare     * @return <code>true</code> if this object is the same as the argument;     * <code>false</code> otherwise.     */    @Override    public boolean equals(Object object) {        // TODO: Warning - this method won't work in the case the id fields are not set        if (!(object instanceof ScorePK)) {            return false;        }        ScorePK other = (ScorePK)object;        if (this.cId != other.cId && (this.cId == null || !this.cId.equals(other.cId)))             return false;        if (this.sId != other.sId && (this.sId == null || !this.sId.equals(other.sId)))             return false;        return true;    }     /**     * Returns a string representation of the object. This implementation constructs      * that representation based on the id fields.     * @return a string representation of the object.     */    @Override    public String toString() {        return "jpa.ScorePK[cId=" + cId + ", sId=" + sId + "]";    }    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.