-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Many-to-Many Association with one-to-many
PostPosted: Wed Dec 28, 2005 12:57 pm 
Newbie

Joined: Tue Jan 04, 2005 4:05 am
Posts: 8
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 28, 2005 3:15 pm 
Newbie

Joined: Tue Oct 18, 2005 2:49 pm
Posts: 13
Zaid,

What you are trying to do is make the association bidirectional. The natural direction of this relationship is a many-to-one from Pages to ExplainerBooks.

You should first specify a many-to-one relationship in Pages for ExplainerBooks. After that set the inverse=true for the Pages set in ExplainerBooks. I think that should do it.

-Pratik


Top
 Profile  
 
 Post subject: Didn't work
PostPosted: Thu Dec 29, 2005 2:19 am 
Newbie

Joined: Tue Jan 04, 2005 4:05 am
Posts: 8
Hi Pratik,

Thanks for your reply. I tried to make it bidirectional, however it didn't work, same error message.

I even tried to remove the <one-to-many> in ExplainerBooks and added the <many-to-one> in Pages configuration, but it didn't work as well.

I think the problem is that Hibernate treats the combination of foreign keys in the middle Class (ExplainerBooks) as a primary key !.

Any suggestions ?

Thanks,
Zaid


Top
 Profile  
 
 Post subject: found it !
PostPosted: Wed Jan 11, 2006 4:30 am 
Newbie

Joined: Tue Jan 04, 2005 4:05 am
Posts: 8
Pratik, I found it

Simply, use idbag instead of set, because it is the only which can generate primary keys

in Explainers, replace the <SET> with the following:


<idbag name="books" table="explainers_books">
<collection-id type="integer" column="explainer_book_id">
<generator class="native"></generator>
</collection-id>
<key column="explainer_id"/>
<many-to-many class="Books" column="book_id"/>
</idbag>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.