Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Teneo] Partially resolve containment references(Is there a way to resolve containment references partially only?)
[Teneo] Partially resolve containment references [message #1620472] Tue, 17 February 2015 07:58 Go to next message
Timo Rohrberg is currently offline Timo RohrbergFriend
Messages: 69
Registered: September 2011
Location: Karlsruhe
Member
Hello everybody,

I have the following situation here: My Ecore model contains two classes and a containment reference between them (e.g. Measurement and MeasurementValue). I would now like to resolve only a specific part of the containment reference when loading Measurement entities from the DB. I would for example load a Measurement entity and get all contained MeasurementValue entities which fulfill a certain query (e.g. "newer than 01.01.2015").

Do you have any ideas how to achieve something like that?

Best regards
Timo
Re: [Teneo] Partially resolve containment references [message #1620607 is a reply to message #1620472] Tue, 17 February 2015 09:50 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Timo,
The default behavior is to load all the contained objects when you 'touch' the collection. So as a default it is lazily
loaded.

But before the collection is loaded you can also load a subset explicitly. But not within the owner object, just as a
separate collection. See here:
http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/#objectstate-filtering

gr. Martin

On 17-02-15 08:58, Timo Rohrberg wrote:
> Hello everybody,
>
> I have the following situation here: My Ecore model contains two classes and a containment reference between them (e.g.
> Measurement and MeasurementValue). I would now like to resolve only a specific part of the containment reference when
> loading Measurement entities from the DB. I would for example load a Measurement entity and get all contained
> MeasurementValue entities which fulfill a certain query (e.g. "newer than 01.01.2015").
>
> Do you have any ideas how to achieve something like that?
>
> Best regards
> Timo


--

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] Partially resolve containment references [message #1620629 is a reply to message #1620607] Tue, 17 February 2015 10:15 Go to previous messageGo to next message
Timo Rohrberg is currently offline Timo RohrbergFriend
Messages: 69
Registered: September 2011
Location: Karlsruhe
Member
Hi Martin,

thanks for your quick reply.

OK, so the default behavior is lazy-loading of all contained objects. But what do you mean with "load a subset explicitly" and "not within the owner object"? The referred documentation just explains how to load persisted collections at the first level. But can I restrict the collection of contained objects?

I would like to perform a query like this: "From all patients sorted alphabetically ascending for lastName give me the first 10 and for each of those the 10 most recent measurements" assuming that patient has a containment reference to measurement.

Do you think there is any way to achieve something like this?

Cheers
Timo
Re: [Teneo] Partially resolve containment references [message #1620642 is a reply to message #1620629] Tue, 17 February 2015 10:24 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Timo,
Hmm, the reference explains how to query/filter the contents of an association, so the second level, after loading the
parent/first level.

So you can create a query to read the first 10 patients sorted by last name and then for each patient load the
measurements through a collection filtered call (see the doc link of the previous post)

For the query you describe below, you can also use HQL to load this data, so you don't need to first load the patients,
you can create a query (hql) which returns tuples of patients - measurements and then sort by lastname, id. HQL has many
nice collection functions.

gr. Martin

On 17-02-15 11:15, Timo Rohrberg wrote:
> Hi Martin,
>
> thanks for your quick reply.
>
> OK, so the default behavior is lazy-loading of all contained objects. But what do you mean with "load a subset
> explicitly" and "not within the owner object"? The referred documentation just explains how to load persisted
> collections at the first level. But can I restrict the collection of contained objects?
>
> I would like to perform a query like this: "From all patients sorted alphabetically ascending for lastName give me the
> first 10 and for each of those the 10 most recent measurements" assuming that patient has a containment reference to
> measurement.
>
> Do you think there is any way to achieve something like this?
>
> Cheers
> Timo


--

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] Partially resolve containment references [message #1620700 is a reply to message #1620642] Tue, 17 February 2015 11:12 Go to previous messageGo to next message
Timo Rohrberg is currently offline Timo RohrbergFriend
Messages: 69
Registered: September 2011
Location: Karlsruhe
Member
Hi Martin,

thanks again for your reply. I am trying to understand your explanation and would like to make an example in more or less pseudo code.

So for your first approach, I would load the first 10 patients sorted by last name and refill the collection of measurements of each patient using the filtered collections:

Criteria criteria = session.createCriteria(PatientPackage.eINSTANCE.getPatient());
criteria.addOrder(Order.asc("lastName"));
criteria.setMaxResults(10);
Collection<Patient> patients = criteria.list();

for (Patient patient : patients) {
  Collection first10Measurements= session.createFilter(patient.getMeasurements(), "").setFirstResult(0).setMaxResults(10).list();
patient.getMeasurements().removeAll();
patient.getMeasurements().addAll(first10Measurements);
}


Isn't that a little bit weired? Once I touch "patient.getMeasurements()" the collection is already lazy loaded with all consequences on the performance?

And for your second approach, I unfortunately do not get how to perform such a query for tuples, and still get the result as instances of modeled entities. A tuple of Patient/Measurement never matches my modeled entities Patient and Measurement, right?

Thanks a lot for so much input you're providing. I have the feeling that we are close to a solution - I just don't get it completely yet...

Regards
Timo
Re: [Teneo] Partially resolve containment references [message #1620721 is a reply to message #1620700] Tue, 17 February 2015 11:30 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Timo,
Doing patient.getMeasurements() does not load the collection yet, only if you call a method on the collection it starts
loading (if needed to do the call), for example, this loads the collection: patient.getMeasurements().get(0)

The removeAll and addAll on patient.getMeasurements will remove/add these objects in the database. So not sure if you
want to do tis.

For the hql to load the measurements, it is something like this:

select p, m from Patient p, Measurement m where m in p.measurements order by p.lastName, p.id, p.date

then within the where clause you can filter on other measurements properties. The result set of the query will be a list
of arrays where each array has 2 entries: a patient and a measurement entity. One patient can occur multiple times if a
patient as multiple measurements.
It is probably difficult to limit the query to the latest 10 measurements though. But you can do inner selects and other
complexer stuff.

My impression looking at your pseudo code is that you want to create have Patient objects which have a measurements
collections which is filtered/sorted. This is however not possible, the measurements collection in the Patient will
always be the complete list of measurements, you can create a filtered separate collection instance but this is a
separate instance from the collection in Patient.

Not sure if I explain it well enough :-)

gr. Martin

On 17-02-15 12:12, Timo Rohrberg wrote:
> Hi Martin,
>
> thanks again for your reply. I am trying to understand your explanation and would like to make an example in more or
> less pseudo code.
>
> So for your first approach, I would load the first 10 patients sorted by last name and refill the collection of
> measurements of each patient using the filtered collections:
>
>
> Criteria criteria = session.createCriteria(PatientPackage.eINSTANCE.getPatient());
> criteria.addOrder(Order.asc("lastName"));
> criteria.setMaxResults(10);
> Collection<Patient> patients = criteria.list();
>
> for (Patient patient : patients) {
> Collection first10Measurements= session.createFilter(patient.getMeasurements(),
> "").setFirstResult(0).setMaxResults(10).list();
> patient.getMeasurements().removeAll();
> patient.getMeasurements().addAll(first10Measurements);
> }
>
>
> Isn't that a little bit weired? Once I touch "patient.getMeasurements()" the collection is already lazy loaded with all
> consequences on the performance?
>
> And for your second approach, I unfortunately do not get how to perform such a query for tuples, and still get the
> result as instances of modeled entities. A tuple of Patient/Measurement never matches my modeled entities Patient and
> Measurement, right?
>
> Thanks a lot for so much input you're providing. I have the feeling that we are close to a solution - I just don't get
> it completely yet...
>
> Regards
> Timo


--

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.Edit] IItemPropertyDescriptor EReference
Next Topic:[TEXO] Mapped superclass in a containment relation
Goto Forum:
  


Current Time: Wed Apr 08 19:13:43 GMT 2020

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

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

Back to the top