Skip to content

Commit

Permalink
JPA ManyToMany bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wangf1122 committed Jul 11, 2024
1 parent 56ac85c commit 292d127
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions domain/src/main/java/org/fao/geonet/domain/page/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.fao.geonet.domain.GeonetEntity;
Expand Down Expand Up @@ -160,9 +160,9 @@ public String getIcon() {
*
* @return all the page's groups.
*/
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST, orphanRemoval = false)
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinTable(name = "spg_page_group", joinColumns = {@JoinColumn(name = "language"), @JoinColumn(name = "linktext")},
inverseJoinColumns = {@JoinColumn(name = "groupid", referencedColumnName = "id", unique = true)})
inverseJoinColumns = {@JoinColumn(name = "groupid", referencedColumnName = "id", unique = false)})
public Set<Group> getGroups() {
return groups;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface PageRepository extends JpaRepository<Page, PageIdentity> {

List<Page> findByPageIdentityLanguage(String language);

List<Page> findPageByStatus(Page.PageStatus status);

}
23 changes: 11 additions & 12 deletions services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import java.nio.file.attribute.FileTime;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

import static org.springframework.data.jpa.domain.Specification.where;

Expand Down Expand Up @@ -547,19 +548,17 @@ public void deleteGroup(
));
}

List<Page> staticPages = pageRepository.findAll();
List<Page> staticPages = pageRepository.findPageByStatus(Page.PageStatus.GROUPS);
List<Page> staticPagesAssignedToGroup =
staticPages.stream().filter(p ->
!p.getGroups().stream().filter(g -> g.getId() == groupIdentifier).collect(Collectors.toList()).isEmpty())
.collect(Collectors.toList());

for (Page page: staticPages) {
if (CollectionUtils.isNotEmpty(page.getGroups())) {
for (Group pageGroup : page.getGroups()) {
if (pageGroup.getId() == groupIdentifier) {
throw new NotAllowedException(String.format(
"Group %s is associated with '%s' static page(s). Please remove the static page(s) associated with that group first.",
group.get().getName(), page.getLabel()
));
}
}
}
if (!staticPagesAssignedToGroup.isEmpty()) {
throw new NotAllowedException(String.format(
"Group %s is associated with '%s' static page(s). Please remove the static page(s) associated with that group first.",
group.get().getName(), staticPagesAssignedToGroup.stream().map(p -> p.getLabel()).collect(Collectors.joining())
));
}

groupRepository.deleteById(groupIdentifier);
Expand Down

0 comments on commit 292d127

Please sign in to comment.