RE: One to many relationships require reverse relationships?

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Apr 25 2003 - 12:32:46 EDT

  • Next message: Merritt, Scott: "PK Generator Synchronization Problem"

    > Also, please try looking into the one-to-one relationship.

    Well if you are talking about "PK to FK" relationship, this is to-many by
    design, this is just how the relational logic works (I believe this was
    discussed before on cayenne-devel, can't find it now though). Real
    "one-to-one" relationships are when the FK is also the complete PK of the
    child entity, and Cayenne does support that (those "to dep pk" columns in
    the modeler).

    So there is nothing really to fix.

    > I can't get
    > it working even with a "reverse" relationship. Once I click the
    > "to-many" on the child side it ends up working. I'd like to say, my
    > parent has one, and only one child object.

    See above. If you want to build real one-to-one, make child FK columns to
    be also the PK columns of the child (all of them, and no other columns).

    Now, if this is a "logical" to-one relationship, when it can be to-many in
    the database, but object model assumes it is always to-one, just create a
    wrapper methods for the List getters/setters:

    public SomeObject getSomeObject() {
      List objects = getSomeObjectArray();
      if(object.size() == 0) {
          return null;
      }
      else if(object.size() == 1) {
           return (SomeObject)objects.get(0);
      }
      else {
           throw new RuntimeException("Wow, this turned out to be to-many!");
      }
    }

    I guess we may introduce this as a class generation feature in the future,
    but it is failrly easy to do now. (I may add an FAQ entry in the docs).

    Andrus



    This archive was generated by hypermail 2.0.0 : Fri Apr 25 2003 - 12:32:46 EDT