小编Mat*_*ter的帖子

Spring boot + jpa lazy fetch

我是新来的,我希望你能帮助我们.

我有一个懒惰的提取类型的问题.

我不想取得所有关系,只要我需要.当我使用CommandLineRunner.run方法时,它没关系,它正在提取Lazily,但如果我从中调用该方法RestController,它总是热切地提取,但我不想要那样.

我试过了:

  • 与DTO和没有DTO对象.
  • 将所有依赖项升级到最新版本.
  • 将@RestController注释更改为@Controller
  • 使用LEFT JOIN FETCH对存储库中的自定义方法进行@Query注释
  • @Lazy(值=真)
  • @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();

最后,我正在使用SpringUserDetailsS​​ervice的Spring安全性.当我登录时,它返回User对象.如果@Transactional注释是在ServiceImpl类上,它正在急切地获取,但是如果我删除了那个注释,那么它就是Lazily,但这只能在登录时使用.

你有什么主意吗?

库:

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{
}
Run Code Online (Sandbox Code Playgroud)

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 …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa spring-mvc

8
推荐指数
1
解决办法
4715
查看次数

标签 统计

hibernate ×1

java ×1

jpa ×1

spring ×1

spring-mvc ×1