标签:
CREATE TABLE tb_student
(
id bigint NOT NULL auto_increment COMMENT ‘ID‘,
no varchar(10) NOT NULL COMMENT ‘学号‘,
name varchar(50) NOT NULL COMMENT ‘姓名‘,
sex char(1) NOT NULL COMMENT ‘性别‘,
birthday datetime COMMENT ‘出生日期‘,
class_id bigint COMMENT ‘班级ID‘,
PRIMARY KEY (id)
) COMMENT = ‘学生信息表‘;
CREATE TABLE tb_teacher
(
id bigint NOT NULL auto_increment COMMENT ‘ID‘,
no varchar(10) NOT NULL COMMENT ‘教师编号‘,
name varchar(50) NOT NULL COMMENT ‘教师姓名‘,
sex char(1) NOT NULL COMMENT ‘教师性别‘,
job_title varchar(50) NOT NULL COMMENT ‘职称‘,
PRIMARY KEY (id)
) COMMENT = ‘教师信息表‘;
CREATE TABLE tb_student_teacher
(
student_id bigint NOT NULL COMMENT ‘学生ID‘,
teacher_id bigint NOT NULL COMMENT ‘教师ID‘,
PRIMARY KEY (student_id,teacher_id)
) COMMENT = ‘学生教师关系表‘;
-- 可选的外键约束
ALTER TABLE tb_student_teacher ADD CONSTRAINT fk_student_id FOREIGN KEY (student_id) REFERENCES tb_student (id);
ALTER TABLE tb_student_teacher ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES tb_teacher (id);
package model;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Student
{
private Long id;
private String no;
private String name;
private String sex;
private Date birthday;
private Long classId;
private Set<Teacher> teachers=new HashSet<Teacher>();
@Override
public String toString()
{
return "Student [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", classId=" + classId + ", teachers=" + teachers.size() + "]";
}
//省略setter、getter...
}
package model;
import java.util.HashSet;
import java.util.Set;
public class Teacher
{
private Long id;
private String no;
private String name;
private String sex;
private String job_title;
private Set<Student> students=new HashSet<Student>();
@Override
public String toString()
{
return "Teacher [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", job_title=" + job_title + ", students=" + students.size() + "]";
}
//省略setter、getter...
}
<hibernate-mapping package="model">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="no" column="no"/>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
<property name="classId" column="class_id"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Teacher" table="tb_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="no" column="no" />
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="job_title" column="job_title" />
<set name="students" table="tb_student_teacher" cascade="save-update">
<key column="teacher_id"/>
<many-to-many class="model.Student" column="student_id" />
</set>
</class>
</hibernate-mapping>
public static void main(String[] args)
{
Teacher teacher;
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
System.out.println("连接数据库");
Session session = sf.openSession();
teacher =(Teacher) session.get(Teacher.class, new Long(1));
System.out.println(teacher);
System.out.println(teacher.getStudents());
session.close();
System.exit(0);
}
连接数据库
Hibernate:
select
teacher0_.id as id1_7_0_,
teacher0_.no as no2_7_0_,
teacher0_.name as name3_7_0_,
teacher0_.sex as sex4_7_0_,
teacher0_.job_title as job_titl5_7_0_
from
tb_teacher teacher0_
where
teacher0_.id=?
Hibernate:
select
students0_.teacher_id as teacher_1_7_0_,
students0_.student_id as student_2_6_0_,
student1_.id as id1_5_1_,
student1_.no as no2_5_1_,
student1_.name as name3_5_1_,
student1_.sex as sex4_5_1_,
student1_.birthday as birthday5_5_1_,
student1_.class_id as class_id6_5_1_
from
tb_student_teacher students0_
inner join
tb_student student1_
on students0_.student_id=student1_.id
where
students0_.teacher_id=?
Teacher [id=1, no=000001, name=教师1, sex=女, job_title=初级教师, students=1]
Hibernate:
select
teachers0_.student_id as student_2_5_0_,
teachers0_.teacher_id as teacher_1_6_0_,
teacher1_.id as id1_7_1_,
teacher1_.no as no2_7_1_,
teacher1_.name as name3_7_1_,
teacher1_.sex as sex4_7_1_,
teacher1_.job_title as job_titl5_7_1_
from
tb_student_teacher teachers0_
inner join
tb_teacher teacher1_
on teachers0_.teacher_id=teacher1_.id
where
teachers0_.student_id=?
[Student [id=1, no=000001, name=学生1, sex=男, birthday=2015-01-27, classId=41, teachers=1]]
关闭数据库
<hibernate-mapping package="model">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="no" column="no"/>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
<property name="classId" column="class_id"/>
<set name="teachers" table="tb_student_teacher" cascade="save-update" inverse="true">
<key column="student_id"/>
<many-to-many class="model.Teacher" column="teacher_id"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Teacher" table="tb_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="no" column="no" />
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="job_title" column="job_title" />
<set name="students" table="tb_student_teacher" cascade="save-update" inverse="false">
<key column="teacher_id"/>
<many-to-many class="model.Student" column="student_id" />
</set>
</class>
</hibernate-mapping>
package model;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Student
{
private Long id;
private String no;
private String name;
private String sex;
private Date birthday;
private Long classId;
private Set<TeacherAndStudent> teacherAndStudents = new HashSet<TeacherAndStudent>();
@Override
public String toString()
{
return "Student [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", classId=" + classId + "]";
}
// 省略setter、getter...
}
package model;
import java.util.HashSet;
import java.util.Set;
public class Teacher
{
private Long id;
private String no;
private String name;
private String sex;
private String job_title;
private Set<TeacherAndStudent> teacherAndStudents = new HashSet<TeacherAndStudent>();
@Override
public String toString()
{
return "Teacher [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", job_title=" + job_title + "]";
}
// 省略setter、getter...
}
package model;
public class TeacherAndStudent
{
private Teacher teacher;
private Student student;
@Override
public String toString()
{
return "TeacherAndStudent [teacher=" + teacher + ", student=" + student + "]";
}
// 省略setter、getter...
}
<hibernate-mapping package="model">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="no" column="no"/>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
<property name="classId" column="class_id"/>
<set name="teacherAndStudents" table="tb_student_teacher" cascade="save-update" inverse="true">
<key column="student_id" />
<composite-element class="TeacherAndStudent">
<parent name="student" />
<many-to-one name="teacher" class="Teacher" column="teacher_id"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Teacher" table="tb_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="no" column="no" />
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="job_title" column="job_title" />
<set name="teacherAndStudents" table="tb_student_teacher" cascade="save-update" inverse="false">
<key column="teacher_id"/>
<composite-element class="TeacherAndStudent">
<parent name="teacher"/>
<many-to-one name="student" class="Student" column="student_id"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
public static void main(String[] args)
{
Teacher teacher;
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
System.out.println("连接数据库");
Session session = sf.openSession();
teacher =(Teacher) session.get(Teacher.class, new Long(1));
System.out.println(teacher);
System.out.println(teacher.getTeacherAndStudents());
session.close();
System.exit(0);
}
连接数据库
Hibernate:
select
teacher0_.id as id1_7_0_,
teacher0_.no as no2_7_0_,
teacher0_.name as name3_7_0_,
teacher0_.sex as sex4_7_0_,
teacher0_.job_title as job_titl5_7_0_
from
tb_teacher teacher0_
where
teacher0_.id=?
Teacher [id=1, no=000001, name=教师1, sex=女, job_title=初级教师]
Hibernate:
select
teacherand0_.teacher_id as teacher_1_7_0_,
teacherand0_.student_id as student_2_6_0_,
student1_.id as id1_5_1_,
student1_.no as no2_5_1_,
student1_.name as name3_5_1_,
student1_.sex as sex4_5_1_,
student1_.birthday as birthday5_5_1_,
student1_.class_id as class_id6_5_1_
from
tb_student_teacher teacherand0_
left outer join
tb_student student1_
on teacherand0_.student_id=student1_.id
where
teacherand0_.teacher_id=?
[TeacherAndStudent [teacher=Teacher [id=1, no=000001, name=教师1, sex=女, job_title=初级教师], student=Student [id=1, no=000001, name=学生1, sex=男, birthday=2015-01-27, classId=41]]]
Hibernate:
delete
from
tb_student_teacher
where
teacher_id=?
Hibernate:
insert
into
tb_student_teacher
(teacher_id, student_id)
values
(?, ?)
关闭数据库
CREATE TABLE tb_teacher_student
(
id bigint NOT NULL auto_increment COMMENT ‘ID‘,
student_id bigint COMMENT ‘学生ID‘,
teacher_id bigint COMMENT ‘教师ID‘,
PRIMARY KEY (id)
) COMMENT = ‘教师学生关系表‘;
package model;
public class StudentTeacher
{
private Long id;
private Teacher teacher;
private Student student;
@Override
public String toString()
{
return "StudentTeacher [id=" + id + ", teacher=" + teacher + ", student=" + student + "]";
}
// 省略setter、getter...
}
package model;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
public class Student
{
private Long id;
private String no;
private String name;
private String sex;
private Date birthday;
private Long classId;
private Set<StudentTeacher> studentTeachers = new HashSet<StudentTeacher>();
@Override
public String toString()
{
return "Student [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", classId=" + classId + "]";
}
// 省略setter、getter...
}
package model;
import java.util.HashSet;
import java.util.Set;
public class Teacher
{
private Long id;
private String no;
private String name;
private String sex;
private String job_title;
private Set<StudentTeacher> studentTeachers = new HashSet<StudentTeacher>();
@Override
public String toString()
{
return "Teacher [id=" + id + ", no=" + no + ", name=" + name + ", sex=" + sex + ", job_title=" + job_title + "]";
}
// 省略setter、getter...
}
<hibernate-mapping package="model">
<class name="StudentTeacher" table="tb_teacher_student">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<many-to-one name="teacher" column="teacher_id" class="Teacher"/>
<many-to-one name="student" column="student_id" class="Student"/>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Teacher" table="tb_teacher">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="no" column="no" />
<property name="name" column="name" />
<property name="sex" column="sex" />
<property name="job_title" column="job_title" />
<set name="studentTeachers" inverse="true" cascade="save-update">
<key column="teacher_id" />
<one-to-many class="StudentTeacher"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="model">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="no" column="no"/>
<property name="name" column="name"/>
<property name="sex" column="sex"/>
<property name="birthday" column="birthday"/>
<property name="classId" column="class_id"/>
<set name="studentTeachers" inverse="true" cascade="save-update">
<key column="student_id" />
<one-to-many class="StudentTeacher"/>
</set>
</class>
</hibernate-mapping>
......省略打开session的代码
try
{
teacher =(Teacher) session.get(Teacher.class, new Long(1));
student = (Student) session.get(Student.class, new Long(1));
StudentTeacher st=new StudentTeacher();
st.setStudent(student);
st.setTeacher(teacher);
student.getStudentTeachers().add(st);
session.update(student);
transaction.commit();
}
......省略关闭session的代码
连接数据库
Hibernate:
........ 省略若干获取关联对象的查询语句Hibernate: insert into tb_teacher_student (teacher_id, student_id) values (?, ?)
关闭数据库
<set name="EntityClassName"
access="field|property|ClassName"
collection-type="collection-type"
schema="schema"
catalog="catalog"
check="arbitrary sql check condition"
table="TableName"
subselect="SQL expression"
where="arbitrary sql where condition"
sort="unsorted"
optimistic-lock="false|true"
inverse="false|true"
fetch="join|select"
batch-size="5"
cascade="all|none|save-update|delete"
lazy="false|true"
mutable="false|true"
outer-join="false|true"
order-by="arbitrary sql order by condition"
embed-xml="false|true"
persister="PersisterClass"
node="element-name"/>
<composite-element class="ClassName
" node="element-name"><parent name="PropertyName
" /><property name="PropertyName
"></property><many-to-one />
</composite-element>
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4280172.html