RE: Single table inheritance

From: Gentry, Michael (michael_gentr..anniemae.com)
Date: Wed Oct 13 2004 - 12:33:38 EDT

  • Next message: Andrus Adamchik: "RE: Single table inheritance"

    My usual disclaimer: I haven't done this yet. :-)

    You say you created *three* tables? Single-table inheritance will only work within one table. It will basically give you different views and classes into a single table. This is why when you set the "Inheritance" pulldown, it switches the "Table/View" pulldown automatically for you (it locks you into that table).

    You'll also notice a "Qualifier" text field under the ObjEntity Configuration. This is where you enter a Cayenne qualifier for that entity, which is how Cayenne determines which class to instantiate.

    For example, with one table (BaseTable), but two entities mapped into it:

    BaseTable DbEntity contains:

    primaryKey (integer, pk, mandatory)
    flag (integer, mandatory)
    value1 (integer)
    value2 (integer)

    BaseTable ObjEntity should be synched up with DbEntity.

    Create an Entity1 ObjEntity. Inherit from BaseTable. For qualifier, make it "flag = 1". Under attributes, delete "value2" row.

    Create an Entity2 ObjEntity. Inherit from BaseTable. For qualifier, make it "flag = 2". Under attributes, delete "value1" row.

    You now have 3 entities, but only 1 table. When you do queries from the database, if the flag is equal to 1, it'll create an instance of Entity1 for you, even though BaseTable and Entity2 share the same tablespace. You can filter which attributes show up in each entity and, maybe more importantly, have separate business logic/derived values for each. Of course, if you actually need BaseTable entities, set it's qualifier to "flag = 0". Be sure to initialize your flags correctly, too ...

    Hope that helps.

    /dev/mrg

    -----Original Message-----
    From: MikaŽl Cluseau [mailto:nwr..wrk.dyndns.org]
    Sent: Wednesday, October 13, 2004 11:44 AM
    To: cayenne-use..bjectstyle.org
    Subject: Single table inheritance

    Heelo people,

    I see that Cayenne support single table inheritance but I realy don't
    understand how it work (CAY-24 feature request).

    I created a simple test with 3 tables under PostgreSQL :
    * Header, with a column "id" (serial, primary key) ;
    * Table1, inherits Header and adds a column "name" ;
    * Table1, inherits Header and adds a column "price" ;

    Then, I reeginered the schema using the modeler and generated the
    classes. No inheritance between Header, Table1 or Table2. When I try to
    set the "Inheritance" ObjEntity Configuration of Table1 it overides
    "Table/view" to Header.

    DerivedEntities don't seems to be done for that, too, and some googleing
    didn't shown me anything useful.

    Can someone help me please ?

    Thanks,
    Mikael.



    This archive was generated by hypermail 2.0.0 : Wed Oct 13 2004 - 12:33:45 EDT