关于Hibernate多对多拆分为多个一对多的问题.

07-05-14 zhanghaidang
关于Hibernate多对多拆分为多个一对多的问题.

开发环境为:MyEclipse5.1

数据库:

create table student

(

sid int(8) not null auto_increment,

sname varchar(30) not null default '',

primary key(sid)

)engine=innodb default charset=gbk;

create table teacher

(

tid int(8) not null auto_increment,

tname varchar(30) not null default '',

primary key(tid)

)engine=innodb default charset=gbk;

create table student_teacher_link

(

id int(8) not null auto_increment,

sid int(8),

tid int(8),

primary key(id),

foreign key(sid) references student(sid),

foreign key(tid) references teacher(tid)

)engine=innodb default charset=gbk;

然后通过MyEclipse自动生成功能,生成以下hbm.xml和POJO类文件

学生类

public class Student{

private Integer sid;

private String sname;

private Set studentTeacherLinks = new HashSet(0);

public Student() {}

public Student(String sname) {

this.sname = sname;

}

public Student(String sname, Set studentTeacherLinks) {

this.sname = sname;

this.studentTeacherLinks = studentTeacherLinks;

}

/*一系列set/get方法*/

}

老师类

public class Teacher{

private Integer tid;

private String tname;

private Set studentTeacherLinks = new HashSet(0);

public Teacher() {}

public Teacher(String tname) {

this.tname = tname;

}

public Teacher(String tname, Set studentTeacherLinks) {

this.tname = tname;

this.studentTeacherLinks = studentTeacherLinks;

}

/*一系列set/get方法*/

}

主键关联类

public class StudentTeacherLink{

private Integer id;

private Teacher teacher;

private Student student;

public StudentTeacherLink() {}

public StudentTeacherLink(Teacher teacher, Student student) {

this.teacher = teacher;

this.student = student;

}

/*一系列set/get方法*/

}

1
zhanghaidang
2007-05-14 21:57
三个映射文件

Studnet.hbm.xml

<class name="com.hibernate.pojo.Student" table="student">

<id name="sid" type="java.lang.Integer">

<column name="sid" />

<generator class="native" />

</id>

<property name="sname" type="java.lang.String">

<column name="sname" length="30" not-null="true" />

</property>

<set name="studentTeacherLinks" inverse="true" lazy="true">

<key>

<column name="sid" unique="true" />

</key>

<one-to-many class="com.hibernate.pojo.StudentTeacherLink" />

</set>

</class>

Teacher.hbm.xml

<class name="com.hibernate.pojo.Teacher" table="teacher">

<id name="tid" type="java.lang.Integer">

<column name="tid" />

<generator class="native" />

</id>

<property name="tname" type="java.lang.String">

<column name="tname" length="30" not-null="true" />

</property>

<set name="studentTeacherLinks" inverse="true" lazy="true">

<key>

<column name="tid" unique="true" />

</key>

<one-to-many class="com.hibernate.pojo.StudentTeacherLink" />

</set>

</class>

StudentTeacherLink.hbm.xml

<class name="com.hibernate.pojo.StudentTeacherLink" table="student_teacher_link">

<id name="id" type="java.lang.Integer">

<column name="id" />

<generator class="native" />

</id>

<many-to-one name="teacher" class="com.hibernate.pojo.Teacher" cascade="save-update">

<column name="tid" unique="true" />

</many-to-one>

<many-to-one name="student" class="com.hibernate.pojo.Student" cascade="save-update">

<column name="sid" unique="true" />

</many-to-one>

</class>

测试类

public class UtilDAO {

public static void saveStudent(){

//两个学生,两个老师

Student stu1 = new Student("亚当");

Student stu2 = new Student("夏娃");

Teacher tea1 = new Teacher("张老师");

Teacher tea2 = new Teacher("李老师");

//相互关联

StudentTeacherLink st1 = new StudentTeacherLink(tea1,stu1);

StudentTeacherLink st2 = new StudentTeacherLink(tea1,stu2);

StudentTeacherLink st3 = new StudentTeacherLink(tea2,stu1);

StudentTeacherLink st4 = new StudentTeacherLink(tea2,stu2);

Session session = null;

Transaction tr = null;

try{

session = HibernateSessionFactory.getSession();

tr = session.beginTransaction();

//保存数据

session.saveOrUpdate(st1);

session.saveOrUpdate(st2);

session.saveOrUpdate(st3);

session.saveOrUpdate(st4);

tr.commit();

}catch(Exception e){

e.printStackTrace();

tr.rollback();

}finally{

session.close();

}

}

public static void printStudent(){

Session session = null;

Transaction tr = null;

try{

session = HibernateSessionFactory.getSession();

tr = session.beginTransaction();

//查找老师ID为1的老师

Query query = session.createQuery("from Teacher t where t.tid=1");

//取得老师对象

Teacher ter = (Teacher)query.uniqueResult();

//???这里我不知道该不该这样获取学生集合

Set set = ter.getStudentTeacherLinks();

Iterator it = set.iterator();

while(it.hasNext()){

//***在这里取出学生对象时,发生异常了(java.lang.ClassCastException: com.hibernate.pojo.StudentTeacherLink)

Student stu = (Student)it.next();

System.out.println(stu.getSname());

}

tr.commit();

}catch(Exception e){

e.printStackTrace();

tr.rollback();

}finally{

session.close();

}

}

public static void main(String[] args){

//UtilDAO.saveStudent();

UtilDAO.printStudent();

}

}

问题:

1.请问:我能这样获取学生集合吗?

Set set = ter.getStudentTeacherLinks();

2.请问:我这样关联学生和老师的对象对吗?

StudentTeacherLink st1 = new StudentTeacherLink(tea1,stu1);

3.我如果想通过老师对象来查看对应的学生集合该怎么做呢?

banq
2007-05-16 11:02
你使用Hibernate的方式就是错误的。

你是先有数据库:>create table student

所以产生后面琐碎问题,就象使用扳手工具,你是倒过来拿,你还要求扳手帮你做更多活,怎么可能。

相关贴:

http://www.jdon.com/jivejdon/thread/31684.html

猜你喜欢