JPA EntityGraph使用Spring的不同视图 [英] JPA EntityGraph with different views using Spring

查看:226
本文介绍了JPA EntityGraph使用Spring的不同视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring应用程序。登录后,我正在调用getUserByEmail()方法。

I have a Spring application. After login, I am invoking getUserByEmail() method.

我只需要用户和角色数据。基于角色,我将显示不同的视图,每个视图都有不同的数据,需要不同的User实体。

I only need user and role data. Based on role I am going to display different views, Each view has different data and requiring different child entity of User.

好像我必须用不同的方式调用getUserByEmail()子实体。

Seems like I have to call getUserByEmail() with different child entity.

这是我的部分代码涉及实体:

This is my partial code involving Entities:

EntityGraph(value = "withAddresses", type = EntityGraphType.FETCH)
public class User{
  public firstName;
  public lastName;

  @OneToMany(fetch = FetchType.LAZY)
  public List<Address> addresses

  @OneToMany(fetch = FetchType.LAZY)
  public List<Order> orders;             
 }

 public userRepository extend jPaRepository(User.class,Long){
  @EntityGraph(name="withAdresses")
  getUserByEmail(String email)

  /* if possible */
  @EntityGraph(name="withOrder")
  getUserByEmail(String email)
 }




  1. 是否可以有两个具有相同查询名称的User对象图?
    因为我需要不同视图的不同数据。

  1. Is it possible to have two graph of User objects with same query name? Because I need different data for different views.

此外,当切换到新视图(spring控制器中的新调用)时,来自之前的事务视图将被关闭,我必须进行新的调用,以便与用户拥有不同的数据。我不明白如果你不是同一个交易服务方法,fetch lazy是多么有用,除非我没有遗漏什么。

Also, when switching to a new view (new call in spring controller), transaction from previous view will be closed already and I have to make new call to have different data with user. I don't understand how fetch lazy is helpful if you are not in same transaction service method, unless I am not missing something.

例如,如果我需要orderWiew.html中的订单数据,则延迟加载订单不会有帮助我必须再次调用相同的用户数据和其他订单数据

For example if I need order data in "orderWiew.html" lazy load of order is not going to help I have to make another full call to same user data and additional Order data

推荐答案

仅仅是关于使用多个实体图的消息:在我工作的地方,我们使用了Spring Data可以为查询方法使用多个前缀的事实。我们设置了一个约定,即具有不同前缀的方法具有不同的实体图。因此,例如,findUserByEmail(String)可以使用比readUserByEmail(String)更懒的图。

Just a sugestion about using multiple entity graphs: where I work we used the fact Spring Data can use multiple prefixes for query methods. We set a convention that methods with different prefixes have different entity graphs. So, for example, findUserByEmail(String) could use a more lazy graph than readUserByEmail(String).

不幸的是我不认为Spring Data支持将实体图传递给以动态的方式使用。但是,您可以实现它并将其添加到您的存储库中。要做到这一点,你应该:

Unfortunately I don't think Spring Data supports passing the entity graph to use in a dynamic way. You could implement it and have it added to your repository, though. To do that you should:

创建一个声明新方法的接口(但不扩展JpaRepository或其他存储库接口)

Create an interface that declares the new method (but does not extend JpaRepository or other repository interfaces)

public interface UserCustomOperations{
    User findUserByEmail(String email, String entityGraph);
}

使您的存储库接口扩展该接口。

Make your repository interface extend that interface.

public interface UserRepository extends JPaRepository<User,Long>, UserCustomOperations{
    // Not much to do here anymore
}

创建一个在同一个包中实现自定义行为的类,后缀为Impl(默认情况下)。

Create a class that implements your custom behaviour in the same package, with a suffix of Impl (by default).

public class  UserRepositoryImpl implements UserCustomOperations{
    public User findUserByEmail(String email, String entityGraph){
        // Inject the EntityManager and execute standard Jpa query with the entity graph set
    }
}

这篇关于JPA EntityGraph使用Spring的不同视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆