The best way to configure the student course table in Hibernate

I have a question days ago on how to configure the student course relations. The main concern is how to express the score attribute. Finally, I find the following solution. To Configure it as a one-to-many map . The key is the course entity, and the value is the score(simple element).


I finally got the following solution:



public class Student implements java.io.Serializable {
    // Fields
    private String id;
    private Clazz clazz;
    private String studentName;
    private Integer studentAge;
    private Map courseScoreMap = new HashMap(0);

    ... omit the setters and getters

}


public class Course implements java.io.Serializable {
    // Fields
    private String id;
    private String courseName;

    // omit the setters and getters
}


public class Clazz implements java.io.Serializable {
    // Fields

    private String id;
    private String clazzName;
    private Set students = new HashSet(0);

    // omit the setters and getters
}

The following is the HBM file:

<hibernate-mapping package="com.hipposoft.school.domain">
    <class name="Clazz" table="clazz">      
        <id name="id" type="string">
            <column name="id" length="32">
            <generator class="uuid.hex"></generator>
        </column>
        <property name="clazzName" type="string">
            <column name="clazz_name" length="100">
        </column>
        <set name="students" inverse="true" cascade="all">
            <key>
                <column name="clazz_id" length="32" not-null="true">
            </column>
            <one-to-many class="Student">
        </one-to-many>
    </key>
</set>

<hibernate-mapping package="com.hipposoft.school.domain">
    <class name="Course" table="course">
        <id name="id" type="string">
            <column name="id" length="32">
            <generator class="uuid.hex"></generator>
        </column>
        <property name="courseName" type="string">
            <column name="course_name" length="100">
        </column>
    </property>
</id>

<hibernate-mapping package="com.hipposoft.school.domain">
    <class name="Student" table="student">
        <id name="id" type="string">
            <column name="id" length="32">
            <generator class="uuid.hex"></generator>
        </column>      
        <many-to-one name="clazz" class="Clazz" fetch="select">
            <column name="clazz_id" length="32" not-null="true">
        </column>
        <property name="studentName" type="string">
            <column name="student_name" length="100" not-null="true">
        </column>
        <property name="studentAge" type="integer">
            <column name="student_age">
        </column>
        <map name="courseScoreMap" cascade="all-delete-orphan" table="student_course_select">
            <key column="student_id"/>
            <map-key-many-to-many class="Course" column="course_id"/>
            <element type="double" column="score" />
        </map>

    </class>
</hibernate-mapping>



The following is the test code:

public void testMap() throws Exception{

    Session session = HibernateSessionFactory.getSession();

    session.beginTransaction();

    Clazz clazz = new Clazz();
    clazz.setClazzName("Gao San");

    Student s = new Student(clazz,"James");
    clazz.getStudents().add(s);

    // save the student and the class
    session.save(clazz);

    Course course = new Course("语文");
    session.save(course);


    session.flush();

    s.getCourseScoreMap().put(course, new Double(80.5));

    // save the course score
    session.update(s);

    session.getTransaction().commit();
}



In the new way, the above code is easy to write and read, as it is very natual to do so. And also the disadvantage is that we can not config a reverse relation to let a course associate to a student set, as hibernate didn't support that.
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:– JavaScript 中的患者数据管理系统 患者数据管理系统是为医院开发的 node JS 项目。通过使用此系统,您可以轻松访问患者数据,它具有成本效益,可改善患者护理和数据安全性。不仅如此,它还减少了错误范围。在运行项目之前,您需要下载 node.js。 这个患者数据管理项目包含 javascript、node.js 和 CSS。我们必须让服务器监听端口 3000,并使用 JSON 在客户端和服务器之间交换数据。这个项目会不断询问您有关插件更新的信息,因此请保持互联网畅通。此系统允许您执行 crud 操作。在这里,您是系统的管理员。您还可以添加所需的员工人数。此外,您还可以更新患者记录。该系统功能齐全且功能齐全。 要运行此项目,您需要在计算机上安装NodeJS并使用现代浏览器,例如 Google Chrome、  Mozilla Firefox。ReactJS项目中的此项目可免费下载源代码。有关项目演示,请查看下面的图像滑块。 对于手动安装 1.将主项目文件夹解压到任意目录 2.从 cmd 设置项目目录的路径 3. 输入命令“npm install” 4.完成后输入命令“npm start” 5.现在,您将获得一个 localhost:portnumber,并转到该 URL 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值