接上一篇,还是以学生---课程为例。
一 把多对多拆分成两个多对一,组合主键的方式
================sql脚本==========================
CREATE TABLE student
(
stuId INT PRIMARY KEY AUTO_INCREMENT,
stuName VARCHAR(20) NOT NULL,
gender VARCHAR(2),
age SMALLINT,
address VARCHAR(200) DEFAULT '学生宿舍'
)AUTO_INCREMENT=100
;
INSERT INTO Student(stuName,gender,age,address) VALUES ('王燕','女',18,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('张栋','男',21,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('李波','男',21,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('陈建','男',19,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('王江洪','男',25,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('潘将','男',24,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('张斌','男',22,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('张斌','女',20,DEFAULT);
--
INSERT INTO Student(stuName,gender,age,address) VALUES ('刘大海','男',28,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('刘德华','男',31,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('周润发','男',41,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('张学友','男',37,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('周星驰','男',42,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('刘亦菲','女',24,DEFAULT);
CREATE TABLE Course
(
courseId INT PRIMARY KEY ,
courseName VARCHAR(20) NOT NULL,
hours DECIMAL(3,1)
);
INSERT INTO course VALUES(10,'数据库基础',20.5);
INSERT INTO course VALUES(11,'c#语言入门',21.5);
INSERT INTO course VALUES(12,'div+css布局',15);
INSERT INTO course VALUES(13,'java面向对象编程',30);
INSERT INTO course VALUES(14,'java程序逻辑',33);
INSERT INTO course VALUES(15,'struts框架开发',20.5);
INSERT INTO course VALUES(16,'oracle数据库开发',22);
--组合主键的形式
CREATE TABLE study
(
stuId INT,
courseId INT,
result INT,
PRIMARY KEY (stuId,courseId),
FOREIGN KEY(stuId) REFERENCES student(stuId),
FOREIGN KEY(courseId) REFERENCES course(courseId)
)
;
INSERT INTO study VALUES(100,11,80);
INSERT INTO study VALUES(100,12,83);
INSERT INTO study VALUES(100,13,56);
INSERT INTO study VALUES(100,14,40);
INSERT INTO study VALUES(101,11,80);
INSERT INTO study VALUES(101,13,70);
INSERT INTO study VALUES(102,12,93);
INSERT INTO study VALUES(103,11,85);
INSERT INTO study VALUES(104,11,89);
INSERT INTO study VALUES(105,12,66);
INSERT INTO study VALUES(105,13,77);
INSERT INTO study VALUES(106,11,45);
INSERT INTO study VALUES(106,14,85);
==================实体类Study.java=========================
package com.obtk.test;
/**
* Study entity. @author MyEclipse Persistence Tools
*/
public class Study implements java.io.Serializable {
private static final long serialVersionUID = 8534796144104395514L;
private Integer stuId;
private Integer courseId;
private Integer result;
private Course course; //关联属性
private Student student; //关联属性
public Study() {
}
public Study(Integer stuId,Integer courseId) {
this.courseId=courseId;
this.stuId=stuId;
}
public Integer getResult() {
return this.result;
}
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public Integer getCourseId() {
return courseId;
}
public void setCourseId(Integer courseId) {
this.courseId = courseId;
}
public void setResult(Integer result) {
this.result = result;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
================映射文件Study.hbm.xml===========================
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.obtk.entitys.Study" table="study" catalog="manydb">
<!-- 组合主键的策略 -->
<composite-id>
<key-many-to-one name="course" class="com.obtk.entitys.Course" column="courseId"></key-many-to-one>
<key-many-to-one name="student" class="com.obtk.entitys.Student" column="stuId"></key-many-to-one>
</composite-id>
<property name="result" type="java.lang.Integer">
<column name="result" />
</property>
</class>
</hibernate-mapping>
====================测试类=======================
案例一 数据添加
package com.obtk.test;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.obtk.entitys.Course;
import com.obtk.entitys.Student;
import com.obtk.entitys.Study;
import com.obtk.utils.HibernateUtil;
public class TestAdd {
public static void main(String[] args) {
Session session=null;
Transaction tx=null;
try {
session=HibernateUtil.getSession();
tx=session.beginTransaction();
Student stu=(Student)session.createQuery("from Student where stuName='王燕'").uniqueResult();
Course cor=(Course)session.createQuery("from Course where courseName='数据库基础'").uniqueResult();
Study study=new Study();
study.setCourse(cor);
study.setResult(99);
study.setStudent(stu);
session.save(study);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}
案例二 数据查询
package com.obtk.test;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.obtk.entitys.Study;
import com.obtk.utils.HibernateUtil;
public class TestQuery {
public static void main(String[] args) {
String hql="from Study s where s.course.courseName=?";
Session session=null;
try {
session=HibernateUtil.getSession();
Query qy=session.createQuery(hql);
qy.setParameter(0, "c#语言入门");
List<Study> studyList=qy.list();
Study study=null;
for(int i=0;i<studyList.size();i++){
study=studyList.get(i);
System.out.println(study.getCourse().getCourseName()+","
+study.getStudent().getStuName()+","+study.getResult());
}
} catch (HibernateException e) {
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
}
}
二 把多对多拆分成两个多对一,独立主键的方式
================sql脚本==========================
CREATE TABLE student
(
stuId INT PRIMARY KEY AUTO_INCREMENT,
stuName VARCHAR(20) NOT NULL,
gender VARCHAR(2),
age SMALLINT,
address VARCHAR(200) DEFAULT '学生宿舍'
)AUTO_INCREMENT=100
;
INSERT INTO Student(stuName,gender,age,address) VALUES ('王燕','女',18,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('张栋','男',21,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('李波','男',21,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('陈建','男',19,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('王江洪','男',25,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('潘将','男',24,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('张斌','男',22,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('张斌','女',20,DEFAULT);
--
INSERT INTO Student(stuName,gender,age,address) VALUES ('刘大海','男',28,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('刘德华','男',31,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('周润发','男',41,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('张学友','男',37,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('周星驰','男',42,DEFAULT);
INSERT INTO Student(stuName,gender,age,address) VALUES ('刘亦菲','女',24,DEFAULT);
CREATE TABLE Course
(
courseId INT PRIMARY KEY ,
courseName VARCHAR(20) NOT NULL,
hours DECIMAL(3,1)
);
INSERT INTO course VALUES(10,'数据库基础',20.5);
INSERT INTO course VALUES(11,'c#语言入门',21.5);
INSERT INTO course VALUES(12,'div+css布局',15);
INSERT INTO course VALUES(13,'java面向对象编程',30);
INSERT INTO course VALUES(14,'java程序逻辑',33);
INSERT INTO course VALUES(15,'struts框架开发',20.5);
INSERT INTO course VALUES(16,'oracle数据库开发',22);
--独立主键的形式
CREATE TABLE study
(
resultId INT PRIMARY KEY AUTO_INCREMENT,
stuId INT,
courseId INT,
result INT,
FOREIGN KEY(stuId) REFERENCES student(stuId),
FOREIGN KEY(courseId) REFERENCES course(courseId)
)
;
INSERT INTO study(stuId,courseId,result) VALUES(100,11,80);
INSERT INTO study(stuId,courseId,result) VALUES(100,12,83);
INSERT INTO study(stuId,courseId,result) VALUES(100,13,56);
INSERT INTO study(stuId,courseId,result) VALUES(100,14,40);
INSERT INTO study(stuId,courseId,result) VALUES(101,11,80);
INSERT INTO study(stuId,courseId,result) VALUES(101,13,70);
INSERT INTO study(stuId,courseId,result) VALUES(102,12,93);
INSERT INTO study(stuId,courseId,result) VALUES(103,11,85);
INSERT INTO study(stuId,courseId,result) VALUES(104,11,89);
INSERT INTO study(stuId,courseId,result) VALUES(105,12,66);
INSERT INTO study(stuId,courseId,result) VALUES(105,13,77);
INSERT INTO study(stuId,courseId,result) VALUES(106,11,45);
INSERT INTO study(stuId,courseId,result) VALUES(106,14,85);
========================实体类Study.java======================
package com.obtk.entitys;
/**
* Study entity. @author MyEclipse Persistence Tools
*/
public class Study implements java.io.Serializable {
private static final long serialVersionUID = 8534796144104395514L;
private Integer resultId;
private Integer result;
private Course course; //关联属性
private Student student; //关联属性
public Study() {
}
public void setResult(Integer result) {
this.result = result;
}
public Integer getResult() {
return result;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public void setResultId(Integer resultId) {
this.resultId = resultId;
}
public Integer getResultId() {
return resultId;
}
}
====================映射文件study.hbm.xml=====================
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.obtk.entitys.Study" table="study" catalog="manydb">
<id name="resultId" type="integer">
<column name="resultId"></column>
<generator class="increment"></generator>
</id>
<property name="result" type="java.lang.Integer">
<column name="result" />
</property>
<!-- 独立主键的策略 -->
<many-to-one name="course" class="com.obtk.entitys.Course" column="courseId"></many-to-one>
<many-to-one name="student" class="com.obtk.entitys.Student" column="stuId"></many-to-one>
</class>
</hibernate-mapping>
案例:数据查询
package com.obtk.test;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.obtk.entitys.Study;
import com.obtk.utils.HibernateUtil;
public class TestQuery2 {
public static void main(String[] args) {
Session session=null;
try {
session=HibernateUtil.getSession();
Study study=(Study)session.load(Study.class, 10);
System.out.println(study.getCourse().getCourseName()+","+study.getStudent().getStuName()
+","+study.getResult());
} catch (HibernateException e) {
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
}
}