Fix deleted object would be re-saved by cascade (remove deleted object from associations): [com.wisemapping.model.Collaboration#1651889]

This commit is contained in:
Paulo Gustavo Veiga 2022-10-21 23:06:04 -07:00
parent 4ee62035b8
commit 6cd0f635d5
5 changed files with 27 additions and 25 deletions

View File

@ -26,6 +26,7 @@ import org.jetbrains.annotations.Nullable;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects;
@Entity @Entity
@Table(name = "COLLABORATION") @Table(name = "COLLABORATION")
@ -127,16 +128,14 @@ public class Collaboration implements Serializable {
Collaboration that = (Collaboration) o; Collaboration that = (Collaboration) o;
if (id != that.id) return false; if (id != that.id) return false;
if (collaborator != null ? !collaborator.equals(that.collaborator) : that.collaborator != null) return false; if (!Objects.equals(collaborator, that.collaborator)) return false;
if (mindMap != null ? !mindMap.equals(that.mindMap) : that.mindMap != null) return false; if (!Objects.equals(mindMap, that.mindMap)) return false;
return role == that.role; return role == that.role;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = id ^ (id >>> 32); //https://thorben-janssen.com/ultimate-guide-to-implementing-equals-and-hashcode-with-hibernate/
result = 31 * result + (role != null ? role.hashCode() : 0); return 13;
result = 31 * result + (mindMap != null ? mindMap.hashCode() : 0);
return result;
} }
} }

View File

@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable;
import javax.persistence.*; import javax.persistence.*;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects;
@Entity @Entity
@Table(name = "LABEL") @Table(name = "LABEL")
@ -34,17 +35,22 @@ public class Label implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id; private int id;
@NotNull private String title; @NotNull
@NotNull private String color; private String title;
@Nullable private String iconName; @NotNull
private String color;
@Nullable
private String iconName;
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="creator_id",nullable = true,unique = true) @JoinColumn(name = "creator_id", nullable = true, unique = true)
@NotNull private User creator; @NotNull
private User creator;
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="parent_label_id",nullable = true) @JoinColumn(name = "parent_label_id", nullable = true)
@Nullable private Label parent; @Nullable
private Label parent;
public void setParent(@Nullable Label parent) { public void setParent(@Nullable Label parent) {
this.parent = parent; this.parent = parent;
@ -104,17 +110,15 @@ public class Label implements Serializable {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof Label)) return false; if (!(o instanceof Label)) return false;
Label label = (Label) o; final Label label = (Label) o;
return id == label.id && creator.getId() == label.creator.getId() return id == label.id && creator.getId() == label.creator.getId()
&& !(parent != null ? !parent.equals(label.parent) : label.parent != null); && Objects.equals(parent, label.parent);
} }
@Override @Override
public int hashCode() { public int hashCode() {
long result = id; long result = title.hashCode();
result = 31 * result + title.hashCode(); result = 31 * result + (creator != null ? creator.hashCode() : 0);
result = 31 * result + (creator!=null?creator.hashCode():0);
result = 31 * result + (parent != null ? parent.hashCode() : 0); result = 31 * result + (parent != null ? parent.hashCode() : 0);
return (int) result; return (int) result;
} }

View File

@ -146,7 +146,9 @@ public class Mindmap implements Serializable {
} }
public void removedCollaboration(@NotNull Collaboration collaboration) { public void removedCollaboration(@NotNull Collaboration collaboration) {
collaborations.remove(collaboration); // https://stackoverflow.com/questions/25125210/hibernate-persistentset-remove-operation-not-working
this.collaborations.remove(collaboration);
collaboration.setMindMap(null);
} }
public void removedCollaboration(@NotNull Set<Collaboration> collaborations) { public void removedCollaboration(@NotNull Set<Collaboration> collaborations) {

View File

@ -38,7 +38,6 @@ public class ViewBaseSecurityAdvise
@Override @Override
protected boolean isAllowed(@Nullable User user, Mindmap map) { protected boolean isAllowed(@Nullable User user, Mindmap map) {
System.out.println("VIEWWWWWWWWWWWWW");
return getMindmapService().hasPermissions(user, map, CollaborationRole.VIEWER); return getMindmapService().hasPermissions(user, map, CollaborationRole.VIEWER);
} }

View File

@ -143,15 +143,13 @@ public class MindmapServiceImpl
public void removeCollaboration(@NotNull Mindmap mindmap, @NotNull Collaboration collaboration) throws CollaborationException { public void removeCollaboration(@NotNull Mindmap mindmap, @NotNull Collaboration collaboration) throws CollaborationException {
// remove collaborator association // remove collaborator association
final Mindmap mindMap = collaboration.getMindMap(); final Mindmap mindMap = collaboration.getMindMap();
final Set<Collaboration> collaborations = mindMap.getCollaborations();
final User creator = mindMap.getCreator(); final User creator = mindMap.getCreator();
if (creator.identityEquality(collaboration.getCollaborator())) { if (creator.identityEquality(collaboration.getCollaborator())) {
throw new CollaborationException("User is the creator and must have ownership permissions.Creator Email:" + mindMap.getCreator().getEmail() + ",Collaborator:" + collaboration.getCollaborator().getEmail()); throw new CollaborationException("User is the creator and must have ownership permissions.Creator Email:" + mindMap.getCreator().getEmail() + ",Collaborator:" + collaboration.getCollaborator().getEmail());
} }
// When you delete an object from hibernate you have to delete it from *all* collections it exists in... // When you delete an object from hibernate you have to delete it from *all* collections it exists in...
collaborations.remove(collaboration); mindMap.removedCollaboration(collaboration);
mindmapManager.removeCollaboration(collaboration); mindmapManager.removeCollaboration(collaboration);
} }
@ -249,7 +247,7 @@ public class MindmapServiceImpl
@Override @Override
public void revertChange(@NotNull Mindmap mindmap, int historyId) public void revertChange(@NotNull Mindmap mindmap, int historyId)
throws WiseMappingException, IOException { throws WiseMappingException {
final MindMapHistory history = mindmapManager.getHistory(historyId); final MindMapHistory history = mindmapManager.getHistory(historyId);
mindmap.setZippedXml(history.getZippedXml()); mindmap.setZippedXml(history.getZippedXml());
updateMindmap(mindmap, true); updateMindmap(mindmap, true);