hibernate中多对多映射

package model;

import java.util.Set;

public class Student {
	private String id;
	
	private String name;
	
	private Set<Course> course;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Course> getCourse() {
		return course;
	}

	public void setCourse(Set<Course> course) {
		this.course = course;
	}
}
package model;

import java.util.Set;

public class Course {
	private String id;
	
	private String name;
	
	private Set<Student> students;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}
	
	
}

<?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">
<hibernate-mapping>
	<class name = "model.Student" table = "student">
		<id name="id" type = "string">
			<generator class="uuid"></generator>
		</id>
		<property name="name">
			<column name="name" length="20"></column>
			<type name="string"></type>
		</property>
		<set name="course" table="student_course" cascade="save-update">
			<key column = "student_id"></key>
			<many-to-many class = "model.Course" column="course_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>




<?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">
<hibernate-mapping>
	<class name = "model.Course" table = "course">
		<id name="id" type = "string">
			<generator class="uuid"></generator>
		</id>
		<property name="name">
			<column name="name" length="20"></column>
			<type name="string"></type>
		</property>
		<set name="student" table = "student_course">
			<key column = "course_id"></key>
			<many-to-many class = "model.Student" column = "student_id"></many-to-many>
		</set>
	</class>
</hibernate-mapping>

package model;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
 * 
 * create table course (id varchar(255) not null, name varchar(20), primary key (id))
create table student (id varchar(255) not null, name varchar(20), primary key (id))
create table student_course (student_id varchar(255) not null, course_id varchar(255) not null, primary key (student_id, course_id))
alter table student_course add index FKB0A3729F5B226DF5 (student_id), add constraint FKB0A3729F5B226DF5 foreign key (student_id) references student (id)
alter table student_course add index FKB0A3729FE5E11E5F (course_id), add constraint FKB0A3729FE5E11E5F foreign key (course_id) references course (id)

 * @author Administrator
 *
 */
public class Create {
	public static void main(String[] args) {
		SchemaExport exprot = new SchemaExport(new Configuration().configure());
		exprot.create(true, false);
	}
}
package model;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class Test {
	public static void main(String[] args) {
		//save();
		//fetch();
		//choiceCourse();
		deleteCourse();
	}
	/**
	 * Hibernate: insert into student (name, id) values (?, ?)
	   Hibernate: insert into course (name, id) values (?, ?)
	   Hibernate: insert into student_course (student_id, course_id) values (?, ?)
	 * 
	 */
	public static void save () {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
				Student student = new Student ();
				student.setName("lisi");
				
				Course course = new Course ();
				course.setName("math");
				
				student.setCourse(new HashSet<Course> ());
				student.getCourse().add(course);
				course.setStudent(new HashSet<Student>());
				course.getStudent().add(student);
				session.save(student);
				
			tx.commit();
		}catch(Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
	//某人选那些课
	/**
	 * 
	 * Hibernate: select student0_.id as id0_0_, student0_.name as name0_0_ from student student0_ where student0_.id=?
Hibernate: select course0_.student_id as student1_1_, course0_.course_id as course2_1_, course1_.id as id2_0_, course1_.name as name2_0_ from student_course course0_ left outer join course course1_ on course0_.course_id=course1_.id where course0_.student_id=?

	 */
	public static void fetch() {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
				Student student = (Student)session.get(Student.class, "8a68ab273edea4bd013edea4be5e0001");
				Set<Course> set = student.getCourse();
				for (Iterator<Course> iter = set.iterator();iter.hasNext();) {
					Course course =iter.next();
					System.out.println(course.getName());
				}
			tx.commit();
		}catch(Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
	//选课
	/**
	 * Hibernate: select student0_.id as id0_0_, student0_.name as name0_0_ from student student0_ where student0_.id=?
Hibernate: select course0_.id as id2_0_, course0_.name as name2_0_ from course course0_ where course0_.id=?
Hibernate: select course0_.student_id as student1_1_, course0_.course_id as course2_1_, course1_.id as id2_0_, course1_.name as name2_0_ from student_course course0_ left outer join course course1_ on course0_.course_id=course1_.id where course0_.student_id=?
Hibernate: select student0_.course_id as course2_1_, student0_.student_id as student1_1_, student1_.id as id0_0_, student1_.name as name0_0_ from student_course student0_ left outer join student student1_ on student0_.student_id=student1_.id where student0_.course_id=?
Hibernate: insert into student_course (student_id, course_id) values (?, ?)
	 * 
	 */
	public static void choiceCourse() {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
				Student student = (Student)session.get(Student.class, "8a68ab273edea4bd013edea4be5e0001");
				Course course = (Course)session.get(Course.class, "f5d7f3f03ede9d45013ede9d46b00002");
				student.getCourse().add(course);
				course.getStudent().add(student);
			tx.commit();
		}catch(Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
	//删除选课
	/**
	 * Hibernate: select student0_.id as id0_0_, student0_.name as name0_0_ from student student0_ where student0_.id=?
	   Hibernate: select course0_.id as id2_0_, course0_.name as name2_0_ from course course0_ where course0_.id=?
	   Hibernate: select course0_.student_id as student1_1_, course0_.course_id as course2_1_, course1_.id as id2_0_, course1_.name as name2_0_ from student_course course0_ left outer join course course1_ on course0_.course_id=course1_.id where course0_.student_id=?
       Hibernate: delete from student_course where student_id=?
	 */
	public static void deleteCourse() {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
				Student student = (Student)session.get(Student.class, "8a68ab273edea4bd013edea4be5e0001");
				Course course = (Course)session.get(Course.class, "f5d7f3f03ede9d45013ede9d46b00002");
				student.getCourse().remove(course);
				
				tx.commit();
		}catch(Exception ex) {
			ex.printStackTrace();
			if (null != tx) {
				tx.rollback();
			}
		}finally {
			HibernateUtil.close(session);
		}
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值