Table inheritance (WAS Re: Single table inheritance)

From: Mikaël Cluseau (nwr..wrk.dyndns.org)
Date: Wed Oct 13 2004 - 15:04:05 EDT

  • Next message: Mikaël Cluseau: "Re: Single table inheritance"

    Ok, I understand I misunderstood what single table inheritance was. I'm
    new the terminology, and I took the concept has the opposite of multiple
    inheritance, forbidden for Java classes and then justyfing the fact that
    it was "single inheritance" only.

    In fact, I must specify a table in PostgreSQL to enforce a single
    identifier for each tuple in the database (or the schema) and to carry
    some common business logic (like locking). To acheive this, I must have
    the generated classes have the same inheritance relation as in the
    database (and I think this makes sense in an ORM, since both meta-models
    share these semantics). This means that generated classes must have the
    following hierarchy :

        _Header
           |
         Header
        / \
    _Table1 _Table2
       | |
     Table1 Table2

    I suppose that Cayenne support this case, but I can't figure out how to
    do this. Sorry for the newbie question.

    Single table inheritance show very interesting application anyway.

    Thank!
    Mikael.

    Gentry, Michael wrote:

    >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 - 15:04:09 EDT