问题

我有一个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就是这个代码,根据this guide编写

public interface PersonRepository extends JpaRepository<Person, Long> {
}

但是,在这个控制器中,我实际上需要延迟数据。如何触发加载?

试图访问它将失败

未能懒惰地初始化角色集合:no.dusken.momus.model.Person.roles,无法初始化代理 - 没有会话

或其他例外取决于我尝试的内容。

Myxml-description,如果需要的话。

谢谢。


#1 热门回答(163 赞)

你必须对延迟集合进行显式调用才能对其进行初始化(通常的做法是为此目的调用.size())。在Hibernate中有一个专门的方法(Hibernate.initialize()),但JPA没有相应的。当然,当会话仍然可用时,你必须确保调用已完成,因此请使用@Transactional注释控制器方法。另一种方法是在Controller和Repository之间创建一个中间服务层,它可以公开初始化惰性集合的方法。

###更新:

请注意,上述解决方案很简单,但会导致对数据库的两个不同查询(一个针对用户,另一个针对其角色)。如果要实现更好的性能,请将以下方法添加到Spring Data JPA存储库接口:

public interface PersonRepository extends JpaRepository<Person, Long> {

    @Query("SELECT p FROM Person p JOIN FETCH p.roles WHERE p.id = (:id)")
    public Person findByIdAndFetchRolesEagerly(@Param("id") Long id);

}

此方法将使用JPQL的fetch joinclause在单个往返数据库中急切地加载角色关联,因此将减轻上述解决方案中两个不同查询所引起的性能损失。


#2 热门回答(25 赞)

虽然这是一篇旧帖子,但请考虑使用@NamedEntityGraph(Javax Persistence)和@EntityGraph(Spring Data JPA)。组合起作用。

@Entity
@Table(name = "Employee", schema = "dbo", catalog = "ARCHO")
@NamedEntityGraph(name = "employeeAuthorities",
            attributeNodes = @NamedAttributeNode("employeeGroups"))
public class EmployeeEntity implements Serializable, UserDetails {
// your props
}

然后是如下的弹簧回购

@RepositoryRestResource(collectionResourceRel = "Employee", path = "Employee")
public interface IEmployeeRepository extends PagingAndSortingRepository<EmployeeEntity, String>           {

    @EntityGraph(value = "employeeAuthorities", type = EntityGraphType.LOAD)
    EmployeeEntity getByUsername(String userName);

}

#3 热门回答(12 赞)

你有一些选择

  • 在存储库上编写一个返回初始化实体的方法,如R.J所建议的那样。

更多的工作,最好的表现。

  • 使用OpenEntityManagerInViewFilter为整个请求保持会话打开。

减少工作量,通常在网络环境中可以接受。

  • 使用辅助类在需要时初始化实体。

减少工作量,当OEMIV不可用时很有用,例如在Swing应用程序中,但在存储库实现上也可能有用,可以一次性初始化任何实体。

对于最后一个选项,我编写了一个实用程序类,JpaUtils以在某些deph中初始化实体。

例如:

@Transactional
public class RepositoryHelper {

    @PersistenceContext
    private EntityManager em;

    public void intialize(Object entity, int depth) {
        JpaUtils.initialize(em, entity, depth);
    }
}

原文链接