How do I insert with a Foreign Key?

Calvin Loh
2002-05-23
2002-05-26
  • Calvin Loh

    Calvin Loh - 2002-05-23

    Tried the FK example in the tutorial. It works,
    but now I need the FK to be another field. For
    example,
    TABLE Employee(empID varchar(5), ...,
                                     deptID varchar(5), ...,
                                     databaseIdentifier int);
    TABLE Department(deptID varchar(5), ...,
                                        databaseIdentifier int);

    I need deptID in Employee to refer to deptID in
    Department. Followed the example in the
    document ("Support Multiple Primary Keys"),
    setJoinOn(Department._deptID), but still found
    "Employee.deptID" set to
    "Department.databaseIdentifier".
    If I setBrokerGeneratedPrimaryKeys(false),
    then "Employee.deptID" is set to NULL in the
    DB.

    For both Employee and Department, I
    setPrimaryKeyField(...) to empID and deptID
    respectively. I have tried with both AccessBroker
    and RDBBroker but without success.

    TIA for any help that can be provided.

     
    • David Hoag

      David Hoag - 2002-05-23

      From your example it appears as if you don't even need databaseIdentifier. deptId and empId appear to be sufficient. I would stop declaring the existance of databaseIdentifier and in the persistent map declare that the deptId and empId are the primary keys. Of course, this may mean you don't want to use broker generated primary keys. If that is the case, you'll have to setBrokerGeneratePrimaryKeys( false ) on the RDBPersistentAdapter instance associated with your persistent objects.

      Did I understand correctly?

       
      • Calvin Loh

        Calvin Loh - 2002-05-24

        Yes, some of my cases require their own primary
        keys. Since it is a greenfield project, having the
        databaseIdentifier field doesn't bother me. I prefer
        the simplicity and reliability of just extending
        DomainObject.

        In any case, I did call
        setBrokerGeneratedPrimaryKeys(false). I also
        called setPrimaryKeyField(myPK). This works for
        the normal non-FK cases. But if I try to set the
        PK of the 2nd table (Dept-->deptID) as the FK of
        the 1st table (Emp-->empID), the field
        Emp-->deptID is set to NULL.

        I won't die without if I can't use FK and aggregates.
        But I foresee a number of cases where it would be
        convenient :)

        Anyway, thanks for all your help so far :)

         
    • David Hoag

      David Hoag - 2002-05-24

      You shouldn't have to call setPrimaryKeyField. Simply define a foreign relation in your persistence map. JGPL will automattically figure out which field in the related object is the primary key and thus determine the column name.
      In the map for the employee object:
      <code>
      AttributeTypeColumn col = AttributeTypeColum.getForeignRelation( Deptartment.class, "deptId", _department );
      </code>

      A field is designated as a primary key in one of two ways. If your persistent definition is done with XML, simply identifying the primary key in the xml will be enough.
      If you are defining your attribute map in code, there is a method on attribute type column that you should use.
      In the map for a Department object:
      <code>
      AttributeTypeColumn col = AttributeTypeCoumn.getAttributeRelation( "deptId", _departmentId);
      col.setAsPrimary();
      </code>

       
      • Calvin Loh

        Calvin Loh - 2002-05-26

        Thanks. It works now.

         

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks