Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0e115bb2

Přidáno uživatelem Jakub Šmíd před více než 2 roky(ů)

Fixed N+1 problem when fetching catalog

re #9244

Zobrazit rozdíly:

backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItem.java
7 7
import cz.zcu.kiv.backendapi.writtenform.WrittenForm;
8 8
import lombok.Data;
9 9
import lombok.NoArgsConstructor;
10
import org.hibernate.annotations.Fetch;
11
import org.hibernate.annotations.FetchMode;
10 12
import org.hibernate.annotations.LazyCollection;
11 13
import org.hibernate.annotations.LazyCollectionOption;
12 14

  
......
23 25
@NoArgsConstructor
24 26
@Entity
25 27
@Table(name = "catalog_item")
28
//@NamedEntityGraph(name = "graph", attributeNodes = {
29
//        @NamedAttributeNode("bibliography"),
30
//        @NamedAttributeNode("alternativeNames"),
31
//        @NamedAttributeNode("countries"),
32
//        @NamedAttributeNode("writtenForms"),
33
//        @NamedAttributeNode("types"),
34
//})
26 35
public class CatalogItem {
27 36
    private static final String INTEGER_PATTERN = "\\d+";
28 37
    private static final String DOUBLE_PATTERN = "(\\d+[.]\\d+)|(\\d+)";
......
59 68
     */
60 69
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
61 70
    @LazyCollection(LazyCollectionOption.FALSE)
71
    @Fetch(FetchMode.SUBSELECT)
62 72
    private Set<Bibliography> bibliography = Collections.emptySet();
63 73

  
64 74
    /**
......
66 76
     */
67 77
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
68 78
    @LazyCollection(LazyCollectionOption.FALSE)
79
    @Fetch(FetchMode.SUBSELECT)
69 80
    private Set<Country> countries = Collections.emptySet();
70 81

  
71 82
    /**
......
73 84
     */
74 85
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
75 86
    @LazyCollection(LazyCollectionOption.FALSE)
87
    @Fetch(FetchMode.SUBSELECT)
76 88
    private Set<WrittenForm> writtenForms = Collections.emptySet();
77 89

  
78 90
    /**
......
80 92
     */
81 93
    @OneToMany(mappedBy = "catalogItem", cascade = CascadeType.ALL)
82 94
    @LazyCollection(LazyCollectionOption.FALSE)
95
    @Fetch(FetchMode.SUBSELECT)
83 96
    private Set<AlternativeName> alternativeNames = Collections.emptySet();
84 97

  
85 98
    /**
86 99
     * Set of user roles - many-to-many relationship
87 100
     */
88 101
    @ManyToMany(fetch = FetchType.EAGER)
102
    @Fetch(FetchMode.SUBSELECT)
89 103
    @JoinTable(
90 104
            name = "catalog_item_type",
91 105
            joinColumns = {
backend/src/main/java/cz/zcu/kiv/backendapi/catalog/CatalogItemRepository.java
21 21
     * @param type    type - optional
22 22
     * @return set of catalog items satisfying filter conditions
23 23
     */
24
    @Query("SELECT DISTINCT e FROM CatalogItem e LEFT JOIN AlternativeName a ON e = a.catalogItem " +
25
            "LEFT JOIN Country c ON e = c.catalogItem " +
26
            "INNER JOIN e.types t " +
24
    @Query("SELECT DISTINCT e FROM CatalogItem e " +
25
            "LEFT JOIN FETCH AlternativeName a ON e = a.catalogItem " +
26
            "LEFT JOIN FETCH Country c ON e = c.catalogItem " +
27
            "INNER JOIN FETCH e.types t " +
27 28
            "WHERE (?1 = '' OR UPPER(e.name) LIKE UPPER(?1) OR UPPER(a.name) LIKE UPPER(?1)) " +
28 29
            "AND (?2 = '' OR UPPER(c.name) LIKE UPPER(?2)) " +
29 30
            "AND (?3 = '' OR UPPER(t.type) LIKE UPPER(?3))")
backend/src/main/java/cz/zcu/kiv/backendapi/security/SecurityConfig.java
69 69
                .antMatchers("/delete/**").hasAuthority(Permission.DELETE.name())
70 70
                .anyRequest()
71 71
                .authenticated();
72

  
73 72
    }
74 73

  
75 74
    /**
backend/src/main/java/cz/zcu/kiv/backendapi/security/jwt/JwtUsernameAndPasswordAuthenticationFilter.java
6 6
import cz.zcu.kiv.backendapi.user.UserEntity;
7 7
import lombok.RequiredArgsConstructor;
8 8
import lombok.extern.slf4j.Slf4j;
9
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
9 10
import org.springframework.security.authentication.AuthenticationManager;
10 11
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
11 12
import org.springframework.security.core.Authentication;
......
61 62
            return authenticationManager.authenticate(authentication);
62 63
        } catch (IOException e) {
63 64
            log.error("Error getting authentication request from request: " + e.getMessage());
64
            throw new RuntimeException(e);
65
            throw new AuthenticationCredentialsNotFoundException(e.getMessage());
65 66
        }
66 67
    }
67 68

  
backend/src/main/resources/application.properties
7 7
spring.jpa.open-in-view=false
8 8
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL10Dialect
9 9
spring.jpa.properties.hibernate.format_sql=true
10
spring.jpa.show-sql=true
10
#spring.jpa.show-sql=true
11 11

  
12 12

  
13 13
application.jwt.secretKey=securesecuresecuresecuresecuresecuresecuresecuresecuresecuresecuresecuresecure

Také k dispozici: Unified diff