该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-09
数据库建表如下: drop database choose ; create unique index PK_student on student(sId); create unique index PK_course on course(coId); insert into student values('0621','jj,'668668668'); insert into course values('0001','汇编原理','3.0'); insert into course values('0002','网页设计与制作','2.0'); insert into chooseCourse values('0001','0621',76.7); insert into chooseCourse values('0002','0621',80.0);
Student.java package vo; import java.io.Serializable; import java.util.Set; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; public class Student implements Serializable { private String sId; private String sName; private String sPwd; private Set chooseCourses; public Student() { } public String getsname() { return sName; } public void setsname(String name) { sName = name; } public String getspwd() { return sPwd; } public void setspwd(String pwd) { sPwd = pwd; } public String getsId() { return sId; } public void setsId(String id) { sId = id; } public Set getChooseCourses() { return chooseCourses; } public void setChooseCourses(Set chooseCourses) { this.chooseCourses = chooseCourses; } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof ChooseCourse)) { return false; } Student student = (Student) obj; return new EqualsBuilder().append(this.sId, student.getsId()).append(this.sName, student.getsname()).append(this.sPwd, student.getspwd()).append(this.chooseCourses, student.getChooseCourses()).isEquals(); } public int hashCode() { return new HashCodeBuilder().append(this.sId).append( this.sName).append(this.sPwd).append(this.chooseCourses).toHashCode(); } } 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"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="vo.Student" table="student" catalog="choose"> <id name="sId" type="java.lang.String"> <column name="sId" length="10" /> <generator class="assigned" /> </id> <property name="sname" type="java.lang.String"> <column name="sName" length="20" not-null="true" /> </property> <property name="spwd" type="java.lang.String"> <column name="sPwd" length="20" not-null="true" /> </property> <set name="chooseCourses" inverse="true"> <key> <column name="sId" length="10" not-null="true" /> </key> <one-to-many class="vo.ChooseCourse" /> </set> </class> </hibernate-mapping> Course.java package vo; import java.io.Serializable; import java.util.Set; public class Course implements Serializable { private String coId; private String coName; private double credit; private Set chooseCourse; public Course() { } public String getCoId() { return coId; } public void setCoId(String coId) { this.coId = coId; } public String getCoName() { return coName; } public void setCoName(String coName) { this.coName = coName; } public double getCredit() { return credit; } public void setCredit(double credit) { this.credit = credit; } public Set getChooseCourse() { return chooseCourse; } public void setChooseCourse(Set chooseCourse) { this.chooseCourse = chooseCourse; } }
Course.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="vo.Course" table="course" catalog="choose"> <id name="coId" type="java.lang.String"> <column name="coId" length="10" /> <generator class="assigned" /> </id> <property name="coName" type="java.lang.String"> <column name="coName" length="20" not-null="true" /> </property> <property name="credit" type="java.lang.Double"> <column name="credit" precision="22" scale="0" not-null="true" /> </property> <set name="chooseCourse" inverse="true"> <key> <column name="coId" length="10" not-null="true" /> </key> <one-to-many class="vo.ChooseCourse" /> </set> </class> </hibernate-mapping> ChooseCourse.java package vo; import java.io.Serializable; public class ChooseCourse implements Serializable { private ccPK ccpk; private double grade; public ChooseCourse() { } public double getGrade() { return grade; } public void setGrade(double grade) { this.grade = grade; } public ccPK getCcpk() { return ccpk; } public void setCcpk(ccPK ccpk) { this.ccpk = ccpk; } } ChooseCourse.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="vo.ChooseCourse" table="choosecourse" catalog="choose"> <composite-id name="ccpk" class="vo.ccPK"> <key-many-to-one name="course" class="vo.Course"> <column name="coId" length="10" /> </key-many-to-one> <key-many-to-one name="student" class="vo.Student"> <column name="sId" length="10" /> </key-many-to-one> </composite-id> <property name="grade" type="java.lang.Double"> <column name="grade" precision="22" scale="0" /> </property> </class> </hibernate-mapping> 最终测试类Test.java: package test; import java.util.Iterator; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import vo.ChooseCourse; import vo.Course; import vo.Student; import vo.ccPK; public class Test { public static void main(String[] args) { Session session = util.HibernateSessionFactory.getSession(); Transaction tran = session.beginTransaction(); Student stu = (Student)session.get(Student.class, "0621"); Set set = stu.getChooseCourses() ; Iterator ite = set.iterator();//这里出错了!是为何?是配置错误还是测试类有问题? while(ite.hasNext()){ ChooseCourse ccc = (ChooseCourse)ite.next(); System.out.println(ccc.getGrade()); } } }
最后测试类出问题了,请高手看一下,是我的配置错误还是测试类有问题? 谢谢!
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-07-28
楼主怎么解决的
|
|
返回顶楼 | |
浏览 3649 次