When we perform em.*(entity1) and entity1 has relationship with entity2 then em atleast tries to maintain the relationships information in the database even though cascade is not declared for the performed operation. If em doesn't have enough information to store the defined relationships in the database, then it throws exception. Relations are maintained in the database by storing foreign keys to the parent table. Hence em expects access to the join column owning entity. If entity1 is the owner of the relationship, then entity1 table would be holding the join column. Here to retain the relationship, we just need to store entity2's id in the foreign key column of entity1. For this to happen the minimum requirement is entity2 should be an already persistent entity (because we need it's id in the database) , it can be detached also. If entity2 owns the join column and is already persistent then em has no work. If entity2 owns the join column and is not already persistent, then we can either call em. persist(entity2) or declare cascade(persist) on entity1.
No comments:
Post a Comment