文件清单:
public Set<Teacher> getTeacs() {
publicvoid setTeacs(Set<Teacher> teacs) {
publicvoid setName(String name) {
publicvoid setClazz(Clazz clazz) {
publicvoid setName(String name) {
public Set<Student> getStus() {
publicvoid setStus(Set<Student> stus) {
publicvoid setName(String name) {
public Set<Student> getStus() {
publicvoid setStus(Set<Student> stus) {
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
package="com.xfimti.hiberante.beans">
<generator class="native"></generator>
<property name="name"></property>
<property name="age"></property>
<many-to-one name="clazz" column="clazz_id"></many-to-one>
<set table="stu_tea" name="teacs">
<key column="student_id"></key>
<many-to-many class="Teacher" column="teacher_id"></many-to-many>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
package="com.xfimti.hiberante.beans">
<generator class="native"></generator>
<property name="name"></property>
<set table="stu_tea" name="stus">
<key column="teacher_id"></key>
<many-to-many class="Student" column="student_id"></many-to-many>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
package="com.xfimti.hiberante.beans">
<generator class="native"></generator>
<property name="name"></property>
<set name="stus" inverse="true">
<one-to-many class="Student"/>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<property name="show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.driver_class">
<property name="hibernate.connection.url">
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
<mapping resource="com/xfimti/hiberante/beans/Student.hbm.xml" />
<mapping resource="com/xfimti/hiberante/beans/Clazz.hbm.xml" />
<mapping resource="com/xfimti/hiberante/beans/Teacher.hbm.xml" />
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
privatestatic SessionFactory sessionFactory = null;
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
System.err.println("Initial SessionFactory creation failed." + e);
publicstatic Session getSession() {
returnsessionFactory.openSession();
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import com.xfimti.hiberante.beans.Clazz;
import com.xfimti.hiberante.beans.Student;
import com.xfimti.hiberante.beans.Teacher;
import com.xfimti.hibernate.util.HibernateUtil;
publicstaticvoid main(String[] args) {
/*List<Student> ss = getAllStudent(3);
System.out.println(ss.size());
Iterator<Student> its = ss.iterator();
System.out.println(s.getName()+","+s.getAge());
/*添加一个学生信息。他的老师是teacher1,所在班级为xxxxxx*/
String teacherName = "teacher1";
addStudent(s, clazzName, teacherName);
publicstatic Clazz getClazz(String name) {
Session s = HibernateUtil.getSession();
Query q = s.createQuery("from Clazz as c where c.name=?");
clazz = (Clazz) q.uniqueResult();
publicstaticvoid addStudent(Student stu,String clazzName) {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
Clazz clazz = getClazz(clazzName);
} catch (HibernateException e) {
publicstaticvoid addClazz(Clazz clazz) {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
} catch (HibernateException e) {
publicstatic List<Student> getAllStudent(int clazz_id) {
List<Student> ss = new ArrayList<Student>();
Session s = HibernateUtil.getSession();
Clazz clazz = (Clazz) s.get(Clazz.class, 3);
for (int i = 0; i < clazz.getStus().size(); i++) {
ss.add((Student)clazz.getStus().toArray()[i]);
publicstaticvoid addTeacher(Teacher t) {
session = HibernateUtil.getSession();
tx = session.beginTransaction();
} catch (HibernateException e) {
publicstaticvoid addStudent(Student s,String clazzName,String teacherName) {
ss = HibernateUtil.getSession();
Query q = (Query) ss.createQuery("from Clazz where name=?");
Clazz clazz = (Clazz) q.uniqueResult();
q = ss.createQuery("from Teacher where name=?");
Teacher t = (Teacher) q.uniqueResult();
/*将clazz和t设置到s,在处理持久化状态自动检测更改并修改。*/
Set<Teacher> st = new HashSet<Teacher>();
} catch (HibernateException e) {