Saving new 1:n:n:1 related entities

Posts   
 
    
Alvaro
User
Posts: 52
Joined: 01-Jun-2004
# Posted on: 30-Sep-2004 15:51:20   

Hi,

I have a complex entity I want to save with one recursive adapter.SaveEntity() call. The entity has a lot of relationships. The one that's giving me one of those puzzled looks is the one in the subject, that is: A is my main entity A has a lot of B's B has a lot of C's each C has a 1:1 relationship with D (C and D have the same PK fields and D has a FK to C)

I created all the entities properly using their relationships to relate them together but when I save A, I get kicked by the FK from D to C.

I tried saving A without D, and saving D later, and it works.

Now I think I could either: 1) Make two adapter.SaveEntity() calls, which annoys me because I think I should be able to do it with one 2) Change the relational design (after all it's not the best thing in the world to have two tables with the same PK), but there is a lot of code and SQL i would have to change so this is probably my last choice 3) Have someone tell me which stupid mistake I made so I can do it right flushed 4) Have Frans tell me it's some sort of weird bug and fix it frowning

Thanks in advance for your time, Alvaro.-

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-Sep-2004 16:50:54   

What is the runtime library version you're using, and the template set version you're using? you can grab the runtime library version in code by using something like:

string version = SD.LLBLGen.Pro.ORMSupportClasses.RuntimeLibraryVersion.Version; string build = SD.LLBLGen.Pro.ORMSupportClasses.RuntimeLibraryVersion.Build;

The template version is mentioned in the window when you press F7.

On May 26, adapter's code was updated with some new code to prevent some code to fail in recursive saves. I'm not sure if you're using an older version.

If possible, could you please paste the code in where you relate C to B and D to C?

Frans Bouma | Lead developer LLBLGen Pro
Alvaro
User
Posts: 52
Joined: 01-Jun-2004
# Posted on: 30-Sep-2004 18:57:15   

Well turns out I am indeed using an older version. Of course, I probably should've checked this and posted this information in the first place.

Version number is 1.0.2003.3. Build is 04302004.

The code you requested looks like this:


BEntity bEntity = new BEntity();
bEntity.Information = "lala";
bEntity.IsNew = true;
bEntity.A = aEntity;
aEntity.B.Add(bEntity);

CEntity cEntity = new CEntity();
cEntity.SomeData = 24;
cEntity.IsNew = true;
cEntity.B = bEntity;
bEntity.C.Add(cEntity);

DEntity dEntity = new DEntity();
dEntity.SomeData = 1;
dEntity.MoreData = 2;
dEntity.IsNew = true;
dEntity.C = cEntity;
cEntity.D = dEntity;

I know the IsNew property isn't really necessary but it's how it looks now (I was trying different things).

As a side note, I thought what the hell let's try the new september 30 runtime libraries instead of just replacing my templates with the may 26 ones you mentioned, but alas the code generated with the september 30 version (I just downloaded it a short while ago) doesn't compile confused . It looks like it got confused generating some relations code:


/// <summary>
/// Returns a new IEntityRelation object, between ABranchIssuerEntity and ABranchIssuerBEEntity over the 1:1 relation they have,
/// using the relation between the fields:
/// ABranchIssuer.IdAIssuer - ABranchIssuerBE.IdAIssuer
/// ABranchIssuer.IdABranchIssuer - ABranchIssuerBE.IdABranchIssuer
/// </summary>
public IEntityRelation ABranchIssuerBEEntityUsing   {
   get {IEntityRelation relation = new EntityRelation(RelationType.OneToOne);
      relation.StartEntityIsPkSide = ;
      return relation;
   }
}

It's easy to see why that won't compile, but I wonder why it generated that? those tables have a very innocent looking FK relationship between them based on two int fields.

I'd love your insight on this too!

thanks, álvaro.-

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-Sep-2004 20:28:29   

Hmm. My buildtests run fine with all types of 1:1 relations, so it must be something with the relation object in the project file.

Please, could you refresh the catalog and try again? if that doesn't help, could you please mail me the .lgp file? (support@llblgen.com) so I can check it with the debugger.


BEntity bEntity = new BEntity();
bEntity.Information = "lala";
bEntity.IsNew = true;
bEntity.A = aEntity;
aEntity.B.Add(bEntity);

bEntity.A = aEntity already adds bEntity to aEntity.B, you don't have to add it again. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
leoduran
User
Posts: 35
Joined: 25-Jun-2004
# Posted on: 14-Feb-2005 18:06:37   

We are having the issue mentioned at the top of this post as well. The relationship looks like this...

A is our main entity. A has a lot of B's. B has a 1:1 relationship with C B and C have the same PK fields and C has a FK to B.

We believe that 1:1 relationships are not handled effectively if they are at the bottom of the tree (B and C).

We checked the output with SQL Profiler and found that the level A entity is skipped during the recursive save.

What it should do is ... 1. Insert A 2. After it has the PK value for A 3. It should Insert B. 4. Get the PK value for B, and 5. Insert C.

It skips A and attempts to insert B, but fails because of the FK constraint on A. However, in the absence of the above mentioned 1:1 relationship between B and C, it seems to perform fine.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Feb-2005 18:37:14   

Is there any field changed to A? If not, is A new?

Frans Bouma | Lead developer LLBLGen Pro
leoduran
User
Posts: 35
Joined: 25-Jun-2004
# Posted on: 14-Feb-2005 18:47:56   

Otis wrote:

Is there any field changed to A? If not, is A new?

A is new.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 14-Feb-2005 19:17:00   

leoduran wrote:

Otis wrote:

Is there any field changed to A? If not, is A new?

A is new.

Ok, that explains why the FK isn't synced if A isn't saved. A has to have a field which is changed, otherwise A isn't saved.

Frans Bouma | Lead developer LLBLGen Pro