一对一单向外键关联
(学生卡表里有“studentId”字段)
public class Student {
private int id;
private String name;
}
public class StuIdCard {
private int id;
private String num;
@OneToOne
@JoinColumn(name="studentId")
private Student student;
}
<hibernate-mapping>
<class name="com.Student" dynamic-update="true">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.StuIdCard">
<id name="id">
<generator class="native"/>
</id>
<property name="num"/>
<many-to-one name="student" column="studentId" unique="true"></many-to-one>
</class>
</hibernate-mapping>
一对一双外键关联
(学生卡表里有“studentId”字段)
<hibernate-mapping>
<class name="com.Student" dynamic-update="true">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<one-to-one name="stuIdCard" property-ref="student"></one-to-one>
</class>
</hibernate-mapping>
学生类添加
@OneToOne(mappedBy="student")
private StuIdCard stuIdCard;
多对一关联
public class User {
private int id;
private String name;
@ManyToOne
private Group group;
}
public class Group {
private int id;
private String name;
}
<hibernate-mapping>
<class name="com.Group" table="t_group">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.User" table="t_user">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="group" column="groupId" />
</class>
</hibernate-mapping>
一对多关联
public class Group {
private int id;
private String name;
@OneToMany
@JoinColumn(name="groupId")
private Set<User> users = new HashSet<User>();
}
public class User {
private int id;
private String name;
}
<hibernate-mapping>
<class name="com.Group" table="t_group">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"/>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.User"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
一对多多对一关联
public class Group {
private int id;
private String name;
@OneToMany(mappedBy="group")
private Set<User> users = new HashSet<User>();
}
public class User {
private int id;
private String name;
@ManyToOne
private Group group;
}
<hibernate-mapping>
<class name="com.Group" table="t_group">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="users">
<key column="groupId"></key>
<one-to-many class="com.User"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<many-to-one name="group" column="groupId"></many-to-one>
</class>
</hibernate-mapping>
多对多单向
public class Student {
private int id;
private String name;
}
public class Teacher {
private int id;
private String name;
@ManyToMany
@JoinTable(name="t_s",
joinColumns={@JoinColumn(name="teacher_id")},
inverseJoinColumns={@JoinColumn(name="student_id")}
)
private Set<Student> students = new HashSet<Student>();
}
<hibernate-mapping>
<class name="com.Student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.Teacher">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="students" table="t_s">
<key column="teacher_id"></key>
<many-to-many class="com.Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>
多对多两向关联
public class Student {
private int id;
private String name;
@ManyToMany(mappedBy="students")
private Set<Teacher> teachers = new HashSet<Teacher>();
}
public class Teacher {
private int id;
private String name;
@ManyToMany
@JoinTable(name="t_s",
joinColumns={@JoinColumn(name="teacher_id")},
inverseJoinColumns={@JoinColumn(name="student_id")}
)
private Set<Student> students = new HashSet<Student>();
}
<hibernate-mapping>
<class name="com.Student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="teachers" table="t_s">
<key column="student_id"></key>
<many-to-many class="comTeacher" column="teacher_id"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.Teacher">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="students" table="t_s">
<key column="teacher_id"></key>
<many-to-many class="com.Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>