Hibernate 映射问题,要多少分都可以
本来有还有很多类的,现在就附上5类,希望热心人能解答
User,Student 和Course 我就想知道这3个表怎么映射的,希望大虾指点 展开
User类:
package com.test.model;
import java.sql.Date;
public class User {
protected String id;
protected String username;
protected String password;
protected String realName;
protected Date birth;
protected Date regTime;
setter&&getter
}
Student类:
package com.test.model;
import java.util.HashSet;
import java.util.Set;
public class Student extends User {
private String studentNo;
private String major;
private Set<Course> courses = new HashSet<Course>();
setter&&getter
}
Teacher类:
package com.test.model;
public class Teacher extends User {
private String teacherNo;
private String specialty;
private Course course;
setter&&getter
}
Course类:
package com.test.model;
import java.util.HashSet;
import java.util.Set;
public class Course {
private String id;
private String description;
private String courseName;
private Set<Student> students = new HashSet<Student>();
private Teacher teacher;
setter&&getter
}
User.hbm.xml映射文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.model.User" table="user">
<id name="id">
<generator class="uuid"/>
</id>
<property name="username" length="16" not-null="true"></property>
<property name="password" length="16" not-null="true"></property>
<property name="realName" length="16" not-null="true"></property>
<property name="birth" not-null="true"></property>
<property name="regTime" not-null="true"></property>
<joined-subclass name="com.test.model.Teacher" table="teacher">
<key column="teacherNo"></key>
<property name="specialty" length="20"></property>
<one-to-one name="course"></one-to-one>
</joined-subclass>
<joined-subclass name="com.test.model.Student" table="student">
<key column="studentNo"></key>
<property name="major" length="20"></property>
<set name="courses" table="student_course">
<key column="studentNo"></key>
<many-to-many class="com.test.model.Course" column="courseId"></many-to-many>
</set>
</joined-subclass>
<joined-subclass name="com.test.model.Administrator" table="administrator">
<key column="id"></key>
</joined-subclass>
</class>
</hibernate-mapping>
Course.hbm.xml映射文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.model.Course" table="course">
<id name="id">
<generator class="foreign">
<param name="property">teacher</param>
</generator >
</id>
<property name="description" length="100" not-null="true"></property>
<property name="courseName" length="16" not-null="true"></property>
<set name="students" table="student_course">
<key column="courseId"></key>
<many-to-many class="com.test.model.Student" column="studentNo"></many-to-many>
</set>
<one-to-one name="teacher" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
Student---Course: many-to-many
Teacher--Course:one-to-one主键关联
2023-07-25 广告
2,你图中,Teacher和Course是1:1的关系。Teacher类再加一个int id、Course类型的属性course,Course类中再加一个Teacher类型的属性teacher;(个人认为Course为主表,Teacher为从表比较好)
3,Teacher.hbm.xml文件:<hibernate-mapping package="test.hibernate.domain">
<class name="Teacher">
<id name="id" column="id">
<generator class="foreign">
<param name="property">course</param>
</generator>
</id>
<property name="TeacherNo" column="TeacherNo"/>
<one-to-one name="course" />
</class>
4,course.hbm.xml文件:<hibernate-mapping package="test.hibernate.domain">
<class name="Course">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="CourseName"/>
<property name="Description"/>
<one-to-one name="teacher"/>
</class>
</hibernate-mapping>
其他的类似。