One-to-(zero or one) relationship issues

Thread is closed for posting
3 posts, 0 answers
  1. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 03 Jun 2009 Link to this post

    Hi,

    There are two tables: TableA (main) and TableB (additional). Each TableA record has zero or one linked records in TableB.
    The TableA ORM object has tableB property:
    public class TableA
    {
    ....
    public TableB tableB .....
    }
    and I have some troubles while dealing with this property.

    1. I would like to be able to check whether tableB exists (is null) or not. I cannot just test:

    if(tableAobject.tableB == null)
    .......
    because if tableB is missing I get an error message:
    No row for ....TableB ....

    I must check the existence of this object independently, asking the DB directly for this object.

    2. Let's suppose I've read some TableA object - together with the existing TableB sub-object. After that and after doing some other work I decide that I must update some data in tableB. TableA object is read out of any transaction. If I try to do something like this:
    someOtherScope.Transaction.Begin();
    tableAobject.tableB.prop = newPropValue;
    someOtherScope.Transaction.Commit();
    I get an error in the second statement ("active transaction required for write operation"). I understand this - but it means that I must reread the whole TableA object (inside an active transaction). If I want to avoid reading it (this object may be pretty large) I must, again, read and then modify TableB object independently.

    These two issues mean that there is nearly no advantage of making theses two classes related. What is the best practice in maintaining such relationship?

    Best regard
    Tomasz
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 05 Jun 2009 Link to this post

    Hello tmlipinski,

    Regarding the first question, I am pretty sure this works. I have just tested it again. You should have a null value in the foreign key column of Table1 for each row that does not have a Table2 record related. Is that so in your case?
    About question 2, if you already loaded a Table1 object into a scope, all changes to this object should be made within this scope. Including changing properties of a related object. If you want to work with another scope, you will have to load the object again. Note that loading within an active transaction is not mandatory but it is a good practice. Beginning a transaction makes sure the cache contains the latest values, so you will be always working with the actual values.

    Greetings,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 07 Jun 2009 Link to this post

    Hi,

    I've thought my problem over and now I can see that my problem is incorrectly designed - in terms of ORM. The database structure says that there is the foreign key of TableA in TableB. This is the case of one-to-many relationship. I know that the list of TableB objects can be of 0 or 1 elements length but it doesn't change the fact that this is a list, not a single object.

    Thanks for all your comments.

    Regards
    Tomasz
Back to Top