commit 0e115bb20f2daaa1269ea052776684f18358f9dd
Author: Jakub Smid <jaksmid@seznam.cz>
Date:   Sun Apr 3 10:08:52 2022 +0200

    Fixed N+1 problem when fetching catalog
    
    re #9244

diff --git a/backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItem.java b/backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItem.java
index 5268ae1..d5db4d0 100644
--- a/backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItem.java
+++ b/backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItem.java
@@ -7,6 +7,8 @@ import cz.zcu.kiv.backendapi.type.Type;
 import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import org.hibernate.annotations.Fetch;
+import org.hibernate.annotations.FetchMode;
 import org.hibernate.annotations.LazyCollection;
 import org.hibernate.annotations.LazyCollectionOption;
 
@@ -23,6 +25,13 @@ import java.util.stream.Collectors;
 @NoArgsConstructor
 @Entity
 @Table(name = "catalog_item")
+//@NamedEntityGraph(name = "graph", attributeNodes = {
+//        @NamedAttributeNode("bibliography"),
+//        @NamedAttributeNode("alternativeNames"),
+//        @NamedAttributeNode("countries"),
+//        @NamedAttributeNode("writtenForms"),
+//        @NamedAttributeNode("types"),
+//})
 public class CatalogItem {
     private static final String INTEGER_PATTERN = "\\d+";
     private static final String DOUBLE_PATTERN = "(\\d+[.]\\d+)|(\\d+)";
@@ -59,6 +68,7 @@ public class CatalogItem {
      */
     @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
     @LazyCollection(LazyCollectionOption.FALSE)
+    @Fetch(FetchMode.SUBSELECT)
     private Set<Bibliography> bibliography = Collections.emptySet();
 
     /**
@@ -66,6 +76,7 @@ public class CatalogItem {
      */
     @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
     @LazyCollection(LazyCollectionOption.FALSE)
+    @Fetch(FetchMode.SUBSELECT)
     private Set<Country> countries = Collections.emptySet();
 
     /**
@@ -73,6 +84,7 @@ public class CatalogItem {
      */
     @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
     @LazyCollection(LazyCollectionOption.FALSE)
+    @Fetch(FetchMode.SUBSELECT)
     private Set<WrittenForm> writtenForms = Collections.emptySet();
 
     /**
@@ -80,12 +92,14 @@ public class CatalogItem {
      */
     @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
     @LazyCollection(LazyCollectionOption.FALSE)
+    @Fetch(FetchMode.SUBSELECT)
     private Set<AlternativeName> alternativeNames = Collections.emptySet();
 
     /**
      * Set of user roles - many-to-many relationship
      */
     @ManyToMany(fetch = FetchType.EAGER)
+    @Fetch(FetchMode.SUBSELECT)
     @JoinTable(
             name = "catalog_item_type",
             joinColumns = {
diff --git a/backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepository.java b/backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepository.java
index 8a8a148..06d3813 100644
--- a/backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepository.java
+++ b/backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepository.java
@@ -21,9 +21,10 @@ public interface CatalogItemRepository extends JpaRepository<CatalogItem, UUID>
      * @param type    type - optional
      * @return set of catalog items satisfying filter conditions
      */
-    @Query("SELECT DISTINCT e FROM CatalogItem e LEFT JOIN AlternativeName a ON e = a.catalogItem " +
-            "LEFT JOIN Country c ON e = c.catalogItem " +
-            "INNER JOIN e.types t " +
+    @Query("SELECT DISTINCT e FROM CatalogItem e " +
+            "LEFT JOIN FETCH AlternativeName a ON e = a.catalogItem " +
+            "LEFT JOIN FETCH Country c ON e = c.catalogItem " +
+            "INNER JOIN FETCH e.types t " +
             "WHERE (?1 = '' OR UPPER(e.name) LIKE UPPER(?1) OR UPPER(a.name) LIKE UPPER(?1)) " +
             "AND (?2 = '' OR UPPER(c.name) LIKE UPPER(?2)) " +
             "AND (?3 = '' OR UPPER(t.type) LIKE UPPER(?3))")
diff --git a/backend/src/main/java/cz/zcu/kiv/backendapi/security/SecurityConfig.java b/backend/src/main/java/cz/zcu/kiv/backendapi/security/SecurityConfig.java
index 9d17e56..e3649b0 100644
--- a/backend/src/main/java/cz/zcu/kiv/backendapi/security/SecurityConfig.java
+++ b/backend/src/main/java/cz/zcu/kiv/backendapi/security/SecurityConfig.java
@@ -69,7 +69,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/delete/**").hasAuthority(Permission.DELETE.name())
                 .anyRequest()
                 .authenticated();
-
     }
 
     /**
diff --git a/backend/src/main/java/cz/zcu/kiv/backendapi/security/jwt/JwtUsernameAndPasswordAuthenticationFilter.java b/backend/src/main/java/cz/zcu/kiv/backendapi/security/jwt/JwtUsernameAndPasswordAuthenticationFilter.java
index 6d8ca07..b294492 100644
--- a/backend/src/main/java/cz/zcu/kiv/backendapi/security/jwt/JwtUsernameAndPasswordAuthenticationFilter.java
+++ b/backend/src/main/java/cz/zcu/kiv/backendapi/security/jwt/JwtUsernameAndPasswordAuthenticationFilter.java
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import cz.zcu.kiv.backendapi.user.UserEntity;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
 import org.springframework.security.authentication.AuthenticationManager;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
@@ -61,7 +62,7 @@ public class JwtUsernameAndPasswordAuthenticationFilter extends UsernamePassword
             return authenticationManager.authenticate(authentication);
         } catch (IOException e) {
             log.error("Error getting authentication request from request: " + e.getMessage());
-            throw new RuntimeException(e);
+            throw new AuthenticationCredentialsNotFoundException(e.getMessage());
         }
     }
 
diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties
index b401cda..787b21d 100644
--- a/backend/src/main/resources/application.properties
+++ b/backend/src/main/resources/application.properties
@@ -7,7 +7,7 @@ spring.jpa.generate-ddl=true
 spring.jpa.open-in-view=false
 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL10Dialect
 spring.jpa.properties.hibernate.format_sql=true
-spring.jpa.show-sql=true
+#spring.jpa.show-sql=true
 
 
 application.jwt.secretKey=securesecuresecuresecuresecuresecuresecuresecuresecuresecuresecuresecuresecure
