-->
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: mapping many to many for same class
PostPosted: Wed Jul 13, 2005 5:03 pm 
Newbie

Joined: Fri Jan 23, 2004 5:19 pm
Posts: 13
i'm having trouble mapping this:

i have a Bug object, which can contain zero or more related Bugs. i have a join table "rel_related_bugs_tbl" to represent this, which contains

int rel_bug_id
int rel_related_bug_id

i believe i'm setting the related bugs in my object properly here:
Code:
Session sess = Global.getSession();
Transaction tx = sess.beginTransaction();
Bug bug = new Bug(title, description, solution);

List list = getRelatedBugs();
Iterator i = list.iterator();

while (i.hasNext()) {
   Long key = (Long)i.next();
   Bug related = (Bug) sess.get(Bug.class, key);
   LOG.debug("getting bug ["+key+"]: " + related);
   bug.addRelatedBug(related);
}


because i'll get log output like
Code:
10976 AddBug [DEBUG] getting bug [13]: bugger.model.Bug@c8f0a4


however when i commit the transaction, i only get the sql for the new bug record, and nothing in the related join table.

Hibernate version: 3

Mapping documents:
Code:
<hibernate-mapping>
   <class
      name="bugger.model.Bug"
      table="bug_bugs_tbl">
      <id
         name="id"
         column="bug_id">
         <generator class="increment" />
      </id>
      <property
         name="title"
         type="string">
         <column
            name="bug_title"
            not-null="true" />
      </property>

      <set
         name="relatedBugs"
         table="rel_related_bugs_tbl"
         inverse="true">
         <key column="rel_bug_id"/>
         <many-to-many
            class="bugger.model.Bug"
            column="bug_id" />
      </set>
   </class>
</hibernate-mapping>


Full stack trace of any exception that occurs: none

Name and version of the database you are using:mysql 4.0.22-nt

The generated SQL (show_sql=true): Hibernate: insert into bug_bugs_tbl (bug_title, bug_description, bug_solution, bug_created, bug_id) values (?,
?, ?, ?, ?)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 4:11 pm 
Newbie

Joined: Fri Jan 23, 2004 5:19 pm
Posts: 13
bump... surely i'm not the first person to attempt something like this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 11:25 am 
Newbie

Joined: Fri Jan 23, 2004 5:19 pm
Posts: 13
has anyone even read this thread? can i at least get a reply? even if it's "hey buddy that problem sucks... don't know how to help you but good luck anyway"

any help would be appreciated.

i've read through chapter 5 of Hibernate In Action so far and nothing has covered mapping a class to itself in a NON parent-child many to many fashion. the only thing close has been a Category with a one to many sub Category. this is not the same as my situation, for the Bugs simply have similar Bugs, not parent or children bugs.


update:

i've changed the <set> to this:
Code:
      <set
         name="relatedBugs"
         table="rel_related_bugs_tbl"
         outer-join="true">
         <key column="bug_id"/>
         <many-to-many
            class="bugger.model.Bug"
            column="rel_bug_id" />
      </set>


but still no go.

in Eclipse Hibernate Console, when I view mapped entities for Bug, i get a never-ending tree of Bugs e.g.
Code:
Bug
id
title
relatedBugs
  id
  title
  relatedBugs
   id
   title
   relatedBugs
    id
    title
    relatedBugs...


if i click on any of the relatedBugs properties, i get an error:
Code:
!SUBENTRY 2 org.hibernate.eclipse.console 4 150 2005-07-19 11:09:35.393
!MESSAGE unexpected end of subtree []
!STACK 0
org.hibernate.hql.ast.QuerySyntaxError: unexpected end of subtree []
   at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:196)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
   at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
   at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at org.hibernate.console.HQLQueryPage.getList(HQLQueryPage.java:30)
   at org.hibernate.eclipse.console.views.QueryPageViewer$ContentProviderImpl.getElements(QueryPageViewer.java:80)
   at org.eclipse.jface.viewers.StructuredViewer.getRawChildren(StructuredViewer.java:848)
   at org.eclipse.jface.viewers.TableViewer.getRawChildren(TableViewer.java:1086)
   at org.eclipse.jface.viewers.StructuredViewer.getFilteredChildren(StructuredViewer.java:790)
   at org.eclipse.jface.viewers.StructuredViewer.getSortedChildren(StructuredViewer.java:905)
   at org.eclipse.jface.viewers.TableViewer.internalRefreshAll(TableViewer.java:798)
   at org.eclipse.jface.viewers.TableViewer.internalRefresh(TableViewer.java:748)
   at org.eclipse.jface.viewers.TableViewer.internalRefresh(TableViewer.java:737)
   at org.eclipse.jface.viewers.StructuredViewer$7.run(StructuredViewer.java:1264)
   at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1201)
   at org.eclipse.jface.viewers.StructuredViewer.refresh(StructuredViewer.java:1262)
   at org.eclipse.jface.viewers.StructuredViewer.refresh(StructuredViewer.java:1221)
   at org.eclipse.jface.viewers.TableViewer.inputChanged(TableViewer.java:698)
   at org.eclipse.jface.viewers.ContentViewer.setInput(ContentViewer.java:248)
   at org.eclipse.jface.viewers.StructuredViewer.setInput(StructuredViewer.java:1417)
   at org.hibernate.eclipse.console.views.QueryPageViewer.createTable(QueryPageViewer.java:174)
   at org.hibernate.eclipse.console.views.QueryPageViewer.createControl(QueryPageViewer.java:153)
   at org.hibernate.eclipse.console.views.QueryPageViewer.<init>(QueryPageViewer.java:111)
   at org.hibernate.eclipse.console.views.QueryPageTabView.rebuild(QueryPageTabView.java:83)
   at org.hibernate.eclipse.console.views.QueryPageTabView$1.contentsChanged(QueryPageTabView.java:52)
   at org.hibernate.eclipse.console.views.QueryPageTabView$1.intervalAdded(QueryPageTabView.java:57)
   at javax.swing.AbstractListModel.fireIntervalAdded(Unknown Source)
   at org.hibernate.console.QueryPageModel.add(QueryPageModel.java:50)
   at org.hibernate.console.KnownConfigurations$3.queryPageCreated(KnownConfigurations.java:167)
   at org.hibernate.console.ConsoleConfiguration.fireQueryPageCreated(ConsoleConfiguration.java:266)
   at org.hibernate.console.ConsoleConfiguration.executeHQLQuery(ConsoleConfiguration.java:247)
   at org.hibernate.eclipse.console.views.KnownConfigurationsView$5.run(KnownConfigurationsView.java:143)
   at org.hibernate.eclipse.console.views.KnownConfigurationsView$3.doubleClick(KnownConfigurationsView.java:91)
   at org.eclipse.jface.viewers.StructuredViewer$1.run(StructuredViewer.java:720)
   at org.eclipse.core.internal.runtime.InternalPlatform.run(InternalPlatform.java:1044)
   at org.eclipse.core.runtime.Platform.run(Platform.java:783)
   at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:44)
   at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:148)
   at org.eclipse.jface.viewers.StructuredViewer.fireDoubleClick(StructuredViewer.java:718)
   at org.eclipse.jface.viewers.StructuredViewer.handleDoubleSelect(StructuredViewer.java:950)
   at org.eclipse.jface.viewers.StructuredViewer$4.widgetDefaultSelected(StructuredViewer.java:1057)
   at org.eclipse.jface.util.OpenStrategy.fireDefaultSelectionEvent(OpenStrategy.java:219)
   at org.eclipse.jface.util.OpenStrategy.access$0(OpenStrategy.java:216)
   at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:275)
   at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
   at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:843)
   at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3080)
   at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2713)
   at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1699)
   at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1663)
   at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:367)
   at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:143)
   at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:103)
   at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:226)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:376)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:163)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.eclipse.core.launcher.Main.invokeFramework(Main.java:334)
   at org.eclipse.core.launcher.Main.basicRun(Main.java:278)
   at org.eclipse.core.launcher.Main.run(Main.java:973)
   at org.eclipse.core.launcher.Main.main(Main.java:948)
Caused by: <AST>:0:0: unexpected end of subtree
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2724)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:513)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:371)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
   ... 66 more



Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 11:59 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
In the two table situation, <many-to-many> uses an associative table to join two tables together. So if you have table 1 (t1) and table 2 (t2), the associative table would be t1t2, with a t1 column, and a t2 column. Your problem is probably that you have one <many-to-one> tag, because there is only one table involved. The <many-to-one> only specifies ONE column, so your associative table needs to be t1t1 with a t1 column and a rel_t1 column, but you aren't telling Hibernate to create two columns in the associative table (try to find the second column in your relation table). You have to have two <many-to-many> elements, with different names, and different column names. There has to be a direction to the relationship (related TO vs. releted BY, or something like that). Try something like this:

Code:
<set
         name="relatedBugs"
         table="rel_related_bugs_tbl"
         inverse="true">
         <key column="rel_bug_id"/>
         <many-to-many
            class="bugger.model.Bug"
            column="bug_id" />
      </set>

<set
         name="relatedByBugs"
         table="rel_related_bugs_tbl"
         inverse="true">
         <key column="rel_by_bug_id"/>
         <many-to-many
            class="bugger.model.Bug"
            column="bug_id" />
      </set>


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.