`
RyanPoy
  • 浏览: 51225 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

hibernate入门使用系列 4-- xml关系映射篇(下)

阅读更多

接上篇 hibernate入门使用系列 3-- xml关系映射篇(中)



开我写的前3篇中,分别讲了one-to-one, one--to-many, many-to-one 。

这篇,主要讲的是 n:n 的关系。即:many-to-many。

我们以老师和学生为例,一个老师可以交很多学生,同样一个学生可以拥有多个老师,所以,他们之间的关系就是n:n的。

实体模型:



从实体模型来看。有2个对象,但是为了在数据库中表示出2者的n:n的关系,我们还得引入一张表。所以,sql脚本如下:

 use HibernateQuickUse;
drop table if exists teacher_student_relation;
drop table if exists Teacher;
drop table if exists Student;

create table Teacher (
	tid varchar(32) primary key,
	name varchar(32) not null
);

create table Student (
	sid varchar(32) primary key,
	name varchar(128) not null
);

create table teacher_student_relation (
	id integer auto_increment primary key,
	teacher_id varchar(32) not null,
	student_id varchar(32) not null,
	foreign key(teacher_id) references Teacher(tid),
	foreign key(student_id) references Student(sid)
);



通过模型,创建java类如下:

Student.java

package org.py.hib.relation.many2many;

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

/**
 * Student entity.
 */

@SuppressWarnings("serial")
public class Student implements java.io.Serializable
{
	private String id;

	private String name;

	private Set<Teacher> teachers = new HashSet<Teacher>(0);

	public Student()
	{
	}

	public String getId()
	{
		return this.id;
	}

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

	public String getName()
	{
		return this.name;
	}

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

	public Set<Teacher> getTeachers()
	{
		return teachers;
	}

	public void setTeachers(Set<Teacher> teachers)
	{
		this.teachers = teachers;
	}
}

 

Teacher.java:

package org.py.hib.relation.many2many;

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

/**
 * Teacher entity.
 */

@SuppressWarnings("serial")
public class Teacher implements java.io.Serializable
{
	private String id;

	private String name;

	private Set<Student> students = new HashSet<Student>(0);

	public Teacher()
	{
	}

	public String getId()
	{
		return this.id;
	}

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

	public String getName()
	{
		return this.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映射文件如下

Student.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">

<hibernate-mapping>
	<class name="org.py.hib.relation.many2many.Student"
		table="student">
		<id name="id" type="java.lang.String" column="sid" length="32">
			<generator class="uuid" />
		</id>

		<property name="name" type="java.lang.String" column="name"
			length="128" not-null="true" />

		<set name="teachers" table="teacher_student_relation" cascade="save-update" inverse="false">
			<key column="student_id" not-null="true" />

			<many-to-many column="teacher_id"
				class="org.py.hib.relation.many2many.Teacher" 
				/>
		</set>
	</class>
</hibernate-mapping>

 注意:

 set中的 table 指向的是数据库中的关联表。

cascade 用的是save-update , 且inverse用的是false,这样的话,当进行修改和保存和删除时,关联表中的记录也会删掉.

如果cascade 用的是 all 那么连同student表中的记录也会被删除掉。

key中的column指的是: 关联表中与Student发生关系的字段。

而many-to-many中的column指的是:关联表中,与class(这里是:org.py.hib.relation.many2many.Teacher)发生关系的字段。

关于inverse,请参考上篇:hibernate入门使用系列 3-- xml关系映射篇(中)

 

 

Teacher.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">

<hibernate-mapping>
	<class name="org.py.hib.relation.many2many.Teacher"
		table="teacher">
		<id name="id" type="java.lang.String" column="tid"
			length="32">
			<generator class="uuid" />
		</id>

		<property name="name" type="java.lang.String" column="name"
			length="32" not-null="true" />

		<set name="students" table="teacher_student_relation" cascade="save-update"
			inverse="false">
			<key column="teacher_id" not-null="true" />
			<many-to-many class="org.py.hib.relation.many2many.Student"
				column="student_id" />
		</set>
	</class>
</hibernate-mapping>

  注意:

这里的inverse也采用了false,这样子的话,Teacher和Student都维护关系表中的关系。

 

 

测试类,Many2ManyTest.java

package org.py.hib.relation.many2many;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;

public class Many2ManyTest extends TestCase
{
	private SessionFactory factory;

	@Before
	public void setUp() throws Exception
	{
		Configuration conf = new Configuration().configure();
		factory = conf.buildSessionFactory();
	}

	/**
	 * 测试添加
	 * @throws Exception
	 */
	public void testSave() throws Exception
	{
		System.out.println("\n=== test save ===");

		Teacher teacher1 = new Teacher();
		teacher1.setName("teacher_1");

		Teacher teacher2 = new Teacher();
		teacher2.setName("teacher_2");

		Student stu1 = new Student();
		stu1.setName("student_1");

		Student stu2 = new Student();
		stu2.setName("student_2");

		stu1.getTeachers().add(teacher1);
		stu1.getTeachers().add(teacher2);

		stu2.getTeachers().add(teacher2);
		teacher1.getStudents().add(stu2);

		Session session = null;
		Transaction tran = null;
		try
		{
			session = factory.openSession();
			tran = session.beginTransaction();

			session.save(stu1);
			session.save(stu2);
			tran.commit();

			Assert.assertNotNull(teacher1.getId());
			Assert.assertNotNull(teacher2.getId());

			Assert.assertNotNull(stu1.getId());
			Assert.assertNotNull(stu2.getId());

		} catch (Exception ex)
		{
			tran.rollback();
			throw ex;
		} finally
		{
			if (session != null)
			{
				try
				{
					session.close();
				} catch (Exception ex)
				{
					// nothing to do
				} finally
				{
					if (session != null)
						session = null;
				}
			}
		}
	}

	/**
	 * 测试从Teacher查询
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void testFindFromTeacher() throws Exception
	{
		System.out.println("\n=== test find from Teacher ===");
		Session session = null;
		try
		{
			session = factory.openSession();
			Iterator<Teacher> iter = session.createQuery("from Teacher").iterate();
			while (iter.hasNext())
			{
				Teacher teacher = iter.next();
				Assert.assertNotNull(teacher.getId());
				String teacherName = teacher.getName();
				if ("teacher_1".equals(teacherName))
				{
					Set<Student> stus = teacher.getStudents();
					Assert.assertEquals(stus.size(), 2);
					for (Student stu : stus)
					{
						String stuName = stu.getName();
						Assert.assertNotNull(stu.getId());
						Assert.assertTrue(stuName.equals("student_1") || stuName.equals("student_2"));
					}
				} else if ("teacher_2".equals(teacherName))
				{
					Set<Student> stus = teacher.getStudents();
					Assert.assertEquals(stus.size(), 2);

					for (Student stu : stus)
					{
						String stuName = stu.getName();
						Assert.assertNotNull(stu.getId());
						Assert.assertTrue(stuName.equals("student_1") || stuName.equals("student_2"));
					}
				} else
				{
					throw new Exception("teacher name error exception.");
				}
			}
		} catch (Exception ex)
		{
			throw ex;
		} finally
		{
			if (session != null)
			{
				try
				{
					session.close();
				} catch (Exception ex)
				{
					// nothing to do
				} finally
				{
					if (session != null)
						session = null;
				}
			}
		}
	}

	/**
	 * 测试从Student查询
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void testFindFromStudent() throws Exception
	{
		System.out.println("\n=== test find from Student ===");
		Session session = null;
		try
		{
			session = factory.openSession();
			Iterator<Student> iter = session.createQuery("from Student").iterate();
			while (iter.hasNext())
			{
				Student stu = iter.next();
				Assert.assertNotNull(stu.getId());
				String stuName = stu.getName();
				if ("student_1".equals(stuName))
				{
					Set<Teacher> teachers = stu.getTeachers();
					Assert.assertEquals(teachers.size(), 2);
					for (Teacher teacher : teachers)
					{
						String tName = teacher.getName();
						Assert.assertNotNull(teacher.getId());
						Assert.assertTrue(tName.equals("teacher_1") || tName.equals("teacher_2"));
					}
				} else if ("student_2".equals(stuName))
				{
					Set<Teacher> teachers = stu.getTeachers();
					Assert.assertEquals(teachers.size(), 2);
					for (Teacher teacher : teachers)
					{
						String tName = teacher.getName();
						Assert.assertNotNull(teacher.getId());
						Assert.assertTrue(tName.equals("teacher_1") || tName.equals("teacher_2"));
					}
				} else
				{
					throw new Exception("student name error exception.");
				}
			}
		} catch (Exception ex)
		{
			throw ex;
		} finally
		{
			if (session != null)
			{
				try
				{
					session.close();
				} catch (Exception ex)
				{
					// nothing to do
				} finally
				{
					if (session != null)
						session = null;
				}
			}
		}
	}

	/**
	 * 测试修改
	 * @throws Exception
	 */
	public void testModify() throws Exception
	{
		System.out.println("\n=== test modify ===");
		Session session = null;
		Transaction tran = null;
		try
		{
			session = factory.openSession();
			tran = session.beginTransaction();

			Teacher t1 = (Teacher) session.createQuery("from Teacher t where t.name='teacher_1'").list().get(0);
			t1.setName("new_teacher_1"); // 修改用户名 = m_name2.(原来用户名= m_name)

			Set<Student> stus = t1.getStudents();
			for (Student stu : stus)
			{
				if (stu.getName().equals("student_1"))
				{
					stus.remove(stu);
					break;
				}
			}

			tran.commit();

		} catch (Exception ex)
		{
			throw ex;
		} finally
		{
			if (session != null)
			{
				try
				{
					session.close();
				} catch (Exception ex)
				{
					// nothing to do
				} finally
				{
					if (session != null)
						session = null;
				}
			}
		}

		/*
		 * 修改后再查询
		 */
		System.out.println("\n=== test find from Teacher after modify===");
		try
		{
			session = factory.openSession();
			Iterator<Teacher> iter = session.createQuery("from Teacher").iterate();
			while (iter.hasNext())
			{
				Teacher teacher = iter.next();
				Assert.assertNotNull(teacher.getId());
				String teacherName = teacher.getName();
				if ("new_teacher_1".equals(teacherName))
				{
					Set<Student> stus = teacher.getStudents();
					Assert.assertEquals(stus.size(), 1);
					for (Student stu : stus)
					{
						String stuName = stu.getName();
						Assert.assertNotNull(stu.getId());
						Assert.assertTrue(stuName.equals("student_2"));
					}
				} else if ("teacher_2".equals(teacherName))
				{
					Set<Student> stus = teacher.getStudents();
					Assert.assertEquals(stus.size(), 2);

					for (Student stu : stus)
					{
						String stuName = stu.getName();
						Assert.assertNotNull(stu.getId());
						Assert.assertTrue(stuName.equals("student_1") || stuName.equals("student_2"));
					}
				} else
				{
					throw new Exception("teacher name error exception.");
				}
			}
		} catch (Exception ex)
		{
			throw ex;
		} finally
		{
			if (session != null)
			{
				try
				{
					session.close();
				} catch (Exception ex)
				{
					// nothing to do
				} finally
				{
					if (session != null)
						session = null;
				}
			}
		}
	}

	/**
	 * 测试删除
	 * @throws Exception
	 */
	public void testDelete() throws Exception
	{
		System.out.println("\n=== test delete ===");
		Session session = null;
		Transaction tran = null;
		try
		{
			session = factory.openSession();
			tran = session.beginTransaction();

			Iterator<Teacher> iter = session.createQuery("from Teacher").iterate();
			while (iter.hasNext())
				session.delete(iter.next());

			tran.commit();

			Integer count = (Integer) session.createQuery("select count(*) from Teacher").list().get(0);
			Assert.assertEquals(0, count.intValue());

		} catch (Exception ex)
		{
			throw ex;
		} finally
		{
			if (session != null)
			{
				try
				{
					session.close();
				} catch (Exception ex)
				{
					// nothing to do
				} finally
				{
					if (session != null)
						session = null;
				}
			}
		}

		/*
		 * 删除后再查询
		 */
		System.out.println("\n=== test find after delete ===");
		try
		{
			session = factory.openSession();

			Integer num = (Integer) session.createQuery("from Teacher").list().size();
			Assert.assertEquals(0, num.intValue());

			num = (Integer) session.createQuery("from Student").list().size();
			Assert.assertEquals(0, num.intValue());

		} catch (Exception ex)
		{
			throw ex;
		} finally
		{
			if (session != null)
			{
				try
				{
					session.close();
				} catch (Exception ex)
				{
					// nothing to do
				} finally
				{
					if (session != null)
						session = null;
				}
			}
		}
	}

	/**
	 * 
	 */
	@After
	public void tearDown() throws Exception
	{
		factory.close();
	}

}

 

从这个例子中可以看出,many-to-many中,需要引入第3张表来表示关系。

附件中有源代码。

 

 

  • 大小: 3.4 KB
分享到:
评论
8 楼 winyou365 2008-05-11  
谢谢搂主这么用心,看后真是受益匪浅阿!
7 楼 tiandp007 2008-05-11  
<div class='quote_title'>RyanPoy 写道</div>
<div class='quote_div'>
<p>接上篇 <a href='190597' target='_blank'>hibernate入门使用系列 3-- 关系映射篇(中)</a> <br/><br/><br/><br/>开我写的前3篇中,分别讲了one-to-one, one--to-many, many-to-one 。<br/><br/>这篇,主要讲的是 n:n 的关系。即:many-to-many。<br/><br/>我们以老师和学生为例,一个老师可以交很多学生,同样一个学生可以拥有多个老师,所以,他们之间的关系就是n:n的。<br/><br/>实体模型:<br/><br/><br/><img src='../../../topics/download/7cd30464-261e-377c-8b88-5409eda5740f' alt=''/> <br/>从实体模型来看。有2个对象,但是为了在数据库中表示出2者的n:n的关系,我们还得引入一张表。所以,sql脚本如下:</p>
<pre name='code' class='sql'> use HibernateQuickUse;
drop table if exists teacher_student_relation;
drop table if exists Teacher;
drop table if exists Student;

create table Teacher (
tid varchar(32) primary key,
name varchar(32) not null
);

create table Student (
sid varchar(32) primary key,
name varchar(128) not null
);

create table teacher_student_relation (
id integer auto_increment primary key,
teacher_id varchar(32) not null,
student_id varchar(32) not null,
foreign key(teacher_id) references Teacher(tid),
foreign key(student_id) references Student(sid)
);
</pre>
<p><br/><br/>通过模型,创建java类如下:</p>
<p>Student.java</p>
<pre name='code' class='java'>package org.py.hib.relation.many2many;

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

/**
* Student entity.
*/

@SuppressWarnings("serial")
public class Student implements java.io.Serializable
{
private String id;

private String name;

private Set&lt;Teacher&gt; teachers = new HashSet&lt;Teacher&gt;(0);

public Student()
{
}

public String getId()
{
return this.id;
}

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

public String getName()
{
return this.name;
}

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

public Set&lt;Teacher&gt; getTeachers()
{
return teachers;
}

public void setTeachers(Set&lt;Teacher&gt; teachers)
{
this.teachers = teachers;
}
}</pre>
<p> </p>
<p>Teacher.java:</p>
<pre name='code' class='java'>package org.py.hib.relation.many2many;

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

/**
* Teacher entity.
*/

@SuppressWarnings("serial")
public class Teacher implements java.io.Serializable
{
private String id;

private String name;

private Set&lt;Student&gt; students = new HashSet&lt;Student&gt;(0);

public Teacher()
{
}

public String getId()
{
return this.id;
}

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

public String getName()
{
return this.name;
}

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

public Set&lt;Student&gt; getStudents()
{
return students;
}

public void setStudents(Set&lt;Student&gt; students)
{
this.students = students;
}

}</pre>
 
<p>xml映射文件如下</p>
<p>Student.hbm.xml</p>
<pre name='code' class='xml'>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

&lt;hibernate-mapping&gt;
&lt;class name="org.py.hib.relation.many2many.Student"
table="student"&gt;
&lt;id name="id" type="java.lang.String" column="sid" length="32"&gt;
&lt;generator class="uuid" /&gt;
&lt;/id&gt;

&lt;property name="name" type="java.lang.String" column="name"
length="128" not-null="true" /&gt;

&lt;set name="teachers" table="teacher_student_relation" cascade="save-update" inverse="false"&gt;
&lt;key column="student_id" not-null="true" /&gt;

&lt;many-to-many column="teacher_id"
class="org.py.hib.relation.many2many.Teacher"
/&gt;
&lt;/set&gt;
&lt;/class&gt;
&lt;/hibernate-mapping&gt;
</pre>
<p><span style='color: #ff0000;'> 注意:</span> </p>
<p> set中的 table 指向的是数据库中的关联表。</p>
<p>cascade 用的是<span style='color: #ff0000;'>save-update</span> , 且inverse用的是false,这样的话,当进行修改和保存和删除时,关联表中的记录也会删掉. </p>
<p>如果cascade 用的是 <span style='color: #ff0000;'>all</span> 那么连同student表中的记录也会被删除掉。</p>
<p>key中的column指的是: 关联表中与Student发生关系的字段。</p>
<p>而many-to-many中的column指的是:关联表中,与class(这里是:org.py.hib.relation.many2many.Teacher)发生关系的字段。</p>
<p>关于inverse,请参考上篇:<a href='190597' target='_blank'>hibernate入门使用系列 3-- 关系映射篇(中)</a> </p>
<p> </p>
<p> </p>
<p>Teacher.hbm.xml</p>
<pre name='code' class='xml'>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;

&lt;hibernate-mapping&gt;
&lt;class name="org.py.hib.relation.many2many.Teacher"
table="teacher"&gt;
&lt;id name="id" type="java.lang.String" column="tid"
length="32"&gt;
&lt;generator class="uuid" /&gt;
&lt;/id&gt;

&lt;property name="name" type="java.lang.String" column="name"
length="32" not-null="true" /&gt;

&lt;set name="students" table="teacher_student_relation" cascade="save-update"
inverse="false"&gt;
&lt;key column="teacher_id" not-null="true" /&gt;
&lt;many-to-many class="org.py.hib.relation.many2many.Student"
column="student_id" /&gt;
&lt;/set&gt;
&lt;/class&gt;
&lt;/hibernate-mapping&gt;
</pre>
<p><span style='color: #ff0000;'>  注意:</span> </p>
<p>这里的inverse也采用了false,这样子的话,Teacher和Student都维护关系表中的关系。</p>
<p> </p>
<p> </p>
<p>测试类,Many2ManyTest.java</p>
<pre name='code' class='java'>package org.py.hib.relation.many2many;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;

public class Many2ManyTest extends TestCase
{
private SessionFactory factory;

@Before
public void setUp() throws Exception
{
Configuration conf = new Configuration().configure();
factory = conf.buildSessionFactory();
}

/**
* 测试添加
* @throws Exception
*/
public void testSave() throws Exception
{
System.out.println("\n=== test save ===");

Teacher teacher1 = new Teacher();
teacher1.setName("teacher_1");

Teacher teacher2 = new Teacher();
teacher2.setName("teacher_2");

Student stu1 = new Student();
stu1.setName("student_1");

Student stu2 = new Student();
stu2.setName("student_2");

stu1.getTeachers().add(teacher1);
stu1.getTeachers().add(teacher2);

stu2.getTeachers().add(teacher2);
teacher1.getStudents().add(stu2);

Session session = null;
Transaction tran = null;
try
{
session = factory.openSession();
tran = session.beginTransaction();

session.save(stu1);
session.save(stu2);
tran.commit();

Assert.assertNotNull(teacher1.getId());
Assert.assertNotNull(teacher2.getId());

Assert.assertNotNull(stu1.getId());
Assert.assertNotNull(stu2.getId());

} catch (Exception ex)
{
tran.rollback();
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}

/**
* 测试从Teacher查询
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void testFindFromTeacher() throws Exception
{
System.out.println("\n=== test find from Teacher ===");
Session session = null;
try
{
session = factory.openSession();
Iterator&lt;Teacher&gt; iter = session.createQuery("from Teacher").iterate();
while (iter.hasNext())
{
Teacher teacher = iter.next();
Assert.assertNotNull(teacher.getId());
String teacherName = teacher.getName();
if ("teacher_1".equals(teacherName))
{
Set&lt;Student&gt; stus = teacher.getStudents();
Assert.assertEquals(stus.size(), 2);
for (Student stu : stus)
{
String stuName = stu.getName();
Assert.assertNotNull(stu.getId());
Assert.assertTrue(stuName.equals("student_1") || stuName.equals("student_2"));
}
} else if ("teacher_2".equals(teacherName))
{
Set&lt;Student&gt; stus = teacher.getStudents();
Assert.assertEquals(stus.size(), 2);

for (Student stu : stus)
{
String stuName = stu.getName();
Assert.assertNotNull(stu.getId());
Assert.assertTrue(stuName.equals("student_1") || stuName.equals("student_2"));
}
} else
{
throw new Exception("teacher name error exception.");
}
}
} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}

/**
* 测试从Student查询
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void testFindFromStudent() throws Exception
{
System.out.println("\n=== test find from Student ===");
Session session = null;
try
{
session = factory.openSession();
Iterator&lt;Student&gt; iter = session.createQuery("from Student").iterate();
while (iter.hasNext())
{
Student stu = iter.next();
Assert.assertNotNull(stu.getId());
String stuName = stu.getName();
if ("student_1".equals(stuName))
{
Set&lt;Teacher&gt; teachers = stu.getTeachers();
Assert.assertEquals(teachers.size(), 2);
for (Teacher teacher : teachers)
{
String tName = teacher.getName();
Assert.assertNotNull(teacher.getId());
Assert.assertTrue(tName.equals("teacher_1") || tName.equals("teacher_2"));
}
} else if ("student_2".equals(stuName))
{
Set&lt;Teacher&gt; teachers = stu.getTeachers();
Assert.assertEquals(teachers.size(), 2);
for (Teacher teacher : teachers)
{
String tName = teacher.getName();
Assert.assertNotNull(teacher.getId());
Assert.assertTrue(tName.equals("teacher_1") || tName.equals("teacher_2"));
}
} else
{
throw new Exception("student name error exception.");
}
}
} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}

/**
* 测试修改
* @throws Exception
*/
public void testModify() throws Exception
{
System.out.println("\n=== test modify ===");
Session session = null;
Transaction tran = null;
try
{
session = factory.openSession();
tran = session.beginTransaction();

Teacher t1 = (Teacher) session.createQuery("from Teacher t where t.name='teacher_1'").list().get(0);
t1.setName("new_teacher_1"); // 修改用户名 = m_name2.(原来用户名= m_name)

Set&lt;Student&gt; stus = t1.getStudents();
for (Student stu : stus)
{
if (stu.getName().equals("student_1"))
{
stus.remove(stu);
break;
}
}

tran.commit();

} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}

/*
* 修改后再查询
*/
System.out.println("\n=== test find from Teacher after modify===");
try
{
session = factory.openSession();
Iterator&lt;Teacher&gt; iter = session.createQuery("from Teacher").iterate();
while (iter.hasNext())
{
Teacher teacher = iter.next();
Assert.assertNotNull(teacher.getId());
String teacherName = teacher.getName();
if ("new_teacher_1".equals(teacherName))
{
Set&lt;Student&gt; stus = teacher.getStudents();
Assert.assertEquals(stus.size(), 1);
for (Student stu : stus)
{
String stuName = stu.getName();
Assert.assertNotNull(stu.getId());
Assert.assertTrue(stuName.equals("student_2"));
}
} else if ("teacher_2".equals(teacherName))
{
Set&lt;Student&gt; stus = teacher.getStudents();
Assert.assertEquals(stus.size(), 2);

for (Student stu : stus)
{
String stuName = stu.getName();
Assert.assertNotNull(stu.getId());
Assert.assertTrue(stuName.equals("student_1") || stuName.equals("student_2"));
}
} else
{
throw new Exception("teacher name error exception.");
}
}
} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}

/**
* 测试删除
* @throws Exception
*/
public void testDelete() throws Exception
{
System.out.println("\n=== test delete ===");
Session session = null;
Transaction tran = null;
try
{
session = factory.openSession();
tran = session.beginTransaction();

Iterator&lt;Teacher&gt; iter = session.createQuery("from Teacher").iterate();
while (iter.hasNext())
session.delete(iter.next());

tran.commit();

Integer count = (Integer) session.createQuery("select count(*) from Teacher").list().get(0);
Assert.assertEquals(0, count.intValue());

} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}

/*
* 删除后再查询
*/
System.out.println("\n=== test find after delete ===");
try
{
session = factory.openSession();

Integer num = (Integer) session.createQuery("from Father").list().size();
Assert.assertEquals(0, num.intValue());

num = (Integer) session.createQuery("from Child").list().size();
Assert.assertEquals(0, num.intValue());

} catch (Exception ex)
{
throw ex;
} finally
{
if (session != null)
{
try
{
session.close();
} catch (Exception ex)
{
// nothing to do
} finally
{
if (session != null)
session = null;
}
}
}
}

/**
*
*/
@After
public void tearDown() throws Exception
{
factory.close();
}

}
</pre>
<p> </p>
<p>从这个例子中可以看出,many-to-many中,需要引入第3张表来表示关系。</p>
<p>附件中有源代码。</p>
<p> </p>
<p> </p>
</div>
<p> </p>
6 楼 hantsy 2008-05-10  
真正应用中,在many to many 的关系中,真正纯粹作many to many关联的关联表是不多见的。
如,一个社区网站的圈子和成员是一个多对多的关系,可以用一个中间表来记录它们关系,但通常我们还记录与这种关系相关的一些数据。如会员是何时申请加入到圈子的,是否批准,何时批准,会员在圈子中角色,状态等。
5 楼 thebye85 2008-05-10  
讲的不错哦。谢谢
4 楼 boempire 2008-05-09  
不错,辛苦楼主了:)  很好的入门教程.
3 楼 毛毛虫 2008-05-08  
哎,好东西啊,再次感谢LZ分享,建议LZ在第一个页面做下第2个页面的连接,2连接3,3连接4,这样方便我们查看,只是LZ费心了.
哎,算是找到好东西了,谢啦,收藏..
2 楼 samsh 2008-05-08  
sdfsdfsdfsdf
1 楼 huevan 2008-05-08  
顶,新手的福音,楼主的劳动应该值得赞扬!

相关推荐

    hibernate入门--第一个实例

    - **XML映射文件**: 传统上,我们使用`hbm.xml`文件来定义对象关系映射。每个实体类都有一个对应的映射文件,其中定义了属性与数据库字段的映射关系。 - **注解映射**: 现代Hibernate更倾向于使用注解进行映射,...

    Hibernate入门到精通

    "Hibernate入门到精通" Hibernate 是一个基于Java的ORM(Object-Relational Mapping,对象关系映射)框架,它提供了一种简洁高效的方式来访问和操作关系数据库。下面是 Hibernate 的主要知识点: Hibernate 简介 ...

    Hibernate入门jar包

    本压缩包提供的是Hibernate入门所需的jar包,包括了Hibernate的核心库以及与之配合使用的相关组件。让我们深入探讨一下这些jar包以及它们在Hibernate中的作用。 1. Hibernate核心库: - `hibernate-core.jar`:这...

    Hibernate入门

    - **使用配置文件实现映射关系**:创建XML格式的配置文件(如`User.hbm.xml`),并在其中指定实体类与数据库表的映射规则。 - **引入DTD约束**:为了确保XML配置文件符合规范,需要在文件头部引入DTD(Document ...

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    Hibernate 是一个对象关系映射(ORM)框架,它允许开发者将Java对象与数据库表进行映射,简化了数据库操作。在Myeclipse中,通过Add Hibernate Capabilities来添加Hibernate支持。配置时,可以选择Hibernate版本(如...

    hibernate教程-从入门到精通-第4篇(共四篇)共2

    【hibernate教程-从入门到精通-第4篇(共四篇)共2】这篇教程是关于Java持久层框架Hibernate的学习指南,旨在帮助初学者深入理解并掌握Hibernate的核心概念和技术。Hibernate作为一款强大的对象关系映射(ORM)工具,...

    hibernate-3.6.8-doc-zh

    Hibernate,作为一款强大的对象关系映射(ORM)框架,为Java开发者提供了便捷的数据持久化服务。本文将深入解析Hibernate 3.6.8版本的中文帮助文档,帮助开发者更好地理解和运用这个优秀的工具。 一、Hibernate简介...

    初学hibernate,hibernate入门

    若使用XML映射,创建对应的`orm.hbm.xml`文件。 5. **初始化SessionFactory**:在应用程序启动时,根据`hibernate.cfg.xml`创建SessionFactory实例。 6. **使用Session**:通过SessionFactory获取Session,执行...

    Hibernate入门案例源码

    3. 映射实体类:使用XML映射文件(`.hbm.xml`)或注解来定义Java类与数据库表的关系。 4. 初始化SessionFactory:在应用程序启动时,根据配置文件创建SessionFactory实例,它是线程安全的,整个应用程序只需要一个。...

    使用Hibernate开发租房系统7-11

    在本教程“使用Hibernate开发租房系统7-11”中,我们将深入探讨如何利用Hibernate这一强大的对象关系映射(ORM)框架来构建一个完整的租房管理应用程序。Hibernate是Java开发人员常用于简化数据库交互的工具,它允许...

    hibernate系列(一)hibernate入门

    **hibernate系列(一)hibernate入门** 在Java世界中,ORM(Object-Relational Mapping,对象关系映射)框架是连接数据库与应用程序的重要桥梁,它将数据库中的数据与程序中的对象进行关联,使得开发者可以使用面向...

    hibernate 映射关系学习入门 一对一映射

    本文将深入探讨 Hibernate 中的一对一映射关系,并以"hibernate 映射关系学习入门 一对一映射Eclipse源码"为主题,详细解析其概念、配置以及实际应用。 一对一映射是Hibernate中的一种关联关系,它表示两个实体之间...

    hibernate使用参考文档

    本文档旨在指导读者快速从 Hibernate 入门到掌握 Hibernate 的使用。 Hibernate 配置文件 在使用 Hibernate 之前,需要将相关的 JAR 包导入到项目中。这些 JAR 包位于 `hibernate-release-4.2.5.Final\lib\...

    hibernate入门小例子

    【hibernate入门小例子】是一个适合初学者的教程,主要涵盖了如何在JavaWeb项目中使用Hibernate框架与MySQL数据库进行集成。在这个例子中,我们将会深入理解Hibernate的核心概念,包括实体映射、对象关系映射(ORM)...

    hibernate学习入门一对多实体映射源码

    在Hibernate中,一对多关系的映射通常通过XML配置文件(hibernate.cfg.xml)或注解实现。这里我们主要讨论注解方式: 1. 在父实体类上,我们需要使用`@OneToMany`注解来定义一对多关系。这个注解包含以下属性: - ...

    Hibernate入门(代码+笔记)

    然后,创建一个简单的实体类,例如`User`,并使用注解或XML映射文件将其映射到数据库中的`users`表。 **第二部分:Hibernate入门(下)** 在这一部分,我们将学习如何初始化SessionFactory,这是Hibernate的核心...

    Hibernate经典入门篇

    ### Hibernate经典入门篇知识点梳理 #### 一、Hibernate简介与作用 - **概念**:Hibernate是一种开放源码的对象关系映射(ORM)框架,用于Java应用与关系型数据库之间的交互。它提供了一种机制,使得Java对象能够...

    Hibernate完整使用教程

    Hibernate 的主要特点是将对象关系映射(O/R Mapping)技术应用于关系数据库,实现了对象到关系数据库的无缝集成。 为什么选择 Hibernate Hibernate 的出现是为了解决传统的 JDBC 编程中的问题。传统的 JDBC 编程...

    hibernate入门

    **hibernate入门** Hibernate 是一个强大的Java持久化框架,它简化了数据库操作,使得开发者无需直接编写SQL语句即可实现对象与关系数据库之间的映射。这个文档将带你步入Hibernate的世界,了解其基本概念和核心...

    Hibernate入门笔记

    ### Hibernate入门知识点详解 #### 一、环境搭建与配置 - **工程创建**:首先,创建一个名为`HibernateDemo`的Java工程,这是构建Hibernate应用的基础。为了使工程能够识别和运行Hibernate相关的代码,需要正确地...

Global site tag (gtag.js) - Google Analytics