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

From: Craig Miskell (cmiskel..lbatross.co.nz)
Date: Thu Feb 27 2003 - 13:48:58 EST

  • Next message: Laszlo Spoor: "Re: [question]: problems with n:m mappings(flattened-relationships)."

    You're certainly close and have all the right ideas, but there's likely
    just one small thing tripping you up. Flattened relationships are
    determined to be readonly, UNLESS

    1) It has exactly 2 dbrelationships (true in your case, so not the
    problem)
    2) The first relationship in the path is toMany or the the second is
    toOne (in your case, toProjectEmployees is toMany and toEmployess is
    toOne, so that's not the problem).
    3) Finally, and this is where I suspect something may be going astray in
    your model, all of the attributes in the intermediate table must be part
    of the primary key of the intermediate table. It's a guess (without
    seeing your model), but I reckon that the most likely candidate is that
    EMP_PROJ dbEntity doesn't have both it's attributes marked as pk?

    If not, send me your data map and I'll have a look.

    Craig

    On Thu, 2003-02-27 at 21:54, Laszlo Spoor wrote:
    > 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 - 13:49:35 EST