How to save linked entities?

Posts   
 
    
TomV
User
Posts: 76
Joined: 31-Jan-2008
# Posted on: 16-May-2008 10:16:22   

Hi,

I have a question about saving entities to the database. I'm using LLBLGenPro 2.5. I have a translations table that has a sequence as PK and a Key field indicating the value we want to translate. This table is linked to a TranslationValues table containing a record for each language defined in the DB (Languages table). An example follows

Languages (PK - Description) 1 Nederlands 2 English 3 French

Translations (PK - Key) 1 date 2 name 3 afd_soft

TranslationValues (PK - TranslationID - LanguageID - Value) 1 1 1 datum 2 1 2 date 3 1 3 date 4 2 1 naam 5 2 2 name 6 2 3 nom 7 3 1 Software 8 3 2 Software 9 3 3 Logiciel

Now I also have tables that hold definitions of entities, for example departments. These don't have a description, but a link to a Translations record. At runtime, the value is taken from TranslationValues corresponding to the language of the logged on user. So far so good.

Departments (PK - Code - TranslationID) 1 SW 3

The problem is in my departments window where I have a grid and where I can add new departments. Let's say I want to add a software department. The user enters a code and a description. This description should link to a Translation record. There's a database trigger that is fired when a record is added/deleted in Translations. This trigger will create the corresponding records in TranslationValues. Which means that if 3 languages are available in the database, 3 records in TranslationValues are created.

Another important thing is that the user can "play"' around in the screen as long as he wants, nothing is saved untill he presses the save button. When the user saves his newly added department, a Translation record should be created, and this Id should be used in the department entity.

All thread were handling order - orderline examples, but this is a little different problem. Can somebody help me a hand on how to save these newly added entities.

I hope this is somewhat clear to you guys, else give a yell and I will try to explain things.

Kind regards, TomV

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 16-May-2008 11:22:16   

Please clarify the following:

1- When the user creates a new departement, does he enters the different languages descriptions?

2- What does the trigger do exactl?

3- Please give an example of the INSERT statements you want to execute when a user inserts a new departement.

TomV
User
Posts: 76
Joined: 31-Jan-2008
# Posted on: 16-May-2008 12:19:22   

Walaa wrote:

Please clarify the following:

1- When the user creates a new departement, does he enters the different languages descriptions?

2- What does the trigger do exactl?

3- Please give an example of the INSERT statements you want to execute when a user inserts a new departement.

Hi Walaa,

Thanks for reading and answering my post.

1) When the user creates a new department, he enters the code and the description for his language. (A linked form is available if he would like to provide the other languages as well, but this is not required)

2) When you add a record in the Translation table, the trigger will create X records in the translationValues table. (where X = number of languages defined in the database). When you delete a translation record, the trigger will first delete the linked translationvalue records.

3) INSERT INTO departments (Code, TranslationID) VALUES ('SW', ?) Where ? stands for the id of the linked translation record which also should been created INSERT INTO translations (TranslationKey, CreatedBy) VALUES ("DEP_SW", "TomV");

When my grid detects a new line, this what I have types in the eventhandler


         DepartmentEntity newDepartment = gvDepartments.GetRow(e.RowHandle) as DepartmentEntity;
         if (newDepartment != null)
         {
            TranslationEntity newTranslation = new TranslationEntity();
            newDepartment.Translation = newTranslation;
         }

While I'm typing this, indeed, I'm not yet able to fill these translationValues. I will hold these in a collection in memory. First save translations and departments. Let the trigger do his work and create the translationvalues. A refetch will provide me these records. i will loop over my in memory collection and fill the values in to the newly fetched objects. And do a save again. Maybe I try to wrap this everything in a transaction. This involves two save actions, but this kind of behavior is only used when initializing our application...

Hope I answered your questions good. anyway, thanks in advance!

Kind regards TomV

TomV
User
Posts: 76
Joined: 31-Jan-2008
# Posted on: 16-May-2008 12:23:07   

Hi,

I just wanted to add another remark:

When I would have worked with order and orderentity, the unit of work would probably make sure that the order is saved before orderentity and use that @@identity value as foreign key value. Maybe the unit or work is saving departments before translations. Can I manipulate this behavior?

Kind regards TomV

TomV
User
Posts: 76
Joined: 31-Jan-2008
# Posted on: 16-May-2008 15:46:24   

I think I found the solution. I also filled in one other required field and now it gets saved. Strange. Anyway, thank you for your time and effort. For now I'm putting this thread on hold.

Thanks TomV