change to lazy fetching in *toOne relationships and modified repository queries accordingy with join fetch to avoid n+1 queries
This commit is contained in:
parent
bf195efea7
commit
c2e0951144
@ -4,6 +4,7 @@ import java.util.Date;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
@ -23,13 +24,13 @@ public class ChatMessage {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "m_id")
|
||||
private Long messageID;
|
||||
@OneToOne
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "from_user")
|
||||
private ChatUser fromUser;
|
||||
@OneToOne
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "to_user")
|
||||
private ChatUser toUser;
|
||||
@OneToOne
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "message")
|
||||
private MessageCipher messageCipher;
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.ros.chatto.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
@ -14,12 +15,12 @@ public class UserRole {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
@ManyToOne
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "user_id")
|
||||
@JsonManagedReference
|
||||
private ChatUser user;
|
||||
|
||||
@ManyToOne
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "role_id")
|
||||
@JsonManagedReference
|
||||
private Role role;
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.ros.chatto.repository;
|
||||
|
||||
import java.awt.print.Pageable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ -16,16 +15,16 @@ public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long>
|
||||
// @Query("select ur from UserRole ur where ur.user.userID = ?1")
|
||||
// @Query("select m from ChatMessage m where m.toUser.userID = ?1 or m.toUser.userID = ?2 and "
|
||||
// + "m.fromUser.userID = ?1 or m.fromUser.userID = ?2 order by m.messageTime asc")
|
||||
@Query("select m from ChatMessage m where (m.toUser.userName = ?1 or m.toUser.userName = ?2) and "
|
||||
@Query("select m from ChatMessage m join fetch m.messageCipher where (m.toUser.userName = ?1 or m.toUser.userName = ?2) and "
|
||||
+ "(m.fromUser.userName = ?1 or m.fromUser.userName = ?2) order by m.messageTime asc")
|
||||
public List<ChatMessage> getAllMessages(String fromUser, String toUser);
|
||||
|
||||
@Query("select m from ChatMessage m where (m.toUser.userName = ?1 or m.toUser.userName = ?2) and "
|
||||
@Query("select m from ChatMessage m join fetch m.messageCipher where (m.toUser.userName = ?1 or m.toUser.userName = ?2) and "
|
||||
+ "(m.fromUser.userName = ?1 or m.fromUser.userName = ?2) and"
|
||||
+ "(m.messageTime > ?3) order by m.messageTime asc")
|
||||
public List<ChatMessage> getNewMessages(String fromUser, String toUser, Date lastMessageTime);
|
||||
|
||||
@Query("select m from ChatMessage m where (m.toUser.userName = ?1 or m.toUser.userName = ?2) and "
|
||||
@Query("select m from ChatMessage m join fetch m.messageCipher where (m.toUser.userName = ?1 or m.toUser.userName = ?2) and "
|
||||
+ "(m.fromUser.userName = ?1 or m.fromUser.userName = ?2) order by m.messageTime asc")
|
||||
public List<ChatMessage> getAllMessages(String fromUser, String toUser, PageRequest pageRequest);
|
||||
|
||||
|
@ -8,13 +8,13 @@ import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserRoleRepository extends JpaRepository<UserRole, Long>{
|
||||
@Query("select ur from UserRole ur where ur.user.userID = ?1")
|
||||
public interface UserRoleRepository extends JpaRepository<UserRole, Long> {
|
||||
@Query("select ur from UserRole ur join fetch ur.user join fetch ur.role where ur.user.userID = ?1")
|
||||
public List<UserRole> findByUser(int userID);
|
||||
|
||||
@Query("select ur from UserRole ur where ur.user.userName = ?1")
|
||||
@Query("select ur from UserRole ur join fetch ur.user join fetch ur.role where ur.user.userName = ?1")
|
||||
public List<UserRole> findByUser(String username);
|
||||
|
||||
@Query("select ur.user.userName from UserRole ur where ur.role.roleID = 2")
|
||||
|
||||
@Query("select ur.user.userName from UserRole ur join ur.role where ur.role.roleID = 2")
|
||||
public List<String> getAllRegularUser();
|
||||
}
|
||||
|
@ -7,6 +7,6 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserSessionRepository extends JpaRepository<UserSession, Long> {
|
||||
@Query("select us from UserSession us where us.user.userName = ?1 ")
|
||||
@Query("select us from UserSession us join fetch us.user where us.user.userName = ?1 ")
|
||||
public UserSession findByUserName(String userName);
|
||||
}
|
||||
|
@ -13,6 +13,8 @@ spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDial
|
||||
# Hibernate ddl auto (create, create-drop, validate, update)
|
||||
spring.jpa.hibernate.ddl-auto = none
|
||||
|
||||
spring.jpa.properties.hibernate.generate_statistics=true
|
||||
logging.level.org.hibernate.stat=debug
|
||||
logging.level.org.springframework.web=DEBUG
|
||||
logging.level.web=DEBUG
|
||||
logging.level.org.hibernate.SQL=DEBUG
|
||||
|
@ -2,19 +2,14 @@ package org.ros.chatto;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.ros.chatto.model.ChatMessage;
|
||||
import org.ros.chatto.model.ChatUser;
|
||||
import org.ros.chatto.repository.ChatMessageRepository;
|
||||
import org.ros.chatto.repository.UserRepository;
|
||||
import org.ros.chatto.repository.UserRoleRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@ -28,6 +23,11 @@ public class ChattoApplicationTests {
|
||||
//
|
||||
// @Autowired
|
||||
// UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
UserRoleRepository userRoleRepository;
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ChattoApplicationTests.class);
|
||||
//
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
@ -38,6 +38,12 @@ public class ChattoApplicationTests {
|
||||
// chatMessageRepository.findAll().toString();
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void testRepo() {
|
||||
List<String> list = userRoleRepository.getAllRegularUser();
|
||||
logger.info("List = {} ", list);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testSave() {
|
||||
// ChatUser fromUser = new ChatUser();
|
||||
|
Loading…
Reference in New Issue
Block a user