5个表,关系如下:
报错信息:
严重: Servlet.service() for servlet jsp threw exception org.hibernate.MappingException: Foreign key (FKEE834461D63EFD3A:answersheet [surQue_id])) must have same number of columns as the referenced primary key (sur_que [survey_id,question_id]) at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113) at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:96) at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1310) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1217) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:329) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) at com.util.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:21) at com.daoImpl.RoleDaoImpl.<init>(RoleDaoImpl.java:16) at org.apache.jsp.Login_jsp._jspService(Login_jsp.java:124) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.util.ChinaFilter.doFilter(ChinaFilter.java:16) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600) at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703) at java.lang.Thread.run(Thread.java:619)
具体请看:http://www.iteye.com/problems/97400
没人回答!我就自己答啦!报错语句:Foreignkey(FKEE834461D63EFD3A:answersheet[surQue_id]))musthavesamenumberofcolumnsasthereferencedprimarykey(sur_que[survey_id,question_id])意思就是:answersheet表的外键surQue_id对应的表sur_que的主键是联合主键[survey_id,question_id],而在answersheet外键就写了一个,应该写两个,即(survey_id,question_id)才对。
解决办法:hibernate多对多@ManyToMany生成的中间表默认是联合主键的,即使你在中间表的实体类中加入@Id也不行。只有手动建表,但是手动建表也不能消除这个bug,所以你只要把多对多用两个多对一替换就行啦!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。