orm: DDC-130: Cascading and @ManyToMany associations is broken
Jira issue originally created by user nicokaiser:
I have two Entities: Users and Alerts. They are associated with @ManyToMany, the assiciation table should be “user_alert”.
Entities: http://pastebin.com/m7bca724a
Sample code: http://pastebin.com/m4e530f42
The “remove” command in the sample code deletes the user entry and alert entry, but not the user_alert entry (which was automatically created though). This leaves an orphan entry (or the DBMS will complain because of FK constraints).
INSERT INTO users (name) VALUES (?)
array(1) {
[1]=>
string(3) "Bob"
}
INSERT INTO alert (name) VALUES (?)
array(1) {
[1]=>
string(9) "Testalert"
}
INSERT INTO user_alert (userId, alertId) VALUES (?, ?)
array(2) {
[0]=>
int(1)
[1]=>
int(1)
}
DELETE FROM users WHERE id = ?
array(1) {
[0]=>
int(1)
}
DELETE FROM alert WHERE id = ?
array(1) {
[0]=>
int(1)
}
The user_alert table should be automatically updated (on creation and removal)…
About this issue
- Original URL
- State: closed
- Created 15 years ago
- Comments: 20
Comment created by alex:
@Roman: All sounds very simple. But in practice this doesn’t seem to work. I have the following:
Now, when I clear the manager and reload the user:
Comment created by romanb:
@Alexander: Just like in normal OOP. If a user as a collection of alerts and an alert a collection of users (bidirectional many-many) and you want to remove an alert from a user you simply remove the alert from the collection and the user from the collection of the corresponding alert. This is very clean, the only problem with this is the efficiency because in order to remove the elements from the collections, the collections must be loaded. (Strictly speaking, you only need to adjust the “owning side”, so only 1 collection needs to be loaded). Of course you can always use some custom SQL that directly manipulates the intermediate table. See also DDC-128. Also there is DDC-45 but I dont think thats doable.
Collections of entities always only represent the association of the entity that has the collection and the contained entities. If you remove an entity from a collection, the association is removed, not the entity itself. “The association is removed” means that either the foreign key is NULLed out (in the case of one-to-many) or that the entry in the association table is removed.
Comment created by alex:
How about removing a relation between a user and an alert?
Meaning the user and alert both remain in the database but they are not connected anymore (think about users with multiple roles).