求助,hibernate做一对多关系遇到问题

fzs2316
08-04-29 4 307

两张表,教师和学生表,分别是1对多,一个老师对应多个学生,在插入时报错,
org.hibernate.exception.GenericJDBCException: could not insert: [org.vo.Student]
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1777)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2178)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:240)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at

fzs2316
2008-04-29 12:46

实现类,学生
package org.vo;

/**
* Student generated by MyEclipse Persistence Tools
*/

public class Student implements java.io.Serializable {

// Fields

private Integer id;

private String studentName;

/*private Teacher tea;*/


// Constructors




/** default constructor */
public Student() {
}

/** full constructor */

// Property accessors

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

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

public String getStudentName() {
return this.studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

/**
* @return the tea
*/
/*public Teacher getTea() {
return tea;
}*/

/**
* @param tea the tea to set
*/
/*public void setTea(Teacher tea) {
this.tea = tea;
}*/



}

fzs2316
2008-04-29 12:47

实体类教师
package org.vo;

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

/**
* Teacher generated by MyEclipse Persistence Tools
*/

public class Teacher implements java.io.Serializable {

// Fields

private Integer id;

private String teacherName;

private Set stu=new HashSet();

// Constructors

/** default constructor */
public Teacher() {
}

/** full constructor */
public Teacher(String teacherName) {
this.teacherName = teacherName;
}

// Property accessors

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

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

public String getTeacherName() {
return this.teacherName;
}

public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}

/**
* @return the stu
*/
public Set getStu() {
return stu;
}

/**
* @param stu the stu to set
*/
public void setStu(Set stu) {
this.stu = stu;
}

}

fzs2316
2008-04-29 12:48

映射文件学生
<?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="org.vo.Student" table="student" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="studentName" type="java.lang.String">
<column name="studentName" length="20" not-null="true" />
</property>
<!--<many-to-one name="tea" column="teacher_id" class="org.vo.Teacher" fetch="select" lazy="false" cascade="save-update" />-->
</class>
</hibernate-mapping>

教师
<?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="org.vo.Teacher" table="teacher" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="teacherName" type="java.lang.String">
<column name="teacherName" length="20" not-null="true" />
</property>

<set name="stu" inverse="true" fetch="select" cascade="save-update" lazy="true">
<key column="teacher_id" />
<one-to-many class="org.vo.Student" />
</set>
</class>
</hibernate-mapping>

fzs2316
2008-04-29 12:50

测试类

Session ses=null;
Transaction tr=null;
try{
ses=HibernateSessionFactory.getSession();

tr=ses.beginTransaction();
Set hs=new HashSet();

Teacher t=new Teacher();
t.setTeacherName("seeed");

Student st=new Student();
st.setStudentName("dww");
hs.add(st);
Student st2=new Student();
st2.setStudentName("dee");
hs.add(st2);
t.setStu(hs);

ses.save(t);

tr.commit();


/*tr=ses.beginTransaction();
Student st=new Student();
st.setStudentName("sdd");

Teacher tc=new Teacher();
tc.setTeacherName("sdd");
st.setTea(tc);
ses.save(st);


tr.commit();*/
}catch(Exception ex){
ex.printStackTrace();
tr.rollback();
}finally{
ses.close();
}

搞了几天,不知哪里错?多对一关系就做出来了,就是一对多关系有问题,求助各位帮帮忙?