-->
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.  [ 5 posts ] 
Author Message
 Post subject: Question on multi-tier relationship mapping
PostPosted: Mon Mar 08, 2004 7:24 pm 
Beginner
Beginner

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 20
Hi,

After scratching my head for hours, may be someone out there have already done similar stuff - any advice is appreciated.

I have persistent objects Entity1 and Entity2 (in the actual app, there are even "Entity10"). Both of them contain a list of enitity "Attachment":

abstract Class AbstractEntity{
private id;
...
}

Class Entity1 extends AbstractEntity{
private Set attachments;
}

Class Entity2 extends AbstractEntity{
private Set attachments;
}
....
Class Attachment{
private id;
private AbstractEntity parent;
private blob;
}

What would be the best mapping method? (I'm reluctant to move attachment property into AbstractEntity and handle only one mapping from there, because I'll end up with a huge table mapped by many entities - definitely a performance hit).

Thanks,
Dan


Top
 Profile  
 
 Post subject: Re: Question on multi-tier relationship mapping
PostPosted: Tue Mar 09, 2004 3:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
danz8086 wrote:
there, because I'll end up with a huge table mapped by many entities - definitely a performance hit).

What do you mean by that ? Setting it to AbstractEntity won't change anything.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 3:23 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Why not this?

Code:
abstract Class AbstractEntity{
private id;
protected Set attachments;
...
}

Class Entity1 extends AbstractEntity{
...
}

Class Entity2 extends AbstractEntity{
...
}
....
Class Attachment{
private id;
private AbstractEntity parent;
private blob;
}


Mapped with a one-to-many lazy set from AbstractEntity to Attachment, and a many-to-one from Attachment to AbstractEntity. The lazy set will prevent the blobs from being loaded unless you actually need them.

Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 8:43 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 20
Hi Emmanuel & Rob,

Thanks for responding. Let me answer Emmanuel's question first - what I actually meant was that I'd like to map the entities separately using "table-per-concrete-class".

Sorry for the vagueness.

There are two reasons (I thought) I have to do this:
1) There is a requirement of having sequential numbers for each entity;
2) Although all concret Entities extend AbstractEntity, I regarded it as pure java code reusing (attributes for workflow flags etc.). I did not want to mix non-business-related data into a common table hierarchy;

Please correct me if I got it wrong.

Now, with this in mind, all my Entities were mapped to independant tables, having their own ID sequence. But they all associate to an "Attachment" collection. I'd like to achieve something like "select attachment from attachments where parentId=?, parentType=?" when Hibernate loads an Entity:

<class name="EntityOne">
...
<set name="attachments" batch-size="1" cascade="delete-orphan" inverse="true">
<key column="parentId"></key>
<one-to-many class="Attachment1"/>
</set>
</class>

<class name="EntityTwo">
...
<set name="attachments" batch-size="1" cascade="delete-orphan" inverse="true">
<key column="parentId"></key>
<one-to-many class="Attachment2"/>
</set>
</class>

Please note the different classes for "one-to-many" associations above. I thought I can use the following mapping for Attachment to differentiate attachments for different entities:

<class name="AttachmentSummary" table="ATTACHMENTS" >

<discriminator column="parentType" type="string" length="50"/>

<property name="parentId" not-null="true" type="long"/>

<subclass lazy="true" name="Attachment" discriminator-value="a">
<property name="blob" type="blob"/>

<!-- wanted to use this to separate attachments of various entities -->
<subclass name="Attachment1" discriminator-value="a1"/>
<subclass name="Attachment2" discriminator-value="a2"/>

</class>

But this did not work for loading entities: Hibernate actually generates SQL like this:
Hibernate: select attachment0_.unid ..... from seatest.ATTACHMENTS attachment0_ where attachment0_.parentId=?

I have read the document but have been confused on whether I should include a "many-to-one" association on the attachment side, like Rob mentioned, and how would it be applied in my situation: do I need multiple many-to-ones like this?
<subclass name="Attachment>
<many-to-one class="Entity1"/>
<many-to-one class="Entity2"/>
</subclass>

Sorry for the long reading. I'm really new in this O/R mapping idea and have been so excited about the potentials of Hibernate.

regards,
Dan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 11:38 am 
Beginner
Beginner

Joined: Tue Mar 02, 2004 6:17 pm
Posts: 20
OK. Although I can see that there are many "read"s to my question, I guess it is too dumb to deserve an effort to answer it ^_^

Anyway, I'd like to share the way I'll have to use. Any comment is welcome (I'm not so good in O/R mapping concepts, so my way of modeling the relations may not be good enough):

For each entity, a constant "entityType" attribute must be defined and used in the "where" attribute in the mapping for set "attachments". So that attachments can be filtered for a specific entity.

Also, the "entityType" must be hardcoded in the mapping due to be problem described in this thread: http://forum.hibernate.org/viewtopic.php?t=928876&highlight=

Code:
(entity mapping)

<property name="entityType" type="string" ></property>
<map name="attachments" batch-size="1" cascade="delete-orphan"
   inverse="true" where="parentType='this_entity_type_value'">

   <key column="parentId"></key>
   <index column="fileName" type="string" length="50"></index>
   <one-to-many class="AttachmentInfo" /><!-- a light version that doesn't include the blob -->

</map>

(and attachment mapping)

<class name="AttachmentInfo" table="ATTACHMENTS">
  <!-- properties mapping for super class -->
  <id name="unid" column="unid" type="long" >
    <generator class="sequence">
      <param name="sequence">SEQ_ATTACHMENT</param>
    </generator>
  </id>
 
  <version name="version" column="version" type="integer"
    unsaved-value="undefined"></version>

  <property name="fileName" length="50" />
  <property name="contentType" length="50" />
  <property name="parentId" type="long"></property>
  <property name="parentType" type="string" ></property>
</class>
   
<!-- full version of attachments for download and insert -->
<class name="Attachment" table="HB_ATTACHMENTS">
  <!-- properties mapping for super class -->
  <id name="unid" column="unid" type="long" length="10">
    <generator class="sequence">
      <param name="sequence">SEQ_ATTACHMENT</param>
    </generator>
  </id>
 
  <version name="version" column="version" type="integer"
    unsaved-value="undefined"></version>

  <property name="fileName" length="50" />
  <property name="contentType" length="50" />
  <property name="parentId" type="long"></property>
  <property name="parentType" type="string" length="50"></property>
  <property name="blob" type="blob" column="BLOB"></property>
</class>


The attachment is mapped in two different classes because I'd like to keep a non-lazy collection of summary of attachments with the entity. Attachment class is actually a subclass of AttachmentInfo.

regards,
Dan


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