Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Teneo] Possible to set fetch="subselect" on a multi-value EAttribute?
[Teneo] Possible to set fetch="subselect" on a multi-value EAttribute? [message #1015940] Mon, 04 March 2013 10:48 Go to next message
Ben Tenne is currently offline Ben TenneFriend
Messages: 49
Registered: October 2009
Member
Hi,

I've been experimenting with the potential performance benefits of setting @Fetch(value=FetchMode.SUBSELECT) @OneToMany(fetch=EAGER) on my multi-value EReferences (i.e. where upperBound!=1). As expected, this results in a single select query for this type, rather than one per object in the containing collection.

To give a concrete example: Order contains many OrderLines (has an EReference called orderLines with upperbound=-1). This (normal behaviour) results in a single query to fetch all OrderLines for a given Order. Then, OrderLine contains a single OrderItem (an EReference called orderItem, upperbound=1). These are fetched (normal behaviour) as part of the same query that fetched the OrderLines, via an inner join.

Next, I have OrderItem containing many ItemEvents (an EReference called itemEvents, upperBound=-1). Normally, this would result in one select query per OrderItem to fetch its associated ItemEvents. This is where my FetchMode.SUBSELECT comes into play; I've set this on OrderItem.itemEvents and this correctly causes all ItemEvents to be selected via single select query that has an 'in' clause referring to a sub-query that fetches all the OrderItems.

Represented in 'ASCII-UML', this looks like:

Order <>--> [0..*] OrderLine <>--> [1] OrderItem <>--> [0..*] ItemEvent

Everything so far is fine and as expected. However, if I go another layer deeper into my containment tree, ItemEvent has a multi-value EAttribute called 'metadata' of type String. These are stored in a table of their own, with a foreign key referring to the containing ItemEvent. This exhibits the 'N+1 problem', in that a separate select query is used to fetch the metadata for each ItemEvent. I'd like to solve this by setting FetchMode.SUBSELECT here too; i.e. cause a single select query on the metadata table (with an 'in' clause to get all entries for all ItemEvents).

Adding this to the hierarchy gives us:

Order <>--> [0..*] OrderLine <>--> [1] OrderItem <>--> [0..*] ItemEvent <>--> [0..*] Metadata

If I look at the hibernate mapping for OrderItem.itemEvents, it has a 'list' declared like this:

<list name="itemEvents" fetch="subselect" lazy="false" cascade="all,delete-orphan">
   ...
   <one-to-many entity-name="com.example.ordermodel.ItemEvent"/>
</list>


Looking at ItemEvent.metadata, it's something like this:

<list name="metadata" table="`ITEMEVENTMETADATA`" lazy="false" cascade="all,delete-orphan">
   <key foreign-key="FK_METADATA" update="true">
      <column name="`ITEMEVENT_METADATA_ID`" not-null="true" unique="false"/>
   </key>
   <list-index column="`ITEMEVENT_METADATA_IDX`"/>
   <element type="java.lang.String" not-null="false" unique="false">
      <column not-null="false" unique="false" name="`METADATA`" length="400"/>
   </element>
</list>


If I understand correctly, hibernate would allow fetch="subselect" to be declared on this list element too, causing all metadata to be retrieved with a single query. However, I'm not sure how to prompt Teneo to cause that. I don't think I can put @Fetch(value=FetchMode.SUBSELECT) there, as this fails with:

org.eclipse.emf.teneo.annotations.parser.AnnotationParserException: The eclass: HbAnnotatedEAttribute does not have an efeature for Fetch

So, my questions are:
1) Is my assumption correct that hibernate would allow fetch='subselect' here and that it would give the desired effect?
2) If so, can I prompt Teneo to cause this?

Many thanks in advance for any help.

Regards,
Ben.
Re: [Teneo] Possible to set fetch=&quot;subselect&quot; on a multi-value EAttribute? [message #1015982 is a reply to message #1015940] Mon, 04 March 2013 13:28 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5339
Registered: July 2009
Senior Member
Hi Ben,
Seems to be a missing feature in annotation processing. Can you enter a bugzilla for this? With this annotation enabled
it should be possible to add subselects fetching also for many-eattributes.

gr. Martin

On 03/04/2013 11:48 AM, Ben Tenne wrote:
> Hi,
>
> I've been experimenting with the potential performance benefits of setting @Fetch(value=FetchMode.SUBSELECT)
> @OneToMany(fetch=EAGER) on my multi-value EReferences (i.e. where upperBound!=1). As expected, this results in a single
> select query for this type, rather than one per object in the containing collection.
>
> To give a concrete example: Order contains many OrderLines (has an EReference called orderLines with upperbound=-1).
> This (normal behaviour) results in a single query to fetch all OrderLines for a given Order. Then, OrderLine contains a
> single OrderItem (an EReference called orderItem, upperbound=1). These are fetched (normal behaviour) as part of the
> same query that fetched the OrderLines, via an inner join.
>
> Next, I have OrderItem containing many ItemEvents (an EReference called itemEvents, upperBound=-1). Normally, this
> would result in one select query per OrderItem to fetch its associated ItemEvents. This is where my FetchMode.SUBSELECT
> comes into play; I've set this on OrderItem.itemEvents and this correctly causes all ItemEvents to be selected via
> single select query that has an 'in' clause referring to a sub-query that fetches all the OrderItems.
>
> Represented in 'ASCII-UML', this looks like:
>
> Order <>--> [0..*] OrderLine <>--> [1] OrderItem <>--> [0..*] ItemEvent
>
> Everything so far is fine and as expected. However, if I go another layer deeper into my containment tree, ItemEvent has
> a multi-value EAttribute called 'metadata' of type String. These are stored in a table of their own, with a foreign key
> referring to the containing ItemEvent. This exhibits the 'N+1 problem', in that a separate select query is used to
> fetch the metadata for each ItemEvent. I'd like to solve this by setting FetchMode.SUBSELECT here too; i.e. cause a
> single select query on the metadata table (with an 'in' clause to get all entries for all ItemEvents).
>
> Adding this to the hierarchy gives us:
>
> Order <>--> [0..*] OrderLine <>--> [1] OrderItem <>--> [0..*] ItemEvent <>--> [0..*] Metadata
>
> If I look at the hibernate mapping for OrderItem.itemEvents, it has a 'list' declared like this:
>
>
> <list name="itemEvents" fetch="subselect" lazy="false" cascade="all,delete-orphan">
> ...
> <one-to-many entity-name="com.example.ordermodel.ItemEvent"/>
> </list>
>
>
> Looking at ItemEvent.metadata, it's something like this:
>
>
> <list name="metadata" table="`ITEMEVENTMETADATA`" lazy="false" cascade="all,delete-orphan">
> <key foreign-key="FK_METADATA" update="true">
> <column name="`ITEMEVENT_METADATA_ID`" not-null="true" unique="false"/>
> </key>
> <list-index column="`ITEMEVENT_METADATA_IDX`"/>
> <element type="java.lang.String" not-null="false" unique="false">
> <column not-null="false" unique="false" name="`METADATA`" length="400"/>
> </element>
> </list>
>
>
> If I understand correctly, hibernate would allow fetch="subselect" to be declared on this list element too, causing all
> metadata to be retrieved with a single query. However, I'm not sure how to prompt Teneo to cause that. I don't think I
> can put @Fetch(value=FetchMode.SUBSELECT) there, as this fails with:
>
> org.eclipse.emf.teneo.annotations.parser.AnnotationParserException: The eclass: HbAnnotatedEAttribute does not have an
> efeature for Fetch
>
> So, my questions are:
> 1) Is my assumption correct that hibernate would allow fetch='subselect' here and that it would give the desired effect?
> 2) If so, can I prompt Teneo to cause this?
>
> Many thanks in advance for any help.
>
> Regards,
> Ben.


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Teneo] Possible to set fetch=&quot;subselect&quot; on a multi-value EAttribute? [message #1015987 is a reply to message #1015982] Mon, 04 March 2013 13:57 Go to previous messageGo to next message
Ben Tenne is currently offline Ben TenneFriend
Messages: 49
Registered: October 2009
Member
Thanks Martin.

Raised here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=402321

In the meantime, could you possibly point me in the direction of how I could go about hacking this capability in (temporarily, for experimental purposes)? In my case, it would be adequate to _always_ set fetch="subselect" for _all_ multiplicity-many EAttributes.

Regards,
Ben.
Re: [Teneo] Possible to set fetch=&amp;quot;subselect&amp;quot; on a multi-value EAttribute? [message #1016013 is a reply to message #1015987] Mon, 04 March 2013 15:29 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5339
Registered: July 2009
Senior Member
Hi Ben,
Here are some tips:
- create your own subclass of the ManyAttributeMapper, set it to be used using the teneo extensionmanager:
http://wiki.eclipse.org/Teneo/Hibernate/Extensions
- override the processManyAttributeMapper method, call it in the super class, after the super call check this object:
getHbmContext().getCurrent() (it returns a simple dom element representation)
- then from that object you can find the created list element, and add your own fetch attribute.

gr. Martin

On 03/04/2013 02:57 PM, Ben Tenne wrote:
> Thanks Martin.
>
> Raised here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=402321
>
> In the meantime, could you possibly point me in the direction of how I could go about hacking this capability in
> (temporarily, for experimental purposes)? In my case, it would be adequate to _always_ set fetch="subselect" for _all_
> multiplicity-many EAttributes.
>
> Regards,
> Ben.


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Previous Topic:[EMF] BinaryResourceImpl uses globale package registry, not the registry of its resource set
Next Topic:(digraph1, digraph2) import is not available
Goto Forum:
  


Current Time: Tue Nov 25 21:10:25 GMT 2014

Powered by FUDForum. Page generated in 0.02202 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software