Retrieving data though relation table

Posts   
 
    
Posts: 4
Joined: 28-Apr-2009
# Posted on: 28-Apr-2009 10:19:07   

Hello,

We are using LLBLGen Pro version 2.6.09.0313 & Oracle 10g I would like to introduce a simple data structure, And I will be happy to know how to retrieve the data using LLBLGen classes and methods. Your quick response will be highly appreciated !

The data structure (ERD) contain 3 simple tables :

Table Name : DM_DOCUMENT Fields : DOCUMENT_ID (PK, NUMBER), DESCRIPTION (NVARCHAR2)

Table Name : FILES Fields : FILE_ID (PK, NUMBER), DESCRIPTION (NVARCHAR2)

Table Name : DM_DOCUMENT_FILE Fields : DOCUMENT_ID (NUMBER, FK TO DM_DOCUMENT), FILE_ID (PK, FK TO FILES, NUMBER)

Generally speaking, we have documents and files, with 1:M relation, Each document can contain more than one file, and specific file can be associated with one document only, For Example, the relation table (DM_DOCUMENT_FILE) can contain :

DOCUMENT_ID, FILE_ID 1 1 1 2 1 3 2 4

Which means that document 1 have three files (1,2,3), and document 2 have one file (4). With a given document id (1 for example), how can I get all it's related files ? (1,2,3), through the relation table ?

Thank you !

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 28-Apr-2009 21:04:31   

As ever, the answer is "It depends" - depending on how you want the data and what you want to do with it...

There are two main ways of retrieving the data - either as an entity graph (on or more entities, each containing related entites), or as a typed list (which is really a flat data table with some nice feature wrapped round it to give you named access to you fields)

Are you using Adapter or Self servicing - let us know and we can give you a more specific example of how to retrieve the data you want. There are also lots of examples of both of these methods in the documentation.

Thanks

Matt

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 29-Apr-2009 00:52:44   

Hi Itzik,

If you're using Self-Servicing, you can simply do:

Dim myDocument as New DocumentEntity(Your Primary Key Here)
Dim myFiles as FilesCollection = myDocument.DocumentFiles.Files

or

Dim myDocument as New DocumentEntity(Your Primary Key Here)
Dim myFiles as FilesCollection = myDocument.GetMultiFilesCollectionViaDocumentFiles

If you are using Adapter, then you will need to Prefetch both the DocumentFiles and Files relations when fetching Document.

Hope this helps!

Ryan

PS. 1.) I'm assuming you've renamed "DM_DOCUMENT", etc. to nice Entity names like "Document" in the LLBLGen Pro Designer.

PS. 2.) If it's truly a 1:M relationship (not M:M), couldn't the File table simply point to a DocumentID?

Posts: 4
Joined: 28-Apr-2009
# Posted on: 29-Apr-2009 02:05:52   

Hi guys,

We are using an Adapter, and Prefetch mechanism, I would like to get as a result the document (DM_DOCUMENT) entity, With EntitiyCollection of Files within.

The problem is that we don't have prefetch from the document up to the files. We have prefetch from DM_DOCUMENT to DOCUMENT_FILE, But without the prefetch from DOCUMENT_FILE to FILES.

It's truly 1:M, but we can't change the data structure.

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 29-Apr-2009 11:03:35   

You should have the following properties generated for you.

Document.FilesCollectionViaDocumentFiles
Document.PrefetchpathFiles......
Document.Relations.Files.....

So you can use a prefetchPath to directly load the files collection.

Unless the relation, or the field mapped on this relation was deliberately hidden from the designer. If so then please un-hide/expose it.

Posts: 4
Joined: 28-Apr-2009
# Posted on: 30-Apr-2009 07:08:14   

Walaa wrote:

You should have the following properties generated for you.

Document.FilesCollectionViaDocumentFiles
Document.PrefetchpathFiles......
Document.Relations.Files.....

So you can use a prefetchPath to directly load the files collection.

Unless the relation, or the field mapped on this relation was deliberately hidden from the designer. If so then please un-hide/expose it.

That's the problem, we don't have these properties. It's looks like we can't move on from Document to Files.

DmDocument entity has no direct access to Files with any way, DmDocument entity has EntityCollection<DmDocumentFileEntity> (the relation table), DmDocumentFileEntity entity has no access to Files.

The strange thing (which may help to figure out what's going on) is that DmDocumentFileEntity inherits from FileEntity instead of CommonEntityBase...

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Apr-2009 11:04:10   

The strange thing (which may help to figure out what's going on) is that DmDocumentFileEntity inherits from FileEntity instead of CommonEntityBase...

Oh...that's why simple_smile

It appears that someone has relaized that m:n is not the right design here, so he specified a 1:1 relation between DocumentFile and File, and created an inheritance hierarchy.

And thus from Document you only need to fetch DocumentFile and all File properties will be included oin the DocumentFile entity through inheritance.

SO what actually happens when you fetch a DocumentFile, is that the framework automatically fetches the paret File entity and populate the FileEntity (base class) of the DocumentFile being fetched.

Posts: 4
Joined: 28-Apr-2009
# Posted on: 30-Apr-2009 11:07:10   

Walaa wrote:

The strange thing (which may help to figure out what's going on) is that DmDocumentFileEntity inherits from FileEntity instead of CommonEntityBase...

Oh...that's why simple_smile

It appears that someone has relaized that m:n is not the right design here, so he specified a 1:1 relation between DocumentFile and File, and created an inheritance hierarchy.

And thus from Document you only need to fetch DocumentFile and all File properties will be included oin the DocumentFile entity through inheritance.

SO what actually happens when you fetch a DocumentFile, is that the framework automatically fetches the paret File entity and populate the FileEntity (base class) of the DocumentFile being fetched.

Make sense, I will check it out.

Thank you !