-->
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.  [ 1 post ] 
Author Message
 Post subject: Reflexive many-to-many association
PostPosted: Mon Mar 07, 2005 10:31 am 
Newbie

Joined: Mon Mar 07, 2005 9:51 am
Posts: 3
Hi,

I'm quite new to Hibernate, so I might be posing a stupid question. I found something like this one on the forum already, but it had no replies yet. Therefore, I'll try to make it as clear as possible to what I'm trying to accomplish.

Version of Hibernate: 2.1.8, with MySQL 4.0.18

I'm working on a project and everything is going very well, except for one thing:
I have a table that needs to reference to its own members and in sql I would do it like this:
Code:
create table FOO
(
    FOO_ID int not null primary key auto_increment
)

create table FOOREFERENCE
(
   PARENT_ID int not null,
   CHILD_ID int not null,
   primary key(PARENT_ID, CHILD_ID)
)

Hibernate is doing exactly the same thing (as far as I can tell from the output below) and actually creates the tables correctly. The problem is that when I create the objects, add the other object and save them, only the objects are saved, but the link is not.

This is my mapping file (I stripped everything unnecessary like meta data):
Code:
<hibernate-mapping>

   <class name="com.packageurl.suburl.FOO" table="FOO">
      
      <id name="id" type="int" column="FOO_ID">
         <meta attribute="scope-set">protected</meta>
         <generator class="native"/>
      </id>
      
      <set name="references" table="FOOREFERENCE" cascade="all">
         <key column="FOO_ID" />
         <many-to-many class="com.storedesk.mailArchive.Mail" column="CHILD_ID" />
      </set>
   </class>
</hibernate-mapping>

, which causes Hibernate to output this:

[schemaexport] create table FOOREFERENCE (
[schemaexport] PARENT_ID integer not null,
[schemaexport] CHILD_ID integer not null,
[schemaexport] primary key (PARENT_ID, CHILD_ID)
[schemaexport] )

[schemaexport] create table FOO (
[schemaexport] FOO_ID integer not null auto_increment,
[schemaexport] primary key (MESSAGE_ID)
[schemaexport] )

[schemaexport] alter table FOOREFERENCE add index FK1B7BCF5491907F3E (CHILD_ID), add constraint FK1B7BCF5491907F3E foreign key (CHILD_ID) references FOO (FOO_ID)
[schemaexport] 14:04:14,951 DEBUG SchemaExport:149 - alter table FOOREFERENCE add index FK1B7BCF54217F38D0 (PARENT_ID), add constraint FK1B7BCF54217F38D0 foreign key (PARENT_ID) references FOO (FOO_ID)

Like I said, when I take a look in the database, the table is created correctly and when I add an object of type FOO like below, everything seems to be ok but the link isn't saved in the database (in table FOO, 2 new objects are inserted, but while there should be a line like "1, 2" - the keys from foo and fooref -, there simply is none).
Code:
Foo foo = new Foo();
Foo fooref = new Foo();

foo.getReferences().add(fooref);

try
{
   HibernateUtil.currentSession().save(fooref);
   HibernateUtil.currentSession().save(foo);
}
catch (HibernateException e)
{
   e.printStackTrace();
}

Finally, this is the SQL generated from the above statements (which shows that there are no inserts into the FOOREFERENCE table).

[java] Hibernate: insert into FOO () values ()
[java] Hibernate: insert into FOO () values ()


I really hope one of you can help me with this problem, since I've been trying to solve it for 2 days now. It's been putting quite some pressure on me, since it's a project for my internship which isn't progressing anymore now.

Lots of thanks in advance,

Benny 'Malavon' Goemans


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

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.