-->
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.  [ 2 posts ] 
Author Message
 Post subject: many-to-many with a join table.....what am I doing wrong?
PostPosted: Wed Aug 09, 2006 4:30 pm 
Newbie

Joined: Wed Nov 23, 2005 11:37 am
Posts: 9
Hibernate version: 3.1

I have seen this issue discussed ad nauseum on this forum, but I'm afraid I'm still stuck. Hopefully someone can help me.

I have a traditional many-to-many association between two tables with an intervening join table. A Concert is associated with N Artists, and an Artist is associated with N Concerts. The join table in the middle of Concert and Artist supplies the many-to-many link. Here is the table structure:

Code:
mysql> desc concert_tbl;
+--------------+------------------+------+-----+---------------------+----------------+
| Field        | Type             | Null | Key | Default             | Extra          |
+--------------+------------------+------+-----+---------------------+----------------+
| concert_id   | int(12) unsigned |      | PRI | NULL                | auto_increment |
| concert_date | datetime         |      | MUL | 0000-00-00 00:00:00 |                |
| venue_id     | int(12) unsigned |      | MUL | 0                   |                |
| notes        | varchar(100)     | YES  |     | NULL                |                |
| flyer_img    | varchar(100)     | YES  |     | NULL                |                |
+--------------+------------------+------+-----+---------------------+----------------+
5 rows in set (0.00 sec)

mysql> desc artist_tbl;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| artist_id     | int(12) unsigned |      | PRI | NULL    | auto_increment |
| name          | varchar(100)     |      |     |         |                |
| website_url   | varchar(100)     | YES  |     | NULL    |                |
| email_address | varchar(100)     | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> desc concert_artists_tbl;
+------------+------------------+------+-----+---------+-------+
| Field      | Type             | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| concert_id | int(12) unsigned |      | PRI | 0       |       |
| artist_id  | int(12) unsigned |      | PRI | 0       |       |
+------------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)



And here are my associated mappings:


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="Concert" table="concert_tbl">
       <id name="concertID" column="concert_id">
          <generator class="identity"/>
       </id>
       
       <property name="concertDate" column="concert_date"/>
       <property name="notes"/>
       <property name="flyerImage" column="flyer_img"/>
       
       <!-- The associated Venue -->
       <many-to-one name="venue" column="venue_id" unique="true" not-null="true" lazy="false"/>

       <!-- The associated Artists -->
       <set name="artists" table="concert_artists_tbl" lazy="false" cascade="all">
         <key column="concert_id" not-null="true"/>
         <many-to-many
            column="artist_id"
            class="Artist"
            outer-join="auto"/>
       </set>
    </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="Artist" table="artist_tbl">
       <id name="artistID" column="artist_id">
          <generator class="identity"/>
       </id>
       
       <property name="name"/>
       <property name="websiteURL" column="website_url"/>
       <property name="emailAddress" column="email_address"/>

       <!-- The associated Concerts -->
       <set name="concerts" table="concert_artists_tbl" lazy="false" cascade="all">
         <key column="artist_id" not-null="true"/>
         <many-to-many
            column="concert_id"
            class="Concert"
            outer-join="auto"/>
       </set>
    </class>
</hibernate-mapping>



Finally, here is my code for associating Artists with a Concert:

Code:
            Venue venue = daoFactory.getVenueDAO().findByID(venueID, false);
            Concert concert
                = new Concert(concertDate, notes, flyerImageName, venue);
            for (int i = 0; i < artistIDs.length; i++) {
                Artist artist = daoFactory.getArtistDAO().findByID(artistIDs[i], false);
                concert.getArtists().add(artist);
                artist.getConcerts().add(concert);
            }
            // NOTE: persist does a saveOrUpdate() on the Session
            daoFactory.getConcertDAO().persist(concert);



Here's the problem: the data in concert_tbl ends up getting inserted correctly, but no rows are getting inserted into concert_artists_tbl in the database, thus the association from a Concert to it's Artists is not persisted.

As you can see in the code above, Artist objects are not being created, they already exist. I am simply attempting to create a new Concert object, and associate that Concert with a set of Artists that the user selected in the UI, by looking up the Artist by it's ID, and adding it to the Concert's artists collection.

I don't know if it matters, but my web app (where this is being used) is using the Open Session In View pattern.

I've tried a bunch of different mappings, and I always run into the same problem of records not being inserted into concert_artists_tbl. Any help would be greatly appreciated. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 4:09 pm 
Newbie

Joined: Wed Nov 23, 2005 11:37 am
Posts: 9
I finally got this working by applying the following mappings, after re-reading section "6.3.2. Bidirectional associations" in the documentation.

Concert.hbb.xml
Code:
<hibernate-mapping>

    <class name="Concert" table="concert_tbl">
       <id name="concertID" column="concert_id">
          <generator class="identity"/>
       </id>
       
       <property name="concertDate" column="concert_date"/>
       <property name="notes"/>
       <property name="flyerImage" column="flyer_img"/>
       
       <!-- The associated Venue -->
       <many-to-one name="venue" column="venue_id" unique="true" not-null="true" lazy="false"/>

       <!-- The associated Artists -->
       <set name="artists" table="concert_artists_tbl" lazy="false" cascade="save-update,delete-orphan">
         <key column="concert_id" not-null="true"/>
         <many-to-many
            column="artist_id"
            class="Artist"
            outer-join="auto"/>
       </set>
    </class>
</hibernate-mapping>



Artist.hbm.xml
Code:
<hibernate-mapping>

    <class name="Artist" table="artist_tbl">
       <id name="artistID" column="artist_id">
          <generator class="identity"/>
       </id>
       
       <property name="name"/>
       <property name="websiteURL" column="website_url"/>
       <property name="emailAddress" column="email_address"/>

       <!-- The associated Concerts -->
       <set name="concerts" table="concert_artists_tbl" lazy="false" cascade="save-update" inverse="true">
         <key column="artist_id" not-null="true"/>
         <many-to-many
            column="concert_id"
            class="Concert"
            outer-join="auto"/>
       </set>

    </class>   
</hibernate-mapping>


Note the inverse="true" on Artist, and the cascade settings. Hope this helps!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.