Re: Error Creating Objects with meaningful PKs

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Wed Oct 29 2003 - 10:50:20 EST

  • Next message: Andrus Adamchik: "Re: trouble with to-one relation"

    On Oct 29, 2003, at 8:17 AM, Axel HONFI wrote:
    > Hello!
    >
    > I have a table called webpage with a composite primary key consisting
    > of
    > two FKs and one String field.
    > These properties are recognized as primary keys, therefore there is no
    > object-attribute mapped to them.

    > page.setToWebContext(webcontext_object);
    > page.setToWebLanguage(weblanguage_object);

    I understand both of these relationships are based on WebPage PK-FK
    columns? You should verify that you have "To Dep PK" checkbox checked
    for both DB relationships *to* WebPage entity per

    http://objectstyle.org/cayenne/modelerguide/modeling-db-layer/to-dep-
    pk.html

    "To Dep PK" is the way to tell Cayenne that a primary key should be
    derived from a related object instead of using auto-generation
    mechanism.

    > How can I create objects with meaningful primary keys?

    In many cases you can simply create an ObjAttribute for the primary
    key, making it a class property. In your case though, when the key is
    derived from relationship, I suggest creating two convenience methods
    to read the value in WebPage.java (I am making up the names of the keys
    of course):

    public Integer getWebContextID() {
        return (Integer)getObjectId().getIdSnapshot().get("WEB_CONTEXT_ID");
    }

    public Integer getWebLanguageID() {
        return (Integer)getObjectId().getIdSnapshot().get("WEB_LANGUAGE_ID");
    }

    Hope this helps.

    Andrus



    This archive was generated by hypermail 2.0.0 : Wed Oct 29 2003 - 10:50:25 EST