The best way to configure the student course table in Hibernate
来源:互联网 发布:java reactor模式 编辑:程序博客网 时间:2024/05/18 15:25
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:
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.
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.
- The best way to configure the student course table in Hibernate
- ADF: The best way to indicate for records in a table the existence of details
- The best way to handle the LazyInitializationException
- The best way to fix a bug
- The Best Way to Learn ASP.NET
- The best way to delete code
- The best way to improve your listening
- 005:What's the best way to iterate over the items in a HashMap?
- How to configure the C3P0 connection pool in Hibernate
- What is the best way to implement a heartbeat in C++ to check for socket connectivity?
- The best way to check the GC status
- The best way to Debugging SQL Server 2005 Stored Procedures in Visual Stuido.2005
- What's the best way to hide a tab in a TabNavigator?
- Is there any best way to reduce the size of ibdata in mysql.?
- What is the best way to paginate results in SQL Server
- The best way to deal with large files in Python style
- The best way to get unique elements of a list in Python
- Another way to HOOK in the kernel
- MSDN windows VISTA 中文版已经发布
- Java Swing 中三种事件处理方法比较
- PreparedStatement-jsp中优化数据查询的一个东西
- oracle的复杂sql语句复习
- J2ee程序员应该掌握的linux知识
- The best way to configure the student course table in Hibernate
- Tapestry绑定的作用与实现
- WEB2.0概念诠释
- 斑点狗
- 用 Eclipse 进行可视化 Java 界面设计
- 创建 SWT 项目的原生库导入问题
- 出圈题目
- 一个证明表达式的求值顺序只是部分由操作符的优先级决定的程序
- 整理一下最近的RIA行业新闻吧