简体   繁体   English

如何在 Spring Controller 中获取与 JPA 和 Hibernate 的 FetchType.LAZY 关联

[英]How to fetch FetchType.LAZY associations with JPA and Hibernate in a Spring Controller

I have a Person class:我有一个 Person 类:

@Entity
public class Person {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToMany(fetch = FetchType.LAZY)
    private List<Role> roles;
    // etc
}

With a many-to-many relation that is lazy.具有懒惰的多对多关系。

In my controller I have在我的控制器中,我有

@Controller
@RequestMapping("/person")
public class PersonController {
    @Autowired
    PersonRepository personRepository;

    @RequestMapping("/get")
    public @ResponseBody Person getPerson() {
        Person person = personRepository.findOne(1L);
        return person;
    }
}

And the PersonRepository is just this code, written according to this guide而 PersonRepository 就是根据本指南编写的这段代码

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

However, in this controller I actually need the lazy-data.但是,在这个控制器中,我实际上需要惰性数据。 How can I trigger its loading?我怎样才能触发它的加载?

Trying to access it will fail with尝试访问它会失败

failed to lazily initialize a collection of role: no.dusken.momus.model.Person.roles, could not initialize proxy - no Session未能懒惰地初始化角色集合:no.dusken.momus.model.Person.roles,无法初始化代理 - 没有会话

or other exceptions depending on what I try.或其他例外取决于我的尝试。

My xml-description , in case needed.我的xml-description ,以防万一。

Thanks.谢谢。

You will have to make an explicit call on the lazy collection in order to initialize it (common practice is to call .size() for this purpose).您必须对惰性集合进行显式调用才能对其进行初始化(通常为此调用.size() )。 In Hibernate there is a dedicated method for this ( Hibernate.initialize() ), but JPA has no equivalent of that.在 Hibernate 中有一个专门的方法( Hibernate.initialize() ),但 JPA 没有对应的方法。 Of course you will have to make sure that the invocation is done, when the session is still available, so annotate your controller method with @Transactional .当然,当会话仍然可用时,您必须确保调用已完成,因此请使用@Transactional注释您的控制器方法。 An alternative is to create an intermediate Service layer between the Controller and the Repository that could expose methods which initialize lazy collections.另一种方法是在控制器和存储库之间创建一个中间服务层,它可以公开初始化惰性集合的方法。

Update:更新:

Please note that the above solution is easy, but results in two distinct queries to the database (one for the user, another one for its roles).请注意,上述解决方案很简单,但会导致对数据库的两个不同查询(一个针对用户,另一个针对其角色)。 If you want to achieve better performace add the following method to your Spring Data JPA repository interface:如果您想获得更好的性能,请将以下方法添加到您的 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);

}

This method will use JPQL's fetch join clause to eagerly load the roles association in a single round-trip to the database, and will therefore mitigate the performance penalty incurred by the two distinct queries in the above solution.此方法将使用 JPQL 的fetch join子句在到数据库的单次往返中急切加载角色关联,因此将减轻上述解决方案中两个不同查询所导致的性能损失。

Though this is an old post, please consider using @NamedEntityGraph (Javax Persistence) and @EntityGraph (Spring Data JPA).虽然这是一篇旧帖子,但请考虑使用@NamedEntityGraph(Javax Persistence)和@EntityGraph(Spring Data JPA)。 The combination works.组合有效。

Example例子

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

and then the spring repo as below然后春天回购如下

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

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

}

You have some options你有一些选择

  • Write a method on repository that return a initialized entity as RJ suggested.在存储库上编写一个方法,返回 RJ 建议的初始化实体。

More work, best performance.更多的工作,最好的表现。

  • Use OpenEntityManagerInViewFilter to keep session open for the entire request.使用 OpenEntityManagerInViewFilter 为整个请求保持会话打开。

Less work, usually acceptable in web enviroments.更少的工作,在网络环境中通常是可以接受的。

  • Use a helper class to initialize entities when required.需要时使用辅助类来初始化实体。

Less work, useful when OEMIV is not at option, for example in a Swing application, but may be useful too on repository implementations to initialize any entity in one shot.较少的工作,当 OEMIV 不可用时很有用,例如在 Swing 应用程序中,但在存储库实现中可能也很有用,以一次性初始化任何实体。

For the last option, I wrote a utility class, JpaUtils to initilize entities at some deph.对于最后一个选项,我编写了一个实用程序类JpaUtils来在某个深度初始化实体。

For example:例如:

@Transactional
public class RepositoryHelper {

    @PersistenceContext
    private EntityManager em;

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

it can only be lazily loaded whilst within a transaction.它只能在事务中延迟加载。 So you could access the collection in your repository, which has a transaction - or what I normally do is a get with association , or set fetchmode to eager.因此,您可以访问存储库中具有事务的集合 - 或者我通常做的是get with association ,或将 fetchmode 设置为eager。

Spring Data JpaRepository Spring Data JpaRepository

The Spring Data JpaRepository defines the following two methods: Spring Data JpaRepository定义了以下两个方法:

  • getOne , which returns an entity proxy that is suitable for setting a @ManyToOne or @OneToOne parent association when persisting a child entity . getOne ,它返回一个实体代理,适用于在持久化子实体时设置@ManyToOne@OneToOne父关联。
  • findById , which returns the entity POJO after running the SELECT statement that loads the entity from the associated table findById ,它在运行从关联表加载实体的 SELECT 语句后返回实体 POJO

However, in your case, you didn't call either getOne or findById :但是,就您而言,您没有调用getOnefindById

Person person = personRepository.findOne(1L);

So, I assume the findOne method is a method you defined in the PersonRepository .因此,我假设findOne方法是您在PersonRepository定义的方法。 However, the findOne method is not very useful in your case.但是, findOne方法在您的情况下不是很有用。 Since you need to fetch the Person along with is roles collection, it's better to use a findOneWithRoles method instead.由于您需要获取Personroles集合,因此最好改用findOneWithRoles方法。

Custom Spring Data methods自定义 Spring Data 方法

You can define a PersonRepositoryCustom interface, as follows:您可以定义一个PersonRepositoryCustom接口,如下所示:

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

}

public interface PersonRepositoryCustom {
    Person findOneWithRoles(Long id);
}

And define its implementation like this:并像这样定义它的实现:

public class PersonRepositoryImpl implements PersonRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Person findOneWithRoles(Long id)() {
        return entityManager.createQuery("""
            select p 
            from Person p
            left join fetch p.roles
            where p.id = :id 
            """, Person.class)
        .setParameter("id", id)
        .getSingleResult();
    }
}

That's it!而已!

我认为您需要OpenSessionInViewFilter来在视图渲染期间保持会话打开(但这不是很好的做法)。

You can do the same like this:你可以这样做:

@Override
public FaqQuestions getFaqQuestionById(Long questionId) {
    session = sessionFactory.openSession();
    tx = session.beginTransaction();
    FaqQuestions faqQuestions = null;
    try {
        faqQuestions = (FaqQuestions) session.get(FaqQuestions.class,
                questionId);
        Hibernate.initialize(faqQuestions.getFaqAnswers());

        tx.commit();
        faqQuestions.getFaqAnswers().size();
    } finally {
        session.close();
    }
    return faqQuestions;
}

Just use faqQuestions.getFaqAnswers().size()nin your controller and you will get the size if lazily intialised list, without fetching the list itself.只需在控制器中使用 faqQuestions.getFaqAnswers().size() ,如果延迟初始化列表,您将获得大小,而无需获取列表本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPA /休眠FetchType.LAZY无法正常工作 - JPA/Hibernate FetchType.LAZY is not working Hibernate @OneToOne(fetch = FetchType.LAZY)无效 - Hibernate @OneToOne(fetch = FetchType.LAZY) is not working 使用Kotlin进行Hibernate:@ManyToOne(fetch = FetchType.LAZY) - Hibernate with Kotlin: @ManyToOne(fetch = FetchType.LAZY) 休眠@MappedSuperclass @ManyToOne(fetch = FetchType.LAZY) - Hibernate @MappedSuperclass @ManyToOne(fetch=FetchType.LAZY) Hibernate @ManyToOne(fetch = FetchType.LAZY)被忽略 - Hibernate @ManyToOne(fetch = FetchType.LAZY) ignored Spring Data JPA,Hibernate,@ ManyToOne(fetch = FetchType.LAZY)和org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - Spring Data JPA, Hibernate, @ManyToOne(fetch=FetchType.LAZY) and org.hibernate.LazyInitializationException: could not initialize proxy - no Session FetchType.Lazy 在 spring 引导中的 JPA 中不起作用 - FetchType.Lazy not working in JPA in spring boot 尽管已设置FetchType.Lazy,但Hibernate / Jpa还是急切地获取@ManyToOne对象。 - Hibernate/Jpa fetch eagerly @ManyToOne object although FetchType.Lazy is setted 使用带有fetch = FetchType.LAZY的休眠状态从表中获取数据 - Fetch data from table using hibernate with fetch=FetchType.LAZY 为什么Hibernate(JPA)对于ManyToOne关系忽略FetchType.LAZY? - Why does Hibernate (JPA) ignore FetchType.LAZY for ManyToOne relations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM