HI friends,
I have a design, you can find the ER diagram on
http://213.42.180.226/problem.jpg. Basically there is a many-to-many relationship between Book and Explainer of a book, the result relation has a one-to-many relation with pages (please visit
http://213.42.180.226/problem.jpg to understand it thoroughly). The problem is that it shows an Exception "Foreign Key XXX must have same number of columns as the referenced primary key".
Has anybody gone through such case ? Please advise.
Look forward to hearing from you
Yours,
Zaid
Hibernate version:3.0
Mapping documents:
<class name="Books" table="books">
<id name="bookId" column="book_id" type="integer">
<generator class="native"/>
</id>
<property name="bookName" column="book_name" type="string" not-null="true" />
<set name="explainers" table="explainers_books">
<key column="book_id"/>
<many-to-many class="Explainers" column="explainer_id"/>
</set>
</class>
<class name="Explainers" table="explainers">
<id name="explainerId" column="explainer_id" type="integer">
<generator class="native"/>
</id>
<property name="explainerName" column="explainer_name" type="string" not-null="true" />
<set name="books" table="explainers_books">
<key column="explainer_id"/>
<many-to-many class="Books" column="book_id"/>
</set>
</class>
<class name="ExplainersBooks" table="explainers_books">
<id name="explainerBookId" column="explainer_book_id" type="integer">
<generator class="native"/>
</id>
<property name="bookId" column="book_id" type="integer" not-null="true" />
<property name="explainerId" column="explainer_id" type="integer" not-null="true" />
<set name="pages">
<key column="page_id"/>
<one-to-many class="Pages"/>
</set>
</class>
<class name="Pages" table="pages">
<id name="pageId" column="page_id" type="integer">
<generator class="native"/>
</id>
<property name="explainerBookId" column="explainer_book_id" type="integer" not-null="true" />
<property name="pageName" column="page_name" type="string" not-null="true" />
</class>
Code between sessionFactory.openSession() and session.close():
Books book=(Books)sess.load(Books.class,new Integer(1));
System.out.println("book="+book.getExplainers().size());
Full stack trace of any exception that occurs:
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Foreign key (FK657EFC4EA1D5846:pages [page_id])) must have same number of columns as the referenced primary key (explainers_books [explainer_id,book_id])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:86)
at org.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:51)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:976)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:921)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:999)
at com.zaid.model.hibernate.SessionFactory.currentSession(SessionFactory.java:49)
at com.zaid.struts.action.GetAddressesAction.execute(GetAddressesAction.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
[ WARN] 08:47:00 (RequestProcessor.java:processException:516)
Unhandled Exception thrown: class java.lang.NullPointerException
[ERROR] 08:47:00 (StandardWrapperValve.java:invoke:253)
Servlet.service() for servlet action threw exception
Name and version of the database you are using: MySQL 4.1