Flat inheritance: how to implement

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

    Posted 09 Mar 2009 Link to this post

    Hi,
    I've read the documentation and the forum threads on this topic - and still I don't know how to achieve my goal. This goal is as following (the example is not a real one - just to illustrate):
    • there is a table named AnyValue with columns:
      • id (int)
      • type (char(1))
      • intval (int)
      • textval (nvarchar(256))
    • this table gathers values of different types; "type" column says, what is the type of the value (e.g. "N" for numerics, "T" for texts)
    • numeric values are stored in "intval" column, text values - in "textval" column

    I want this table to be represented by three classes:

    • abstract class: AnyValue (with "id" and "type" fields only)
    • class IntValue: inheriting from Value and adding "intval" field
    • class TextValue: inheriting from Value and adding "textval" field

    When I ask: "select * from AnyValueExtent", I should get a mixed list of IntValue and TextValue objects (but the query should be of the AnyValue type: scope.GetOqlQuery<AnyValue>(...)). And - important - if I create a new object of TextValue class and save it, it should automatically set "T" to the "type" column.
    Is it possible to get such a functionality?
    If "yes" - how to design and configure it? And - will you support it in your wizards?

    Regards
    Tomasz

  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 11 Mar 2009 Link to this post

    Hello tmlipinski,

    Yes, this is a possible scenario. The only difference is that the type (discriminator) column is automatically generated and named 'voa_class'. The hierarchy mapping strategy has to be set in the Forward mapping wizard for each child class. In the same wizard the unique values that reside in the voa_class column can be specified as well. The option is called Class-id and can be a predefined value from the combobox or any other user-defined text. In your case you can set the IntValue class-id to 'N' and the TextValue class-id to 'T'.

    All the best,
    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 11 Mar 2009 Link to this post

    Hi,

    I'm sorry but it' still not a complete guideline.

    My table is named Setting and has following columns:
    SET_id, int: record's id; autoincrement
    SET_key, nvarchar(64): record's name
    SET_category, nchar(1): class discriminator: 'T' for texts (SET_value is the value), 'O' for options (SET_option is the value)
    SET_value, nvarchar(256): value for texts
    SET_option, nvarchar(64): value for options

    The classes:
      [Telerik.OpenAccess.Persistent(IdentityField = "sETId")]  
      public abstract class Setting  
      {  
        private int sETId; // pk   
        private char sETCategory;  
        private string sETKey;  
     
        public Setting()  
        {}  
     
        [Telerik.OpenAccess.FieldAlias("sETId")]  
        public int SETId  
        {get... set...}  
     
        [Telerik.OpenAccess.FieldAlias("sETCategory")]  
        public char SETCategory  
        {get... set...}  
     
        [Telerik.OpenAccess.FieldAlias("sETKey")]  
        public string SETKey  
        {get... set...}  
      }  
     
      [Telerik.OpenAccess.Persistent()]  
      public class SettingText : Setting  
      {  
        private string sETValue;  
     
        [Telerik.OpenAccess.FieldAlias("sETValue")]  
        public string SETValue  
        {get... set...}  
      }  
     
      [Telerik.OpenAccess.Persistent()]  
      public class SettingOption : Setting  
      {  
        private string sETOption;  
     
        [Telerik.OpenAccess.FieldAlias("sETOption")]  
        public string SETOption  
        {get... set...}  
      } 

    The config file:
    <class name="Setting">  
      <extension key="db-do-not-create-table" value="false" /> 
      <extension key="db-table-name" value="Setting" /> 
      <extension key="db-class-id" value="{no}" /> 
      <extension key="db-key-generator" value="AUTOINC" /> 
      <field name="sETId">  
        <extension key="db-column">...</extension> 
      </field> 
      <field name="sETKey">  
        <extension key="db-column">...</extension> 
      </field> 
      <field name="sETCategory">  
        <extension key="db-column">...</extension> 
      </field> 
    </class> 
    <class name="SettingText">  
      <extension key="db-table-name" value="Setting" /> 
      <extension key="db-class-id" value="T" /> 
      <extension key="db-inheritance" value="flat" /> 
      <field name="sETValue">  
        <extension key="db-column">  
        <extension key="db-column">...</extension> 
        </extension> 
      </field> 
    </class> 
    <class name="SettingOption">  
      <extension key="db-table-name" value="Setting" /> 
      <extension key="db-class-id" value="O" /> 
      <extension key="db-inheritance" value="flat" /> 
      <field name="sETOption">  
        <extension key="db-column">...</extension> 
      </field> 
    </class> 
     
    When trying to access the database is that:
    Class EX.Common.DAL.SettingOption must use vertical inheritance as
    it has siblings and the base class does not have a descriminator (jdo_class) column

    1. jdo_class or voa_class?
    2. I've tried a lot of combinations:
    - database column name changed to voa_class / jdo_class
    - removing this column from both - config and class definition
    Nothing helped. How this discriminator column should be declared?

    On the other hand. It's a pity that there is no single source of definition of both: database and objects. They are partially defined in C# code - by persistent classes. But they are partially defined in app.config also - for example: the model of inheritance; and physical definition of columns, of course (length and scale). It would be wonderful to model the database and its objects in a single XML file and one shot: create the database; second shot: create classes....
    And yet another thing: you have decided to declare attributes (in app.config) using <extension key=... value=.../> template. The major consequence that there is (and can't be) no prompt for "value". If you declared them as attributes (e.g. <class name="xx" inheritance="vertical" .... > instead of <class name="xx"> <extension key="inheritance" value="vertical" /> </class>) yould could define prompt in every case.

    Uff. I'm sorry for so long reply / question / considerations :-)

    Regards
    Tomasz
  4. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 13 Mar 2009 Link to this post

    Hi,
    I've succeeded implementing flat hierarchy, at last. The key issue was that in your solution the base class is NOT an abstract class and has to have declared the discriminator column value. That's not good, because depending on the case, the base class is abstract or not. If it is abstract (as in my case) declaring discriminator's value is unnatural.

    The rest remains to be answered:
    - why the error message says about "jdo_class" column?
    - why the discriminator column must be called "voa_class" (instead its name to be the base class' atrribute)?

    - and all that "On the other hand..." section of my previous post

     

    Regards

    Tomasz

  5. Ady
    Admin
    Ady avatar
    589 posts

    Posted 16 Mar 2009 Link to this post

    Hi Thomas,

    1)  The error message has been fixed and no longer refers to 'jdo_class' . The 'jdo' comes from the earlier java code base.  In your mapping for the base class you have specified '{no}'. This disables the discriminator column for the table. Hence the message suggests that you should turn that on or use vertical mapping for the derived classes.

    2) The schema file - OpenAccess.xsd present under [folder]\Telerik\OpenAccess ORM\sdk\IDEIntegrations\Microsoft Visual Studio 2005\schema, should provide intellisense in case you need to manually edit the config file. There was a problem in installing this file in version 2008.3.1421 but this has been fixed. You can update to the latest version or manually copy this file.

    All the best,
    Ady
    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.
Back to Top