hibernate(多对多)问题。
我在一个视频教程看了一下hibernate的用法,就按照视频上的做法写了一个,唯一不同是就是用数据库,我用的是sqlserver数据,打过sp4补丁。可是当运行到关联表的时候就报错:org.hibernate.exception.SQLGrammarException: could not insert collection: [org.eimhe.Student.teachers#7]
网上我也找了好久,始终不得解决,现将代码提供如下,希望高手指教。
顺便说一下,执行到数据关联关系表的时候数据库端口堵塞,怀疑是session有问题
student表(stuid,stuname)teacher表(teaid,teaname)
stu_tea_table(stuid,teaid)关系表
Student.hbm.xml
<?xml version= "1.0 "?>
<!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 - Hibernate Tools
-->
<hibernate-mapping>
<class name= "org.eimhe.Student " table= "student " schema= "dbo " catalog= "test ">
<id name= "stuid " type= "java.lang.Integer ">
<column name= "stuid " />
<generator class= "native " />
</id>
<property name= "stuname " type= "java.lang.String ">
<column name= "stuname " length= "50 " />
</property>
<set name= "teachers " table= "stu_tea_table "
lazy= "false "
cascade= "save-update "
inverse= "false ">
<key column= "stuid "> </key>
<many-to-many class= "org.eimhe.Teacher " column= "teaid "> </many-to-many>
</set>
</class>
</hibernate-mapping>
Teacher.hbm.xml
<?xml version= "1.0 "?>
<!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 - Hibernate Tools
-->
<hibernate-mapping>
<class name= "org.eimhe.Teacher " table= "teacher " schema= "dbo " catalog= "test ">
<id name= "teaid " type= "java.lang.Integer ">
<column name= "teaid " />
<generator class= "native " />
</id>
<property name= "teaname " type= "java.lang.String ">
<column name= "teaname " length= "50 " />
</property>
<set name= "students " table= "stu_tea_table "
lazy= "false "
cascade= "save-update "
sort= "unsorted ">
<key column= "teaid "> </key>
<many-to-many class= "org.eimhe.Student " column= "stuid " outer-join= "auto "> </many-to-many>
</set>
</class>
</hibernate-mapping>
数据存储:
public class StuDAO {
public void save(Student stu)
{
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
session.save(stu);
tx.commit();
}
}
调试类代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Student stu1=new Student();
stu1.setStuname( "stu1 ");
Student stu2=new Student();
stu2.setStuname( "stu2 ");
Teacher tea1=new Teacher();
tea1.setTeaname( "tea1 ");
Teacher tea2=new Teacher();
tea2.setTeaname( "tea2 ");
stu1.getTeachers().add(tea1);
stu1.getTeachers().add(tea2);
stu2.getTeachers().add(tea1);
stu2.getTeachers().add(tea2);
StuDAO studao=new StuDAO();
studao.save(stu1);
studao.save(stu2);
[解决办法]
补上报错代码
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.hibernate.exception.SQLGrammarException: could not insert collection: [org.eimhe.Student.teachers#7]
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:935)
org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:23)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
org.eimhe.StuDAO.save(StuDAO.java:12)
org.eimhe.MyTest.doGet(MyTest.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html