如何在控制器中从Hibernate / JPA加载延迟获取的项目

我有一个Person类:


@Entity

public class Person {


    @Id

    @GeneratedValue

    private Long id;


    @ManyToMany(fetch = FetchType.LAZY)

    private List<Role> roles;

    // etc

}

与多对多的关系是懒惰的。


在我的控制器中


@Controller

@RequestMapping("/person")

public class PersonController {

    @Autowired

    PersonRepository personRepository;


    @RequestMapping("/get")

    public @ResponseBody Person getPerson() {

        Person person = personRepository.findOne(1L);

        return person;

    }

}

而PersonRepository只是此代码,是根据本指南编写的


public interface PersonRepository extends JpaRepository<Person, Long> {

}

但是,在此控制器中,我实际上需要惰性数据。如何触发加载?


尝试访问它将会失败


无法延迟初始化角色集合:no.dusken.momus.model.Person.roles,无法初始化代理-没有会话


或其他例外情况,具体取决于我的尝试。


我的xml-description,如果需要的话。


谢谢。


UYOU
浏览 639回答 3
3回答

叮当猫咪

您必须对延迟集合进行显式调用才能对其进行初始化(通常的做法是.size()为此目的而调用)。在Hibernate中,有一个专用于此(Hibernate.initialize())的方法,但是JPA没有与此等效的方法。当然,当会话仍然可用时,您必须确保调用已完成,因此请使用注释控制器方法@Transactional。一种替代方法是在控制器和存储库之间创建一个中间服务层,该服务层可以公开初始化惰性集合的方法。更新:请注意,上述解决方案很简单,但是会导致对数据库进行两个不同的查询(一个用于用户,另一个用于其角色)。如果要获得更好的性能,请在Spring Data JPA存储库接口中添加以下方法:public interface PersonRepository extends JpaRepository<Person, Long> {&nbsp; &nbsp; @Query("SELECT p FROM Person p JOIN FETCH p.roles WHERE p.id = (:id)")&nbsp; &nbsp; public Person findByIdAndFetchRolesEagerly(@Param("id") Long id);}此方法将使用JPQL的fetch join子句在一次往返数据库中热切地加载角色关联,因此将减轻上述解决方案中两个截然不同的查询引起的性能损失。

30秒到达战场

尽管这是一篇老文章,但请考虑使用@NamedEntityGraph(Javax Persistence)和@EntityGraph(Spring Data JPA)。组合有效。例@Entity@Table(name = "Employee", schema = "dbo", catalog = "ARCHO")@NamedEntityGraph(name = "employeeAuthorities",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attributeNodes = @NamedAttributeNode("employeeGroups"))public class EmployeeEntity implements Serializable, UserDetails {// your props}然后如下所示的春季回购@RepositoryRestResource(collectionResourceRel = "Employee", path = "Employee")public interface IEmployeeRepository extends PagingAndSortingRepository<EmployeeEntity, String>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; @EntityGraph(value = "employeeAuthorities", type = EntityGraphType.LOAD)&nbsp; &nbsp; EmployeeEntity getByUsername(String userName);}

波斯汪

你有一些选择根据RJ建议,在存储库上编写一个返回初始化实体的方法。更多工作,最佳性能。使用OpenEntityManagerInViewFilter可使整个请求的会话保持打开状态。工作量少,通常在网络环境中可以接受。需要时,使用帮助器类来初始化实体。较少的工作,在未选择OEMIV时(例如在Swing应用程序中)很有用,但在存储库实现中一次也可以初始化任何实体可能也有用。对于最后一个选项,我编写了一个实用程序类JpaUtils来在某些时候初始化实体。例如:@Transactionalpublic class RepositoryHelper {&nbsp; &nbsp; @PersistenceContext&nbsp; &nbsp; private EntityManager em;&nbsp; &nbsp; public void intialize(Object entity, int depth) {&nbsp; &nbsp; &nbsp; &nbsp; JpaUtils.initialize(em, entity, depth);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP