[question]: problems with n:m mappings (flattened-relationships).

From: Laszlo Spoor (lspoor_cayenn..otmail.com)
Date: Thu Feb 27 2003 - 03:54:45 EST

  • Next message: Miguel Angel Lorente Navarro: "Primary Key Generation-SQL Server 7.0"

    Database: MySQL 3.23
    Cayenne : 1.0a6

    Hi Guys,

    I have recently started to work with Cayenne. Pretty cool stuff! However, I
    have some problems with the 'flattened-relationships'. My problem is that I
    cannot get it to work, but I don't know what I am doing wrong. I described
    the steps I took below:

    I have created 3 tables (this case is based on the mail of one of the
    subscribers of this mailing list) and inserted some data:

    EMPLOYEES (id integer, firstname varchar(100));
    PROJECTS (id integer, projectname varchar2(100));
    EMP_PROJ (emp_id integer, prj_id integer);

    Since I use MySQL (which has no support for referential constraints), I
    created the relationships between the DataObject myself in the modeler
    (after reverse engineering the schema into cayenne):

    EMPLOYEES <-|(1:n)|-> EMP_PROJ
    PROJECTS <-|(1:n)|-> EMP_PROJ
    EMP_PROJ <-|(n:1)|-> EMPLOYEES
    EMP_PROJ <-|(n:1)|-> PROJECTS

    The objects that Cayenne generated for me where:
    - Employees
    - Projects
    - EmpProj

    I removed the EmpProj Object (not the table), removed the Ids and renamed
    the classes to:
    - Employee
    - Project

    In the ObjectMap I created a pass-through relationship for Employee:
    name : projects
    target : Project
    to-many : yes.

    The DBRelationship I selected was: toProjectEmployees.ToProjects. This maps
    from EMPLOYEE.ID to EMP_PROJ.PRJ_ID. I cannot select anything else. It will
    just replace it with PRJ_ID, which seems fine.

    In the ObjectMap I created a pass-through relationship for Project:
    name : employees
    target : Projects
    to-many : yes.

    The DBRelationship I selected here was: toProjectEmployeesToEmployees. This
    maps from PROJECT.ID to EMP_PROJ.EMP_ID. I cannot select anything else here
    as well.

    The Java Code I use is:

       [...]
       DataContext mContext =
         Configuration.getSharedConfig().getDomain().createDataContext();

       SelectQuery qry = new SelectQuery (Employee.class);
       List emps = mContext.performQuery(qry);

       Employee emp = (Employee) emps.get(0);
       logger.info("The Employee instance found: " + emp.getFirstname());

       qry = null;
       qry = new SelectQuery (Project.class);
       List prjs = mContext.performQuery(qry);

       Project prj = (Project) prjs.get(0);
       logger.info("The Project Instance found: " + prj.getName());

       logger.info("Adding employee to project... ");
       emp.addToProjects(prj);

       mContext.commitChanges(Level.WARN);
       [...]

    ... Which results in:

    INFO AddEmpToProject: The Employee instance found: John
    INFO AddEmpToProject: The Project Instance found : yx-05
    INFO AddEmpToProject: Adding employee to project...

    org.objectstyle.cayenne.CayenneRuntimeException: Cannot modify (add to) the
    read-only relationship projects
    at
    org.objectstyle.cayenne.CayenneDataObject.addToManyTarget(CayenneDataObject.java
    :283)at tst.cayenne._Employee.addToProjects(_Employee.java:22)
    at tst.test.AddEmpToProject.main(AddEmpToProject.java:51)
    Exception in thread main

    I tried several variations, but this seems the most probable way to me. Can
    anyone tell me what I am doing wrong and what I should do to get this
    working?

    Thanks in advance, Laszlo

    _________________________________________________________________
    Add photos to your e-mail with MSN 8. Get 2 months FREE*.
    http://join.msn.com/?page=features/featuredemail



    This archive was generated by hypermail 2.0.0 : Thu Feb 27 2003 - 03:57:27 EST