数据库建表如下:
drop database choose ;
create database choose ;
use choose;
--student
create table student(
sId char(10) not null primary key,
sName char(20) not null,
sPwd char(20) not null
);
create unique index PK_student on student(sId);
--course
create table course(
coId char(10) not null primary key,
coName char(20) not null,
credit float(53) not null
);
create unique index PK_course on course(coId);
--chooseCourse
create table chooseCourse(
coId char(10) not null,
sId char(10) not null,
grade float(53),
primary key(coId,sId),
foreign key (coId) references course(coId),
foreign key (sId) references student(sId)
);
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()); } } }
最后测试类出问题了,请高手看一下,是我的配置错误还是测试类有问题?
谢谢!