Java持久性API(JPA)第5讲——关系建立

 
目标:建立实体类之间的关系来体现数据库中实体之间的关联关系。
主要内容:
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
 */
@Embeddable
public 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 + "]";
    }
   
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值