Spring boot + jpa 懒取

Posted

技术标签:

【中文标题】Spring boot + jpa 懒取【英文标题】:Spring boot + jpa lazy fetch 【发布时间】:2016-05-30 16:55:25 【问题描述】:

我是新来的,希望大家能帮帮我。

我对惰性获取类型有疑问。

我不喜欢获取所有的关系,只是在我需要的时候。 当我使用CommandLineRunner.run 方法时,没关系,它正在获取 Lazily,但如果我从 RestController 调用该方法,它总是急切地 获取,但我不希望那样。

我试过了:

带 DTO 和不带 DTO 对象。 将所有依赖项升级到最新版本。 将@RestController注解改为@Controller 在存储库中的自定义方法上使用 LEFT JOIN FETCH 的 @Query 注释 @Lazy(value=true) @Basic(fetch = FetchType.LAZY) @LazyCollection(value = LazyCollectionOption.TRUE) @LazyToOne(value = LazyToOneOption.NO_PROXY) @ElementCollection(fetch = FetchType.LAZY) 当然,我尝试将 fetch = FetchType.LAZY 并放入 @ManyToMany、@ManyToOne... 注释以及级联内容中。 将 @PersistenceContext 私有 EntityManager 管理器与 createQuery() 一起使用;

最后,我将 Spring Security 与 CustomUserDetailsS​​ervice 结合使用。当我登录时,它会返回 User 对象。 如果@Transactional 注释在ServiceImpl 类上,它会eagerly 获取,但如果我删除该注释,它会延迟获取,但这仅适用于登录。

你有什么想法吗?

存储库:

import hu.pte.clms.model.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor

ServiceImpl:

import hu.pte.clms.model.domain.User;
import hu.pte.clms.repository.UserRepository;
import hu.pte.clms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> listAll()
        return userRepository.findAll();
    

    /* And another methods with this scheme */

控制器:

import hu.pte.clms.model.domain.User;
import hu.pte.clms.model.dto.UserDTO;
import hu.pte.clms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class UserController

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user/all", method = RequestMethod.GET)
    public ResponseEntity<List<UserDTO>> listAll()
        return new ResponseEntity<>(userService.listAll().stream().map(user ->
                new UserDTO(user.getId(), user.getFirstName(), user.getLastName(), user.getCity(), user.getCountry(), user.getBio(), user.getPictureUrl())).collect(Collectors.toList()), HttpStatus.OK);
        

    @RequestMapping(value = "/auth/user")
    public LoginResult get()
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(!auth.getName().equals("anonymousUser"))
            User user = userService.findByUsername(auth.getName());
            return new LoginResult(auth.getName(), auth.getAuthorities(), user);
        
        return null;
    

登录结果:

import hu.pte.clms.model.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

public class LoginResult implements UserDetails
    private String password;
    private String name;
    private User user;
    private Collection<? extends GrantedAuthority> authorities;

    public LoginResult(String name, Collection<? extends GrantedAuthority> authorities, User user)
        this.name = name;
        this.authorities = authorities;
        this.user = user;
    

    public LoginResult(String username, String s, boolean b, boolean userNonExpired, boolean credentialsNonExpired, boolean userNonLocked, Collection<? extends GrantedAuthority> authorities)

    public LoginResult(String username, String password, List<GrantedAuthority> grantedAuthorities)
        this.name = username;
        this.password = password;
        this.authorities = grantedAuthorities;
    

用户:

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import hu.pte.clms.model.domain.relationship.UserSkill;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "USER")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class User implements Serializable

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "USERNAME")
    private String username;

    @Column(name = "PASSWORD")
    private String password;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "AGE")
    private Short age;

    @Column(name = "SEX")
    private String sex;

    @Column(name = "PHONE")
    private String phone;

    @Column(name = "SKYPE")
    private String skype;

    @Column(name = "PRIMARY_EMAIL")
    private String primaryEmail;

    @Column(name = "SECONDARY_EMAIL")
    private String secondaryEmail;

    @Column(name = "CITY")
    private String city;

    @Column(name = "COUNTRY")
    private String country;

    @Column(name = "BIO")
    private String bio;

    @Column(name = "PICTURE_URL")
    private String pictureUrl;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "CONFIG_ID")
    private Config config;

    @JsonIgnore
    @ManyToMany
    @JoinTable(name = "REL_USER_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
    private List<Role> roles = new ArrayList<>();

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "REL_USER_SECURITY_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "SECURITY_ROLE_ID"))
    private List<SecurityRole> securityRoles = new ArrayList<>();

    @ManyToMany(mappedBy = "user")
    private List<UserSkill> skills = new ArrayList<>();

    @JsonIgnore
    @ManyToMany
    @JoinTable(name = "REL_USER_PROJECT", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "PROJECT_ID"))
    private List<Project> projects = new ArrayList<>();

    @JsonIgnore
    @OneToMany(mappedBy = "reviewed", cascade = CascadeType.ALL)
    private List<Review> reviews = new ArrayList<>();

 /*Getters & setters*/

用户DTO:

import com.fasterxml.jackson.annotation.JsonInclude;
import hu.pte.clms.model.domain.*;
import hu.pte.clms.model.domain.relationship.UserSkill;
import java.util.ArrayList;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class UserDTO

    private Long id;
    private String username;
    private String password;
    private String firstName;
    private String lastName;
    private Short age;
    private String sex;
    private String phone;
    private String skype;
    private String primaryEmail;
    private String secondaryEmail;
    private String city;
    private String country;
    private String bio;
    private String pictureUrl;
    private Config config;
    private List<Role> roles = new ArrayList<>();
    private List<SecurityRole> securityRoles = new ArrayList<>();
    private List<UserSkill> skills = new ArrayList<>();
    private List<Project> projects = new ArrayList<>();
    private List<Review> reviews = new ArrayList<>();

    public UserDTO()
    

    public UserDTO(User user)
        this.id = user.getId();
        this.username = user.getUsername();
        this.password = user.getPassword();
        this.firstName = user.getFirstName();
        this.lastName = user.getLastName();
        this.age = user.getAge();
        this.sex = user.getSex();
        this.phone = user.getPhone();
        this.skype = user.getSkype();
        this.primaryEmail = user.getPrimaryEmail();
        this.secondaryEmail = user.getSecondaryEmail();
        this.city = user.getCity();
        this.country = user.getCountry();
        this.bio = user.getBio();
        this.pictureUrl = user.getPictureUrl();
        this.config = user.getConfig();
        this.roles = user.getRoles();
        this.securityRoles = user.getSecurityRoles();
        this.skills = user.getSkills();
        this.projects = user.getProjects();
        this.reviews = user.getReviews();
    

    public UserDTO(Long id, String firstName, String lastName, String city, String country, String bio, String pictureUrl)
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.city = city;
        this.country = country;
        this.bio = bio;
        this.pictureUrl = pictureUrl;
    
    /*Getters & setters*/

Application.java:

import hu.pte.clms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application implements CommandLineRunner

    @Autowired
    private UserService userService;

    public static void main(String[] args) 
        SpringApplication app = new SpringApplication(Application.class);
        app.setShowBanner(false);
        app.setRegisterShutdownHook(true);  
    

    @Override
    public void run(String... strings) throws Exception
        userService.listAll();
    

pom.xml:

    ...

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>$spring.boot.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>$spring.boot.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>$spring.boot.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>$spring.boot.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
    </dependencies>

Application.yml:

spring.datasource:
  url: jdbc:mysql://localhost:3306/clms?autoReconnect=true
  username: clms
  password: clms
  testOnBorrow: true
  validationQuery: SELECT 1
  driverClassName: com.mysql.jdbc.Driver

【问题讨论】:

您能否尝试缩小您的问题范围,以免发布您的整个应用程序代码? 我分享了它,因为也许我出了什么问题,有人注意到了。我的问题一开始就写了,不需要经过代码。 正如@aribeiro 所说,最好尝试提供一个简化的示例来说明正在发生的事情,以便人们对其进行测试。我没有看到所有对解决此案有用的代码。看看this。 好吧,抱歉。下次我会使用你的建议。 究竟什么是急切不应该的? 【参考方案1】:

**对不起,我真的不能写评论,所以我在这里写。

我在这里唯一能看到的是服务类中的@Transactional 注释,并且需要默认传播。

另一件事,在杰克逊@JsonIgnoreProperties 和@JsonIgnore 不能一起工作。最好将要忽略的属性放在 @JsonIgnoreProperties(value = "projects", "reviews")

我希望这会有所帮助。

【讨论】:

感谢您的反馈,我修改了。但不幸的是,它并没有改变任何东西。 :(【参考方案2】:

您应该在数据库中执行映射,而不是在您的代码中。即

SELECT NEW package.UserDTO(user.id, user.firstName, user.lastName, user.city, user.country, user.bio, user.pictureUrl) FROM User user …

当您使用这样的 JPQL 查询时,保证只会获取构造函数中的字段。

【讨论】:

以上是关于Spring boot + jpa 懒取的主要内容,如果未能解决你的问题,请参考以下文章

Spring-boot官方案例分析之data-jpa

Spring Boot JPA 连接验证不起作用

带有 Spring Boot 的 Spring Data JPA 中的多个数据源,[重复]

Spring Boot - Mysql Driver - JPA - 在服务器运行发布请求很长时间后显示无法打开 JPA EntityManager 进行事务处理

Spring Boot快速入门:使用MyBatis(注解形式)进行数据库操作

Elasticsearch学习 spring boot整合Elasticsearch的原生方式