Hibernate_Many-to-Many
来源:互联网 发布:cmd 执行java文件 编辑:程序博客网 时间:2024/05/21 09:37
昨天老师下午是打算讲Hibernate中 many-to-many 来着。可惜因为他从来不备课,所以啊,昨天又讲到一半就把自己给弄懵了.我们狂晕!!还是自己动手丰衣足食啦!
其实找些资料看一下,并没有想象的那么难,具体做法如下:
一,创建一个web工程,添加Hibernate 包(右键点击工程名称/Myeclipse/add Hibernate Capabilites/在弹出的对话框中选择next/next/在specify database connection details 中不将其选中,点击next/在java package选择一个包,如果没有就建一个(假如叫user),点击finish.
此时在user包中会看到一个名字叫HibernateSessionFactory.java类
二,在user包下,建立两个类,分别是Student.jave,Teacher.java
Student.jave
package user;
import java.util.HashSet;
import java.util.Set;
public class Student {
private Integer studentId;
private String studentName;
private Set<Teacher> teacherSet=new HashSet<Teacher>();
public Student(){}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Set<Teacher> getTeacherSet() {
return teacherSet;
}
public void setTeacherSet(Set<Teacher> teacherSet) {
this.teacherSet = teacherSet;
}
}
Teacher.java
package user;
import java.util.HashSet;
import java.util.Set;
public class Teacher {
private Integer teacherId;
private String teacherName;
private Set<Student> studentSet=new HashSet<Student>();
public Teacher(){}
public Set<Student> getStudentSet() {
return studentSet;
}
public void setStudentSet(Set<Student> studentSet) {
this.studentSet = studentSet;
}
public Integer getTeacherId() {
return teacherId;
}
public void setTeacherId(Integer teacherId) {
this.teacherId = teacherId;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
}
三,创建两个xml,分别为student.bm.ml,teacher.hbm.xml
student.bm.ml
<?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="user.Student" table="student" catalog="test">
<id name="studentId" type="integer">
<column name="studentId"></column>
<generator class="native"></generator>
</id>
<property name="studentName" type="string">
<column name="studentName"></column>
</property>
<set name="teacherSet" table="t_student_teacher" lazy="true" inverse="true" cascade="save-update">
<key column="studentId"></key>
<many-to-many class="user.Teacher" column="teacherId" />
</set>
</class>
</hibernate-mapping>
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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="user.Teacher" table="teacher" catalog="test"><!--teacher class -->
<id name="teacherId" type="integer"><!--class -->
<column name="teacherId" /><!--table -->
<generator class="native" />
</id>
<property name="teacherName" type="string"><!--class -->
<column name="teacherName" /><!--table -->
</property>
<!-- -->
<set name="studentSet" table="t_student_teacher" lazy="true" inverse="false" cascade="save-update"><!--中间表 -->
<key column="teacherId"></key><!--左边表 -->
<many-to-many class="user.Student" column="studentId"/><!--右边表 -->
</set>
</class>
</hibernate-mapping>
四,配置Hibernate.cfg.xml,配置之间需要向工程中添加连接数据库的驱动包(mysql-connector-java-3.1.7-bin.jar),如没有请下载
添加方法,右键点击工程名称/properties/java build path/Libraries/add external jars,然后选择包即可。
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">
jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<mapping resource="user/student.hbm.xml" />
<mapping resource="user/teacher.hbm.xml" />
</session-factory>
</hibernate-configuration>
五,创建一个类(Test),此类根据xml生成所需的表
package user;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class Test {
public static void main(String args[]) {
Configuration cfg=new Configuration().configure();
System.out.println("Configuration通过映射文件生成数据库的表");
SchemaExport sc=new SchemaExport(cfg);
sc.create(true, true);
System.out.println("完成生成数据库表");
}
}
之后在mysql中,会看到新生成的三张表,分别是teacher,student,t_student_teacher
六,写一个Save类,通过此类向表中插入数据
package user;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Save {
static Session session;
static Transaction tr;
public static void main(String args[]) {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
Student s1 = new Student();
Student s2 = new Student();
s1.setStudentName("aa学生");
s2.setStudentName("bb学生");
Teacher t1 = new Teacher();
Teacher t2 = new Teacher();
Teacher t3 = new Teacher();
t1.setTeacherName("aa老师");
t2.setTeacherName("bb老师");
t3.setTeacherName("cc老师");
s1.getTeacherSet().add(t3);
s1.getTeacherSet().add(t1);
s1.getTeacherSet().add(t2);
s2.getTeacherSet().add(t1);
s2.getTeacherSet().add(t2);
t1.getStudentSet().add(s1);
t1.getStudentSet().add(s2);
t2.getStudentSet().add(s2);
t2.getStudentSet().add(s1);
t3.getStudentSet().add(s1);
try {
session.save(s1);
session.save(s2);
session.save(t1);
session.save(t2);
tr.commit();
} catch (Exception e) {
if (tr != null) {
System.out.println("事务发生回滚");
tr.rollback();
}
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
}
}
六,写一个Get类,通过此类可以将学生与老师的对应关系显示出来,从而看出many-to-many的关系
package user;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Get {
static Session session;
static Transaction tr;
public static void main(String args[]) {
Get g=new Get();
g.Student_TeacherSet();
}
//TeacherSet
public void Student_TeacherSet() {
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
List list=session.createQuery("from Student s").list();
for(int i=0;i<list.size();i++)
{
Student s=(Student)list.get(i);
System.out.println("第"+(i+1)+"个学生名字是:"+s.getStudentName());
Set<Teacher> set=s.getTeacherSet();
Iterator it=set.iterator();
int j=0;
while(it.hasNext())
{
Teacher t=(Teacher)it.next();
System.out.println("第"+(i+1)+"个学生下的第"+(++j)+"个老师:"+t.getTeacherName());
}
}
} catch (Exception e) {
if (tr != null) {
System.out.println("事务发生回滚");
tr.rollback();
}
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
}
}
- hibernate_many-to-many
- Hibernate_Many-to-Many
- many-to-many性能
- hibernate-----many-to-many
- Hibernate many to many
- hibernate many-to-many
- many to many
- Many-to-many Query
- Many-to-Many
- many-to-many
- Hibernate Many-to-Many
- Hibernate Many-To-Many Revisited
- hibernate many to many查询
- hibernate映射many-to-many
- grials many-to-many配置
- django:Many-to-many关系
- Hibernate Many-to-Many Mappings
- Many to many relationships&Assignment
- ViewState 相关
- C#对图片的几种简单处理
- asp.net 缓存技术
- smtp发信出现的问题
- c# 中的正则表达式
- Hibernate_Many-to-Many
- JS+Hibernate+vss
- 局域网中根据IP地址反查主机的名称(C#)
- 在结构化表中,创建视图,合并字段自身的值
- 自己做的年级海报
- .NET获取系统特殊文件夹路径(收藏夹,桌面)
- Event
- Excel管理的一个类
- New Features in EJB3.1(Part 1)