Able to remove relation with ManyToMany and no cascade.

5 Messages Forum Options Options
Permalink
Jitendra chintamadaka
Able to remove relation with ManyToMany and no cascade.
Reply Threaded More
Print post
Permalink
I stumbled accross a test scenario and did not think it was working right. Before posting a bug, I wanted to check if this scenario was already addressed (either in this forum or other forums).

I have three tables:

STUDENT (SID INT, SFSTNM VCHAR10, SLSTNM VCHAR10)
TEAM (TID INT, NAME VCHAR10)
COLLEGE( STUDID INT - REFERENCING SID STUDENT table, TEAMID INT - REFERENCING TID of TEAM table)

STUDENT entity defines a owning relation of ManyToMany with TEAM as follows:

    @ManyToMany ()
    @JoinTable(name="COLLEGE", joinColumns=@JoinColumn(name="STUDID", referencedColumnName="SID"), inverseJoinColumns=@JoinColumn(name="TEAMID", referencedColumnName="TID"))
    public List<Team> getTeamCollection() {
        return teamCollection;
    }

TEAM entity defines the inverse relation as below:

    @ManyToMany(mappedBy="teamCollection")
    public List<Student> getStudentList() {
        return studentList;
    }

Data in the tables is as follows:
Student
SID         SFSTNM     SLSTNM
----------- ---------- ----------
          1 ONE        LAST
          2 TWO        LAST

Team
TID         TNAME
----------- ----------
        100 TEAM 100
        200 TEAM 200

College
STUDID      TEAMID
----------- -----------
          2         200
          1         100
          2         100
          1         200

Scenario 1:
Using EntityManager, I find the Student entity with PK value "1". Access the team list and remove the entry in the list with TID "100". Now when I commit this transaction, the entry from intermediate table (COLLEGE) with STUDID "1" and TEAMID "100" is removed.

Scenario 2:
Using EntityManager, I find the Team entity with PK value "200". Access the student list and remove the entry in the list with SID "1". On comitting the transaction, no changes are noticed in the COLLEGE table.

Conclusion:
With this testing, I am able to conclude that, using owner side of ManyToMany relation, we can remove entries from JoinTable (with or without any cascade option - Yes, I tried with no cascade option, refresh cascade option), where as from the inverse side of ManyToMany, we are unable to remove.

I have looked up the JSR 220 and did not find any specific mention on this scenario. This seems like a bug in the implementation. Please let me know if this really is a bug, so I can submit a bug report.

Regards
Jitendra.



     
Pinaki Poddar
Re: Able to remove relation with ManyToMany and no cascade.
Reply Threaded More
Print post
Permalink
Hi,

> I have looked up the JSR 220 and did not find any specific mention on this scenario. This seems like a
> bug in the implementation. Please let me know if this really is a bug, so I can submit a bug report.

JSR-220 Section 3.2.3 "Synchronization to the Database" pp 44 says:
"Bidirectional relationships between managed entities will be persisted based on references held by the owning side of the relationship"
 
The scenario you have described can be explained and not attributed as a limitation of implementation by the above.

Jitendra chintamadaka
Re: Able to remove relation with ManyToMany and no cascade.
Reply Threaded More
Print post
Permalink
Ok, section 3.2.3 in JSR 220, does explain.

Assuming that cascade attribute does not have any effect (as its not mentioned in that section). This then is an issue with the JPA specification itself. As introducing unwanted DB changes cannot be prevented with a annotation. May be I should open a issue or recommendation for JPA Specification itself.

Regards


----- Original Message ----
From: Pinaki Poddar <ppoddar@...>
To: users@...
Sent: Monday, July 7, 2008 8:04:03 PM
Subject: Re: Able to remove relation with ManyToMany and no cascade.


Hi,

> I have looked up the JSR 220 and did not find any specific mention on this
> scenario. This seems like a
> bug in the implementation. Please let me know if this really is a bug, so
> I can submit a bug report.

JSR-220 Section 3.2.3 "Synchronization to the Database" pp 44 says:
"Bidirectional relationships between managed entities will be persisted
based on references held by the owning side of the relationship"

The scenario you have described can be explained and not attributed as a
limitation of implementation by the above.


--
View this message in context: http://n2.nabble.com/Able-to-remove-relation-with-ManyToMany-and-no-cascade.-tp221593p363223.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


     
Pinaki Poddar
Re: Able to remove relation with ManyToMany and no cascade.
Reply Threaded More
Print post
Permalink
Hi,
> Access the student list and remove the entry in the list with SID "1"

What exactly does 'remove' imply?  Is it something like:
a) team.getStudents().remove(1);
or
b) em.remove(team.getStudents().get(1));

If it is (a) then no database change is expected. Removal of entity X from a Java collection will not, in general, imply deletion of X from database. This is in agreement with association relation that collection membership implies. However, OpenJPA does provide @Dependent/@ElementDependent annotations to mean ownership of B by A. Annotating uni-cardinality or multi-cardinality relation with @Dependent or @ElementDependent will cause deletion from database when the relationship is severed/nullified in memory. In JPA 2.0, this behavior is being specified under @OrphanRemoval annotation or an equivalent cascade type.

If it is (b) then the record should be deleted from database.

 
Jitendra chintamadaka wrote:
Ok, section 3.2.3 in JSR 220, does explain.

Assuming that cascade attribute does not have any effect (as its not mentioned in that section). This then is an issue with the JPA specification itself. As introducing unwanted DB changes cannot be prevented with a annotation. May be I should open a issue or recommendation for JPA Specification itself.

Regards


----- Original Message ----
From: Pinaki Poddar <ppoddar@apache.org>
To: users@openjpa.apache.org
Sent: Monday, July 7, 2008 8:04:03 PM
Subject: Re: Able to remove relation with ManyToMany and no cascade.


Hi,

> I have looked up the JSR 220 and did not find any specific mention on this
> scenario. This seems like a
> bug in the implementation. Please let me know if this really is a bug, so
> I can submit a bug report.

JSR-220 Section 3.2.3 "Synchronization to the Database" pp 44 says:
"Bidirectional relationships between managed entities will be persisted
based on references held by the owning side of the relationship"

The scenario you have described can be explained and not attributed as a
limitation of implementation by the above.


--
View this message in context: http://n2.nabble.com/Able-to-remove-relation-with-ManyToMany-and-no-cascade.-tp221593p363223.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


     
Jitendra chintamadaka
Re: Able to remove relation with ManyToMany and no cascade.
Reply Threaded More
Print post
Permalink
Hello,

> What exactly does 'remove' imply?  Is it something like:

> a) team.getStudents().remove(1);
> or
> b) em.remove(team.getStudents().get(1));

I meant (a). And with just JPA annotation specifications, the remove of entity from a java collection (on owning side) does imply removal of the associated record from the JoinTable (for ManyToMany).

From my Scenario 1 (Using owner of relationship "student") :
student.getTeamCollection().remove(0);
entityTransaction.commit();
// The record from COLLEGE table related to this student entity PK and the team entity PK removed above, will be deleted.

NOTE: This scenario 1, I think is not an expected behavior and can introduce un-expected programming bugs.

From my Scenario 2 (Using inverse of relationship "team") :
team.getStudentList().remove(0);
entityTransaction.commit();
// The record from COLLEGE table related to this team entity PK and the student entity PK remove above, will not be deleted.

NOTE: This scenario 2, behavior is explained by 3.2.3 of JPA specification. So, no questions here.

Regarding JPA 2.0, I have not yet reviewed the @OrphanRemoval annotation to comment yet. Will verify that and confirm if its really addressing this issue.

Thanks.

Regards,
Jitendra.