-->
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: Too many join tables
PostPosted: Wed Aug 13, 2008 3:11 pm 
Newbie

Joined: Wed Aug 13, 2008 2:01 pm
Posts: 1
Hello,

I'm trying to add attachments to various objects without having to create a join table for each object. For example, to add a set of attachments to BR, I created a join table called ATTACHMENT_BR and added the relationship to the hibernate mapping file.

<set name="attachments"
cascade="all, delete-orphan"
lazy="true"
table="ATTACHMENT_BR">
<key column="BR_ID" />
<many-to-many class="com.foo.bar.vo.Attachment" column="ATTACHMENT_ID" not-found="ignore"/>
</set>

CREATE TABLE ATTACHMENT_BR
(
BR_ID INTEGER NOT NULL,
ATTACHMENT_ID INTEGER NOT NULL
)

I'm trying to keep the Attachment object clean without adding a column and foreign key each time I relate it to another object.

However, adding attachments using join tables means if I add a set of attachments to a dozen objects, it requires a dozen join tables. Is there a way to relate one table to many others without having to create a separate join table for each one or add many columns to the table?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 13, 2008 5:22 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think I have made something similar and I solved by introducing an intermediate entity. In your case it would be called something like AttachementSet. The solution introduces an extra column in the tables of those objects that you want to add attachments to. Here are some example mappings which may help you. There may be some error and you need to add options for cascading, lazy loading, etc.

For any entity that needs association to attachments:

Code:
<many-to-one
   name="attachments"
   column="attachmentset_id"
   class="AttachmentSet"
  ... and other options as desired
/>


In AttachmentSet.hbm.xml:

Code:
<class
   name="AttachmentSet"
   table="ATTACHMENT_SETS"
   ... and other options as desired
   >
   <id column="attachmentset_id" />
   <set
      name="attachments"
      inverse="true"
      table="ATTACHMENT_JOINS"
      ... and other options as desired
       >
      <key column="attachmentset_id" />
      <many-to-many class="Attachment" column="attachment_id" />
   </set>
</class>



This should create one table (ATTACHMENT_SETS) with just a single column (attachmentset_id) and one join table (ATTACHMENT_JOINS) with two columns (attachmentset_id and attachment_id).

Another possible solution may be to use the <many-to-any> mapping but this probably needs to be done from the Attachment side. I am not very familiar with this so the examples below are just guesses. As I understand it though you get an extra column in the join table that holds the entity name. Eg something like:

Code:
<set
   name="entities"
   table="ATTACHMENT_JOINS"
   ... and other options
   >
   <key column="attachment_id"
    <many-to-any
       id-type="int"
       meta-type="string"
       >
       <column
         name="ENTITY_NAME"
         length="255"
         not-null="true"
      />
      <column
        name="ENTITY_ID"
        not-null="true"
       />
   </many-to-any>
</set>



The inverse mapping may need a "where" to filter on the ENTITY_NAME column, but I don't know if something like this works or not.
Code:
<set name="attachments"
   cascade="all, delete-orphan"
   lazy="true"
   where="ENTITY_NAME='BR'"
   inverse="true"
   table="ATTACHMENT_JOINS">
   <key column="ENTITY_ID" />
   <many-to-many class="com.foo.bar.vo.Attachment"
     column="ATTACHMENT_ID" not-found="ignore"/>
</set>


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.