Re: many to many relation

From: gilbertoca (gilbertoc..mail.com)
Date: Thu Sep 30 2010 - 17:54:55 UTC

  • Next message: Andrey Razumovsky: "Re: many to many relations and default values for the association table?"

    Robert Zeigler-6 wrote:
    >
    > Don't use cascade from User -> Role or from Role -> User. Given that a
    > user can existing with 0 or more roles, and a role can exist with 0 or
    > more users... cascade definitely not what you want there. You might
    > believe that "cascade" is the right choice because you want to "cascade"
    > the deletion from User to the association table. But note that the delete
    > rule is on the object relationship: from user -> role (and vice versa).
    >
    Something that would help a lot is just one place to define relationship. It
    seems that object relationship and db relationship are things different and
    that confuses me!

    Robert Zeigler-6 wrote:
    >
    > If you had modeled the join table as an object entity, /then/ you would
    > make the delete rule (from user to association table) cascade, and the
    > delete rule form association table -> role "nullify". But since you're
    > working with a "flattened" relationship, you just specify Nullify, and
    > cayenne will do the right thing.
    >
    Yes, taking your advice it seems to work. But ...

    Robert Zeigler-6 wrote:
    >
    > To prove this, I changed the delete rule from user -> role to Nullify, and
    > from role -> user to Nullify, then added the following lines to the
    > "testAddAndRemoveUser" test:
    >
    > lU = (List<User>) userService.getAll();
    > System.out.println("***** Checking # users *****");
    > assertEquals(3, lU.size());
    > System.out.println("***** Checking # roles *****");
    > assertEquals(2, roleService.getAll().size());
    >
    >

    Yes, but for some reason it leaves the database in a inconsistent state:

    > Testcase:
    > com.google.constructor.extras.security.shiro.cayenne.UserCayenneTest:
    > Caused an ERROR
    > Referential integrity constraint violation: "FK_SHIRO_USER_ROLE_ROLE_ID:
    > PUBLIC.SHIRO_USER_ROLE FOREIGN KEY(ROLE_ID) REFERENCES
    > PUBLIC.SHIRO_ROLE(ID)"; SQL statement:
    > delete from SHIRO_ROLE where ID = ? [23003-131]
    > org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation:
    > "FK_SHIRO_USER_ROLE_ROLE_ID: PUBLIC.SHIRO_USER_ROLE FOREIGN KEY(ROLE_ID)
    > REFERENCES PUBLIC.SHIRO_ROLE(ID)"; SQL statement:
    > delete from SHIRO_ROLE where ID = ? [23003-131]
    > at
    > org.h2.message.DbException.getJdbcSQLException(DbException.java:316)
    >
    I will try to find the problem!

    Robert Zeigler-6 wrote:
    >
    > Without the change, you get 0 users and 0 roles. Because:
    > deleteUser -> deleting the user, who is associated with the default role;
    > b/c of cascade rule, default role is deleted. Default role is associated
    > with all users; because of cascade rule, all users are deleted. Because
    > of cascade rule, any additional roles (admin) associated with users are
    > /also/ deleted. So the net result is: you have no users or roles left in
    > your db at the end of your test. :)
    >
    That was the situation! :)

    Thanks a lot, Robert.

    Regards,

    Gilberto

    -- 
    View this message in context: http://cayenne.195.n3.nabble.com/many-to-many-relation-tp3491p1609838.html
    Sent from the Cayenne - User mailing list archive at Nabble.com.
    



    This archive was generated by hypermail 2.0.0 : Thu Sep 30 2010 - 17:55:25 UTC