首页 新闻 会员 周边 捐助

Hibernate中含有外键的数据表

0
[已关闭问题] 关闭于 2019-01-14 09:56

定义了一个学生表含有stu_id列,一个课程表,含有C_id列,都是主键;定义了一个分数表,含有

如图列,S_id是学生表的外键,S_Co_id是课程表的外键

反向工程生成的文件:

Score.java

package com.ctt.stu.orm;

/**
 * Score entity. @author MyEclipse Persistence Tools
 */

public class Score implements java.io.Serializable {

    // Fields

    private ScoreId id;
    private String SScore;

    // Constructors

    /** default constructor */
    public Score() {
    }

    /** minimal constructor */
    public Score(ScoreId id) {
        this.id = id;
    }

    /** full constructor */
    public Score(ScoreId id, String SScore) {
        this.id = id;
        this.SScore = SScore;
    }

    // Property accessors

    public ScoreId getId() {
        return this.id;
    }

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

    public String getSScore() {
        return this.SScore;
    }

    public void setSScore(String SScore) {
        this.SScore = SScore;
    }

}
View Code

ScoreId.java

package com.ctt.stu.orm;

/**
 * ScoreId entity. @author MyEclipse Persistence Tools
 */

public class ScoreId implements java.io.Serializable {

    // Fields

    private Student student;
    private Course course;

    // Constructors

    /** default constructor */
    public ScoreId() {
    }

    /** full constructor */
    public ScoreId(Student student, Course course) {
        this.student = student;
        this.course = course;
    }

    // Property accessors

    public Student getStudent() {
        return this.student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Course getCourse() {
        return this.course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public boolean equals(Object other) {
        if ((this == other))
            return true;
        if ((other == null))
            return false;
        if (!(other instanceof ScoreId))
            return false;
        ScoreId castOther = (ScoreId) other;

        return ((this.getStudent() == castOther.getStudent()) || (this
                .getStudent() != null && castOther.getStudent() != null && this
                .getStudent().equals(castOther.getStudent())))
                && ((this.getCourse() == castOther.getCourse()) || (this
                        .getCourse() != null && castOther.getCourse() != null && this
                        .getCourse().equals(castOther.getCourse())));
    }

    public int hashCode() {
        int result = 17;

        result = 37 * result
                + (getStudent() == null ? 0 : this.getStudent().hashCode());
        result = 37 * result
                + (getCourse() == null ? 0 : this.getCourse().hashCode());
        return result;
    }

}
View Code

Score.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.ctt.stu.orm.Score" table="score" schema="dbo" catalog="Stu">
        <composite-id name="id" class="com.ctt.stu.orm.ScoreId">
            <key-many-to-one name="student" class="com.ctt.stu.orm.Student">
                <column name="S_id" />
            </key-many-to-one>
            <key-many-to-one name="course" class="com.ctt.stu.orm.Course">
                <column name="S_Co_id" />
            </key-many-to-one>
        </composite-id>
        <property name="SScore" type="java.lang.String">
            <column name="S_score" />
        </property>
    </class>
</hibernate-mapping>
View Code

由于多表连接,导致出现了ScoreId.java类,而且没有具体的get、set方法,不知该如何实现增删改查,望各路大神指点一二

 

曹婷婷的主页 曹婷婷 | 初学一级 | 园豆:149
提问于:2018-06-15 21:38
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册