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